In this Program, I would like to create a class Circle and check multiple circle if they are concentric with each other. However, I am not sure what's wrong with my code in the isConcentricWith method. Why it must be Circle in the parameter but not Point and why I cannot use this.center == center (it will produce error under the parameter of Circle.
Would anyone point out my error and give explanation and solution with it? Thanks!
public class Circle {
private Point center;
private int radius;
public static void main(String[] args) {
Circle c1 = new Circle(new Point(1, 2), 3);
Circle c2 = new Circle(4, 5, 6);
Circle c3 = new Circle(1, 2, 8);
Circle c4 = new Circle(12, 5, 2);
System.out.println("C1: " + c1);
System.out.println("C2: " + c2);
System.out.println("C3: " + c3);
System.out.println("C4: " + c4);
System.out.println();
if (c1.isConcentricWith(c2)) {
System.out.println("C1 and C2 are concentric");
} else {
System.out.println("C1 and C2 are not concentric");
}
if (c1.isConcentricWith(c3)) {
System.out.println("C1 and C3 are concentric");
} else {
System.out.println("C1 and C3 are not concentric");
}
}
public Circle(Point center, int radius) {
this.center = center;
this.radius = radius;
}
public Circle(int x_coordinates, int y_coordinates, int radius) {
center = new Point(x_coordinates, y_coordinates);
this.radius = radius;
}
public Point getCenter() {
return center;
}
public int getRadius() {
return radius;
}
public void setCenter(Point center) {
this.center = center;
}
public void setRadius(int radius) {
this.radius = radius;
}
public boolean isConcentricWith(Circle center) {
return this.center == center;
}
public String toString(){
return getCenter() +" with radius " + getRadius();
}
Update: isConcentricWith
will always return false when I wrote it like this:
public boolean isConcentricWith(Circle center) {
return (equals(this.center));
}