Which Quadrant does the Point belong to

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 int whichQuadrant() {
  34.   if(isOrigin())
  35.    return "origin";
  36.   else if((this.x < 0)&&(this.y < 0))
  37.    return "quadrant 3";
  38.   else if((this.x < 0)&&(this.y == 0))
  39.    return "-ve X-Axis";
  40.   else if((this.x == 0)&&(this.y < 0))
  41.    return "-ve Y-Axis";
  42.   else if((this.x > 0)&&(this.y > 0))
  43.    return "quadrant 1";
  44.   else if((this.x > 0)&&(this.y < 0))
  45.    return "quadrant 4";
  46.   else if((this.x < 0)&&(this.y > 0))
  47.    return "quadrant 2";
  48.   else if((this.x > 0)&&(this.y == 0))
  49.    return "+ve X-Axis";
  50.   else if((this.x == 0)&&(this.y > 0))
  51.    return "+ve Y-Axis";
  52.  }
  53. }
  54. public class Driver {
  55.  public static void main(String[]){
  56.   Point p0 = new Point();
  57.   System.out.println(p0.whichQuadrant());
  58.   Point p1 = new Point(4,3);
  59.   System.out.println(p1.whichQuadrant());
  60.   Point p2 = new Point(7,-5);
  61.   System.out.println(p2.whichQuadrant());
  62.   Point p3 = new Point(-10,4);
  63.   System.out.println(p3.whichQuadrant());
  64.   Point p4 = new Point(-4,-10);
  65.   System.out.println(p4.whichQuadrant());
  66.   Point p5 = new Point(7,0);
  67.   System.out.println(p5.whichQuadrant());
  68.   Point p6 = new Point(0,8);
  69.   System.out.println(p6.whichQuadrant());
  70.   Point p7 = new Point(0,-3);
  71.   System.out.println(p7.whichQuadrant());
  72.   Point p8 = new Point(-1,0);
  73.   System.out.println(p8.whichQuadrant());
  74.  }
  75. }
  • 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: 

  • Define a class named 'Point'. 
  • Declare two private integer variables, 'x' and 'y' inside the 'Point' class. 
  • Define two constructors for the 'Point' class. 
  • The first constructor (Point()) is a parameterless constructor that initializes the 'x' and 'y' variables to 0. 
  • The second constructor (Point(int xInit, int yInit)) takes two integer parameters and initializes the 'x' and 'y' variables with the provided values. 
  • Define 5 private methods in the 'Point' class such as: 
  • 'setX(int xCoord)' and 'setY(int yCoord)' are setter methods that set the values of 'x' and 'y' respectively. 
  • 'getX()' and 'getY()' are getter methods that return the values of 'x' and 'y' respectively. 
  • 'print()' method prints the coordinates of the point in the format "(x,y)" using 'System.out.println()'. 
  • Define a private boolean method isOrigin(). 
  • It checks if the point is at the origin (0,0) by comparing the values of 'x' and 'y'. 
  • If both 'x' and 'y' are equal to 0, the method returns true, indicating that the point is at the origin. Otherwise, it returns false. 
  • The final method within the 'Point' class is 'whichQuadrant()', which is a public method that determines the quadrant or axis in which the point is located. 
  • It first calls the 'isOrigin()' method. If the point is at the origin, it returns the string "origin". 
  • If the point is not at the origin, a series of conditional statements check the values of 'x' and 'y' to determine the quadrant or axis. 
  • The method returns the corresponding string based on the conditions met. 
  • After the Point class definition, a new class named Driver is declared. 
  • The 'Driver' class contains the main method, which serves as the entry point of the program. 
  • Within the main method, several instances of the 'Point' class (p0, p1, p2, etc.) are created using different constructors. 
  • The 'whichQuadrant()' method is called on each 'Point' object, and the result is printed using 'System.out.println()'. 
  • The program execution ends, and the output is displayed based on the conditions in the 'whichQuadrant()' 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.