The problem
In this illustration, write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in Figure 3.9b. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to 10 / 2 and its vertical distance to (0, 0) is less than or equal to 5.0 / 2. Test your program to cover all cases.)
Breaking it down
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a pont with two coordinates: ");
double x = input.nextDouble();
double y = input.nextDouble();
input.close();
double xDistance = Math.pow(x * x, 0.5D);
double yDistance = Math.pow(y * y, 0.5D);
if ((yDistance <= 2.5) && (xDistance <= 5.0)) {
System.out.println("Point (" + x + ", " + y + ") is in the rectangle.");
} else {
System.out.println("Point (" + x + ", " + y + ") is not in the rectangle.");
}
}
Output
Enter a pont with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle.