-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointInRectangle.java
More file actions
31 lines (25 loc) · 1.02 KB
/
PointInRectangle.java
File metadata and controls
31 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
public class PointInRectangle {
public static void main(String[] args) {
// Create Scanner to read input
Scanner scanner = new Scanner(System.in);
// Prompt to enter point (x, y)
System.out.print("Enter a point with two coordinates: ");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
// Close scanner
scanner.close();
// Dimensions of rectangle
double width = 10.0;
double height = 5.0;
// Calculate half dimensions
double halfWidth = width / 2.0;
double halfHeight = height / 2.0;
// Check if point is inside rectangle (Used Math.abs to make life easier)
if (Math.abs(x) <= halfWidth && Math.abs(y) <= halfHeight) {
System.out.printf("Point (%.1f, %.1f) is in the rectangle%n", x, y);
} else {
System.out.printf("Point (%.1f, %.1f) is not in the rectangle%n", x, y);
}
}
}