-4

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));
}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 4
    Possible duplicate of [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Joakim Danielson Oct 27 '19 at 19:26

1 Answers1

0

The parameter named center is a Circle; the instance variable center (i.e. this.center) is a Point.

Two circles are concentric if they have the same center; thus, you want to compare the center of center with this.center. If you have trouble doing that, you might want to look at the question that @JoakimDanielson linked to.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I know two circles are concentric if they have the same center. However, I don't know why using public boolean isConcentricWith(Circle center) { return (equals(this.center)); } will always return false and always print not concentric. c1 = c3 is concentric and it should print out c1 and c3 are concentric. – Gregory C Ammons Oct 27 '19 at 19:56
  • a) That isn't the code you originally posted (which is still there). b) What do you think `equals`, when called by itself, compares its argument to? – Scott Hunter Oct 27 '19 at 23:12