JAVA on eclipse 1. Write a class called Point that has two int fields x and y co
ID: 3687251 • Letter: J
Question
JAVA on eclipse
1. Write a class called Point that has two int fields x and y coordinates. The class should
have the necessary accessors, mutators and constructors. (You may choose to write the
compareTo method as well)
2. Write another class called Circle that has 2 fields; a double field called radius and a Point
object that stores coordinates of the central point of the circle. The class should have the
necessary accessors, mutators and constructors. Use the compareTo method to define a
natural order for the Circle class. The objects should be ordered based on the area of the
circle. If the circles have equal area then they should be ordered according to the x-
coordinates and if the x-coordinates are same then use y-coordinates to define the order.
3. Also write a client class that creates an array of circles and use the Arrays.sort() method
to see if the compareTo method you wrote works as intended.
For your output, I need an input with the same area but different points, an input with the same area and x but different y.
For example:
radius = 25pi x = 6 y = 8;
radius = 25pi x = 7 y = 9;
radius = 25pi x = 7 y = 16;
Explanation / Answer
Point class will look something like shown below:
class Point {
int x;
int y;
public Point (int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String toPrint() {
return "[" + x + "," + y + "]";
}
}
Circle class can be coded as:
public class Circle {
Point centre = null;
int radius = 0;
public Circle(int x, int y, int rad) {
centre = new Point(x, y);
radius = rad;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.