Defining Getters - Reading Values of Attributes

The Procedure 

Real Lab Procedure 

  • 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 execution starts from the main method in the Driver class.
  • Inside the main method, a new 'Point' object named 'p' is created using the default constructor.
  • The 'Point' object 'p' is created with initial values of 'x' and 'y' set to 0 because the 'Point' class does not have a custom constructor and no explicit initialization of 'x' and 'y' is done. 
  • The 'setX(2)' method is called on the 'p' object, passing the value 2.
  • Inside the 'setX(int xCoord)' method of the 'Point' class, the parameter 'xCoord' (which has the value 2) is assigned to the 'x' attribute of the 'Point' object.
  • Similarly, the 'setY(3)' method is called on the 'p' object, passing the value 3.
  • Inside the 'setY(int yCoord)' method of the 'Point' class, the parameter 'yCoord' (which has the value 3) is assigned to the 'y' attribute of the 'Point' object.
  • The 'getX()' method is called on the 'p' object to retrieve the value of 'x'. 
  • Inside the 'getX()' method of the 'Point' class, the value of 'x' is returned. 
  • Similarly, the 'getY()' method is called on the 'p' object to retrieve the value of 'y'.
  • Inside the 'getY()' method of the 'Point' class, the value of 'y' is returned.
  • The returned values of 'x' and 'y' are concatenated with the string ("(" + p.getX() + "," + p.getY() + ")") and printed to the console.
  • The output would be "(2,3)", indicating the coordinates of the point. 
  • In summary, the code creates a 'Point' object, sets its 'x' and 'y' coordinates using the 'setX()' and 'setY()' methods, retrieves the coordinates using the 'getX()' and 'getY()' methods, and then prints the coordinates to the console. The code demonstrates encapsulation by using private methods to access and modify the object's attributes. 

 

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.