Defining Constructors and Initializing Attributes using the Default Constructor

The Procedure 

Real Lab Procedure 

  1. public class Point{
  2.  private int x;
  3.  private int y;
  4.  Point() {
  5.   this.x = 0;
  6.   this.y = 0;
  7.  }
  8.  Point(int xInit, int yInit) {
  9.   this.x = xInit;
  10.   this.y = yInit;
  11.  }
  12.  private void setX(int xCoord) {
  13.   this.x = xCoord;
  14.  }
  15.  private void setY(int yCoord) {
  16.   this.y = yCoord;
  17.  }
  18.  private int getX() {
  19.   return this.x;
  20.  }
  21.  private int getY() {
  22.   return this.y;
  23.  }
  24.  private void print() {
  25.   System.out.println("(" + this.x + "," + this.y+ ")");
  26.  }
  27. }
  28. public class Driver {
  29.  public static void main(String[]){
  30.   Point p0 = new Point();
  31.   p0.print();
  32.  }
  33. }
  • The above program displayed on the left side of the simulation.
  • On the right side of the simulation, there are two buttons [Class Diagram and Sequence Diagram].
  • First, click on the Class Diagram then the below diagrams will be shown.

There are two classes in the program, and it has three parts [class name, variables, function] 

                               Fig. 1 

                              Fig. 2 

  • Fig. 1 shows the class diagram of class Point.
  • Fig. 2 shows the class diagram of class Driver.
  • Then click on the sequence diagram which will show how the program that on the left side is executed.

                       Sequence diagram 

Following steps are explaining Sequence Diagram.

The given diagram defines two classes, "Point" and "Driver." Let's go through the code and understand its functionality.

The "Point" class: 

  • It has two private integer variables, "x" and "y," representing the coordinates of a point. 
  • It contains two constructors. The first constructor is a default constructor with no parameters. It initializes the "x" and "y" coordinates to 0. 
  • The second constructor takes two parameters, "xInit" and "yInit," and assigns them to the "x" and "y" variables, respectively. 
  • It has four private methods: 
  • "setX" and "setY" are setter methods used to set the values of "x" and "y" coordinates, respectively. 
  • "getX" and "getY" are getter methods used to retrieve the values of "x" and "y" coordinates, respectively. 
  • "print" is a method that prints the coordinates of the point in the format "(x,y)" using the "System.out.println()" statement. 

The "Driver" class: 

  • It contains the main method, which serves as the entry point of the program. 
  • Inside the main method, it creates an object "p0" of the "Point" class using the default constructor. This creates a point object with coordinates (0,0). 
  • It calls the "print" method on the "p0" object, which prints the coordinates of the point using the "System.out.println()" statement. 

Overall, the code defines a "Point" class that represents a point in a two-dimensional plane. It provides constructors to initialize the coordinates and getter and setter methods to access and modify the coordinates. The "Driver" class demonstrates the usage of the "Point" class by creating an object and printing its coordinates. 


Simulator Procedure 

  • There are two buttons [Class Diagram and Sequence Diagram] on the right side of the canvas.
  • First, click on the Class Diagram then the Class Diagrams will be shown.
  • Then click on the Sequence Diagram, it will display two buttons – Previous and Next.
  • The Next button shows the next sequence, and the Previous button shows the previous sequence.
  • After all the sequence is completed the result box will be shown.
  • Click on the Reset button to redo the Simulation.