0

my assignment (in summary) is to override the following methods, in my code: compareTo (interface Comparable), toString (class GeometricObject) and equals (class Object).

I am new to Java and am not sure if I am using the @override methods correctly. I would appreciate any feedback, and if you could point me to the right direction.

Partial code that I'm using to override the methods stated:

Circle.java partial code

//Circle.java
@Override /**Override equals(class Object**/
public boolean equals(Object o) {
    return this.compareTo((Circle)o) == 0;
}

@Override /**Override compareTo(interface Comparable)**/
public int compareTo(Circle o) {
    if (this.radius > o.radius)
        return 1;
    else if (this.radius < o.radius)
        return -1;
    else
        return 0;
}

@Override /**Override toString(class GeometricObject**/
public String toString(GeometricObject o) {
    return super.toString()+"\nDate created:"+getDateCreated()+"\nRadius:"+radius; }

GeometricObject.java partial code

//GeometricObject.java
/** Return color */
public String getColor() {
    return color;
}

/** Set a new color */
public void setColor(String color) {
    this.color = color;
}

/** Return filled. Since filled is boolean,
 *  the get method is named isFilled */
public boolean isFilled() {
    return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
    this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
    return dateCreated;
}

@Override
public String toString() {
    return "created on"+dateCreated+"\ncolor:"+color+" and filled:" + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();

}
Anna Nguyen
  • 33
  • 1
  • 11
  • If you use `@Override` incorrectly, it throws an error. That's what it's there for, to tell programmers if they messed up. It won't check your code, but your declaration will be correct or it causes an error. – markspace Apr 08 '19 at 01:32
  • Well, `equals` should be checking the type before blindly casting it – MadProgrammer Apr 08 '19 at 01:32
  • Also, technically, if you override `equals`, you should override `hashcode` at well. But your instructor might not have taught that yet, so stick to the assignment. – markspace Apr 08 '19 at 01:35
  • `toString` is not correct on `Circle`, it should not take a parameter (unless you mean different method than `Object.toString`). – markspace Apr 08 '19 at 01:36

0 Answers0