Defining Constructors and Initializing Attributes using the Parameterized Constructor

The Procedure

Real Lab Procedure 

  1. public class Point{ 
  2.  private int x; 
  3.  private int y; 
  4.  Point (int xInit, int yInit) { 
  5.   this.x = xInit; 
  6.   this.y = yInit; 
  7.  } 
  8.  private void setX(int xCoord) { 
  9.   this.x = xCoord; 
  10.  } 
  11.  private void setY(int yCoord) { 
  12.   this.y = yCoord; 
  13.  } 
  14.  private int getX() { 
  15.   return this.x; 
  16.  } 
  17.  private int getY() { 
  18.   return this.y; 
  19.  } 
  20.  private void print() { 
  21.   System.out.println("(" + this.x + "," + this.y+ ")"); 
  22.  } 
  23. public class Driver { 
  24.  public static void main(String[]){ 
  25.   Point p2 = new Point(4,5); 
  26.   p2.print(); 
  27.  } 

 

  • The "Point" class is defined, which represents a point in a two-dimensional space. It has two private integer attributes, "x" and "y." 
  • The "Point" class has a constructor with two parameters, "xInit" and "yInit." When a new "Point" object is created using the constructor, the values passed as arguments are assigned to the "x" and "y" attributes of that object. 
  • The "Point" class also contains private methods: 
  • "setX(int xCoord)" sets the value of the "x" attribute to the specified "xCoord" value. 
  • "setY(int yCoord)" sets the value of the "y" attribute to the specified "yCoord" value. 
  • "getX()" returns the current value of the "x" attribute. 
  • "getY()" returns the current value of the "y" attribute. 
  • "print()" prints the current coordinates of the point in the format "(x, y)" using the "System.out.println()" method. 
  • The "Driver" class is defined, which contains the "main" method. The "main" method is the entry point of the program. 
  • Inside the "main" method, the following steps occur: 
  • A new instance of the "Point" class is created with the statement: "Point p2 = new Point(4, 5);" This line creates a new "Point" object and initializes it with the coordinates (4, 5) by invoking the constructor with the values 4 and 5. 
  • The "print()" method is called on the "p2" object using the statement "p2.print();". This method prints the current coordinates of the point (4, 5) using the "System.out.println()" method. 
  • When the code is executed, the following output is displayed : (4 , 5) 
  • This output represents the coordinates of the point created, which are (4, 5) in this case. 
  • In summary, the code creates a "Point" object, initializes it with the coordinates (4, 5), and then prints the coordinates using the "print()" method. 

 

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.