Projection of a Point on X-axis and Y-axis

The Procedure 

Real Lab Procedure

 

  1. import java.io.*; 
  2. import java.util.Scanner; 
  3. class Point { 
  4.   public int x; 
  5.   public int y; 
  6.   Point() { 
  7.    this.x = 0; 
  8.    this.y = 0; 
  9.   } 
  10.   Point(int xInit, int yInit) { 
  11.    this.x = xInit; 
  12.    this.y = yInit; 
  13.   } 
  14.   private void setX(int xCoord) { 
  15.    this.x = xCoord; 
  16.   } 
  17.   private void setY(int yCoord) { 
  18.    this.y = yCoord; 
  19.   } 
  20.   private int getX() { 
  21.    return this.x; 
  22.   } 
  23.   private int getY() { 
  24.    return this.y; 
  25.   } 
  26.   public void print() { 
  27.    System.out.println("(" + this.x + "," + this.y + ")"); 
  28.   } 
  29.   private boolean isOrigin() { 
  30.    if ((this.x == 0) && (this.y == 0)) 
  31.     return true; 
  32.    else 
  33.     return false; 
  34.   } 
  35.   public String whichQuadrant() { 
  36.    if (isOrigin()) 
  37.     return "origin"; 
  38.    else if ((this.x < 0) && (this.y < 0)) 
  39.     return "quadrant 3"; 
  40.    else if ((this.x < 0) && (this.y == 0)) 
  41.     return "-ve X-Axis"; 
  42.    else if ((this.x == 0) && (this.y < 0)) 
  43.     return "-ve Y-Axis"; 
  44.    else if ((this.x > 0) && (this.y > 0)) 
  45.     return "quadrant 1"; 
  46.    else if ((this.x > 0) && (this.y < 0)) 
  47.     return "quadrant 4"; 
  48.    else if ((this.x < 0) && (this.y > 0)) 
  49.     return "quadrant 2"; 
  50.    else if ((this.x > 0) && (this.y == 0)) 
  51.     return "+ve X-Axis"; 
  52.    else 
  53.     return "+ve Y-Axis"; 
  54.   } 
  55.   public Point xProjection() { 
  56.    Point p = new Point(this.x, 0); 
  57.    return p; 
  58.   } 
  59.   public Point yProjection() { 
  60.    Point q = new Point(0, this.y); 
  61.    return q; 
  62.   } 
  63. public class Driver { 
  64.   public static void main(String args[]) { 
  65.    Scanner s = new Scanner(System.in); 
  66.    int m1, m2, n1, n2; 
  67.    System.out.println("Enter the values of X and Y for calculating x projection"); 
  68.    m1 = s.nextInt(); 
  69.    m2 = s.nextInt(); 
  70.    Point p0 = new Point(m1, m2); 
  71.    Point xP = p0.xProjection(); 
  72.    System.out.println("X projection of point " + m1 + " and " + m2 + " is "); 
  73.    xP.print(); 
  74.    System.out.println("Enter the values of X and Y for calculating y projection"); 
  75.    n1 = s.nextInt(); 
  76.    n2 = s.nextInt(); 
  77.    Point p1 = new Point(n1, n2); 
  78.    Point yP = p1.yProjection(); 
  79.    System.out.println("Y projection of point " + n1 + " and " + n2 + " is "); 
  80.    yP.print(); 
  81.   } 

The given program is written in Java and consists of two classes: Point and Driver. 

The Point class represents a point in a two-dimensional coordinate system. It has the following attributes: 

  • x and y: Public integer variables to store the coordinates of the point. 

Constructors: 

  • Default constructor: Initializes x and y to 0. 
  • Parameterized constructor: Initializes x and y with the provided values. 

Private methods: 

  • setX(int xCoord): Sets the value of x to the given xCoord. 
  • setY(int yCoord): Sets the value of y to the given yCoord. 
  • getX(): Returns the current value of x. 
  • getY(): Returns the current value of y. 

Public methods: 

  • print(): Prints the coordinates of the point in the format "(x, y)". 
  • isOrigin(): Checks if the point is at the origin (x = 0, y = 0) and returns true if it is, false otherwise. 
  • whichQuadrant(): Determines and returns the quadrant or axis where the point lies based on its coordinates. 
  • xProjection(): Creates a new Point object with the x-coordinate of the current point and y-coordinate of 0, then returns it. 
  • yProjection(): Creates a new Point object with the y-coordinate of the current point and x-coordinate of 0, then returns it. 

The Driver class contains the main() method and serves as the entry point for the program. Here's what the main() method does: 

  • Creates a Scanner object (s) to read input from the user. 
  • Declares variables m1, m2, n1, and n2 to store the input values. 
  • Prompts the user to enter the values of X and Y for calculating the x-projection. 
  • Reads the values of m1 and m2 from the user. 
  • Creates a Point object p0 using the parameterized constructor with m1 and m2 as arguments. 
  • Calls the xProjection() method on p0 to get the x-projection point and stores it in a new Point object xP. 
  • Prints the message "X projection of point and is ". 
  • Calls the print() method on xP to print the x-projection point. 
  • Prompts the user to enter the values of X and Y for calculating the y-projection. 
  • Reads the values of n1 and n2 from the user. 
  • Creates a Point object p1 using the parameterized constructor with n1 and n2 as arguments. 
  • Calls the yProjection() method on p1 to get the y-projection point and stores it in a new Point object yP. 
  • Prints the message "Y projection of point and is ". 
  • Calls the print() method on yP to print the y-projection point. 

In summary, the program allows the user to input coordinates of points, calculates their x-projections and y-projections using the Point class, and prints the results to the console. 

 

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.