Question 3 (5 points)
Q3: Write a Java class Point to represent a point with x and y coordinates. The class Point has the following:
Attributes:
- double x
- double y
Methods:
- Setters and getters for x and y.
- distance(Point p): double. This method returns the distance between the two points p and the calling object. Hint: to compute the distance between two points, use the equation Distance = sqrt((x - x)^2 + (y - y)^2).
Write another class TestPoint that does the following:
1. Declare two Point objects p1 and p2.
2. Read the values of x and y for both points.
3. Compute the distance between the two points using the method distance. If the distance is less than or equal to 5.0, you should print the distance and exit. Otherwise, keep reading two more points and printing the distance until you get two points with a distance less than or equal to 5.0.
Sample run 1:
Enter x and y for p1: 1 2
Enter x and y for p2: 6 8
Distance is 7.810249675906654 which is > 5. Try again.
Enter x and y for p1: 2 6
Enter x and y for p2: 9 18
Distance is 13.892443989449804 which is > 5. Try again.
Enter x and y for p1: 1 1
Enter x and y for p2: 4 3
The two points are close. The distance between them is 3.605551275463989 which is less than 5.
Sample run 2:
Enter x and y for p1:
Enter x and y for p2:
The two points are close. The distance between them is 0.0 which is less than or equal to 5.
Sample run 3:
Enter x and y for p1: 10 5
Enter x and y for p2: 15 5
The two points are close. The distance between them is 5.0 which is less than or equal to 5.