Which Quadrant does the Point belong to

Objective

To implement a program that defines which quadrant the point belongs to.

 

Theory 

The algorithm for this experiment is given below: 

  1. Define a class named Point.
  2. Declare private instance variables x and y of type int to store the coordinates of a point. 
  3. Define a default constructor Point().
  4. Set x and y to 0. 
  5. Define a parameterized constructor Point(int xInit, int yInit). 
  6.  Initialize x with xInit and y with yInit. 
  7. Define private methods: 
    1. setX(int xCoord): Set x to the provided xCoord. 
    2. setY(int yCoord): Set y to the provided yCoord. 
    3. getX(): Return the value of x. 
    4. getY(): Return the value of y. 
    5. print(): Print the coordinates of the point in the format "(x,y)". 
    6. isOrigin():  - Check if x and y are both 0. 
    7. If true, return true; otherwise, return false.    
  8. Define a public method whichQuadrant().
  9. Check if the point is at the origin using the isOrigin() method.
  10. If true, return the string "origin". 
  11. Check the coordinates of the point to determine the quadrant or axis. 
    • If x and y are both negative, return "quadrant 3". 
    • If x is negative and y is 0, return "-ve X-Axis". 
    • If x is 0 and y is negative, return "-ve Y-Axis". 
    • If x and y are both positive, return "quadrant 1". 
    • If x is positive and y is negative, return "quadrant 4". 
    • If x is negative and y is positive, return "quadrant 2". 
    • If x is positive and y is 0, return "+ve X-Axis". 
    • If x is 0 and y is positive, return "+ve Y-Axis". 
  12. Define a class named Driver.
  13. Define the main method.
  14. Create instances of the Point class with different coordinates. 
    • Create p0 using the default constructor. 
    • Create p1 using the parameterized constructor with coordinates (4, 3). 
    • Create p2 using the parameterized constructor with coordinates (7, -5). 
    • Create p3 using the parameterized constructor with coordinates (-10, 4). 
    • Create p4 using the parameterized constructor with coordinates (-4, -10). 
    • Create p5 using the parameterized constructor with coordinates (7, 0). 
    • Create p6 using the parameterized constructor with coordinates (0, 8). 
    • Create p7 using the parameterized constructor with coordinates (0, -3). 
    • Create p8 using the parameterized constructor with coordinates (-1, 0). 
  15. Print the quadrants or axes of each point using the whichQuadrant() method and the System.out.println() statement. 

This program consists of two Java classes: Point and Driver, which are used to work with cartesian coordinates and determine in which quadrant or axis a point is located. The 'Point' class represents a point with 'x' and 'y' coordinates and provides methods for setting and getting these coordinates. It also has a whichQuadrant() method that determines the point's location in the cartesian plane and returns a corresponding string value.

The Driver class contains the main method, which serves as the program's entry point. In the main method, several Point objects are instantiated with different coordinates. The whichQuadrant() method is then called on each object, and the results are printed to the console.

The whichQuadrant() method categorizes points into various categories, such as origin, quadrants (1 to 4), or the 'x' and y-axes. The results are presented as strings indicating the point's location within the Cartesian plane.

When you run the program, you will see a series of output lines, each specifying the location of a particular point based on its coordinates. This program is a simple but useful demonstration of working with Cartesian coordinates and determining the relative position of points within a coordinate system.

 

Learning Outcomes 

  • Students will learn how to define a class and create objects from that class. 
  • Students will learn how to use constructors.
  • Students will learn how to define and use methods within a class. 
  • Students will learn about getter methods, conditional statements, and the use of a boolean method.