In plane geometry, the x- and y axis of a 2D Cartesian system divide it into four infinite regions called quadrants. Your task is to model each point with an x-value and y-value. Apart from the constructors, getters, and setters, implement a getQuadrant method to determine the region in which a given point falls.
Sample Run1 Sample Run2
Enter the x and y value: 5 8 Enter the x and y value: 7 -3
Output1: Point (5,8) is in Quadrant I Output2: Point (7,-3) is in Quadrant IV
import java.util.*;
class Point {
private int x;
private int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String getQuadrant() {
if (x == 0 && y == 0) {
return "Point: (" + x + ", " + y + ") is the origin";
} else if (y == 0) {
return "Point: (" + x + ", " + y + ") is on the x-axis";
} else if (x == 0) {
return "Point: (" + x + ", " + y + ") is on the y-axis";
} else if (y > 0) {
if (x > 0) {
return "Point: (" + x + ", " + y + ") is in Quadrant I";
} else {
return "Point: (" + x + ", " + y + ") is in Quadrant II";
}
} else {
if (x < 0) {
return "Point: (" + x + ", " + y + ") is in Quadrant III";
}
}
return "Point: (" + x + ", " + y + ") is in Quadrant IV";
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}
}
class App {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the x and y value: ");
int x = keyboard.nextInt();
int y = keyboard.nextInt();
Point p = new Point(x, y);
System.out.print(p.getQuadrant());
keyboard.close();
}
}
Comments
Leave a comment