Answer to Question #308537 in Java | JSP | JSF for lee

Question #308537

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

1
Expert's answer
2022-03-10T07:15:50-0500


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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS