To Find the Origin of a Point using a Function in Java

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.  private boolean isOrigin() { 
  28.   if((this.x==0)&&(this.y==0)) 
  29.    return true; 
  30.   else 
  31.    return false; 
  32.  } 
  33. public class Driver { 
  34.  public static void main(String[]){ 
  35.   Point p = new Point(4,7); 
  36.   System.out.println(p.isOrigin()); 
  37.   Point q = new Point(); 
  38.   System.out.println(q.isOrigin()); 
  39.  } 

Here is an algorithm that describes the execution flow of the code: 

Create a class named "Point"

  • Declare private integer variables "x" and "y" in the "Point" class. 
  • Define a default constructor "Point()" inside the "Point" class.
  • Set "x" to 0. 
  • Set "y" to 0. 
  • Define a parameterized constructor "Point(int xInit, int yInit)" inside the "Point" class.
  • Set "x" to the value of "xInit". 
  • Set "y" to the value of "yInit". 
  • Define a private method "setX(int xCoord)" inside the "Point" class.
  • Set "x" to the value of "xCoord". 
  • Define a private method "setY(int yCoord)" inside the "Point" class.
  • Set "y" to the value of "yCoord". 
  • Define a private method "getX()" inside the "Point" class.
  • Return the value of "x". 
  • Define a private method "getY()" inside the "Point" class.
  • Return the value of "y". 
  • Define a private method "print()" inside the "Point" class.
  • Print the coordinates in the format "(x,y)". 
  • Define a private method "isOrigin()" inside the "Point" class.
  • Check if "x" is equal to 0 and "y" is equal to 0. 
  • If both conditions are true, return true; otherwise, return false. 

Create a class named "Driver". 

  • Define the main method inside the "Driver" class.
  • Create a "Point" object "p" with coordinates (4, 7) using the parameterized constructor. 
  • Print the result of "p.isOrigin()". 
  • Create a "Point" object "q" using the default constructor. 
  • Print the result of "q.isOrigin()". 

Note: The algorithm assumes that the missing access modifiers (public or private) for the main method and the "Point" class methods are public for the main method and private for the "Point" class methods. 

 

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.