0

Ok guys, first off sorry if this code is messy and if my equals() is completely wrong, but this is my first time using it.

I'm trying to create an equals method to check if two lines are equal, two lines are defined as equal if two end points are the same.

My first question is, am i even close with the method in the Point class, and also how would I call the equals() method in the Point class from the Line class?

Thanks for any and all help.

public class Point {

private int x;
private 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 toString() {
    return "x=" + x + ", y=" + y;
}

public boolean equals(Object o) {
      if (!(o instanceof Point)) {
            return false;
        }
        return (x == ((Point) o).x && y == ((Point) o).y);
}
}

}

for the return this.y, it says "unreachable code". Also Should my Object be "Point" or "Line"?

public class Line {
private Point beg, end;
Color color;

public Line(Point beg, Point end, String color) {
    this.beg = beg;
    this.end = end;


public Point getEnd() {
    return end;
}

public Point getBeg() {
    return beg;
}

public Color getColor() {
    return color;
}

public String toString() {
    return "Beg=" + beg + ", End=" + end + ", Color" + color.toString();
}

Line() {
    return this.beg.equals(Point.x);
    return this.end.equals(Point.y);
}

}

I updated the equals() in point class but I'm still having trouble calling it from the Line class, would it be a similar fix?

Thanks for all the help.

user2745043
  • 189
  • 2
  • 7
  • 24
  • You might have a look at http://stackoverflow.com/questions/7655396/implementing-the-equals-method-in-java . – Joshua Taylor Sep 05 '13 at 14:39
  • Check out this [how to override equals method article](http://javarevisited.blogspot.com/2011/02/how-to-write-equals-method-in-java.html) for further information. – TheKojuEffect Sep 05 '13 at 14:47

8 Answers8

1

It is unreachable code because you exited the method just before by returning from it . You probably meant this.x == ((Point)o).x && this.y == ((Point)o).y.

You should have something like:

@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Point)) {
        return false;
    }
    return (x == ((Point) o).x && y == ((Point) o).y);
}

Also, for the Line class you compare the relevant fields (beg and end).

@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Line)) {
        return false;
    }
    return (beg == ((Line) o).beg && end == ((Line) o).end);
}

When you override equals (and hashCode, always write them in pairs) it is a good idea to use the @Override annotation, in case you write public boolean equals(Point o) (notice the parameter is Point, not Object) by mistake, so the compiler will catch it.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • Ok thanks this fixed my problem, but I'm still having trouble with the correct way to call it from the Line class? – user2745043 Sep 05 '13 at 15:16
0

It says unreachable code since your equals() method will finish execution on return this.x == ((Point)o).x;

Try using:

public boolean equals(Object o) {
    if(this.x == ((Point)o).x && this.y == ((Point)o).y) {
        return true;
    }
    return false;
}

Which can be shortened to:

public boolean equals(Object o) {
    return this.x == ((Point)o).x && this.y == ((Point)o).y;
}
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

It should be:

public class Point {
    public boolean equals(Object o) {
        return (this.x == ((Point)o).x) && (this.y == ((Point)o).y);
    }
}

This way your code is not unreachable.

Also you must check:

if (!(o instanceof Point)) return false;
Omar Mainegra
  • 4,006
  • 22
  • 30
  • You should handle cases where `o` is `null`. All these answers should. – Sotirios Delimanolis Sep 05 '13 at 14:42
  • 1
    `null` is never an instanceof a class. So the instanceof check will handle `null` correctly. Checking for `null` explicitly is just a matter of style (and micro performance optimization). – Holger Sep 05 '13 at 16:19
0

After the first return call the method exits so the second one is never evaluated.

Use return (this.x == ((Point)o).x) && (this.y == ((Point)o).y); instead.

public class Point {
    public boolean equals(Object o) {
        if(o == null) return false;
        if(!(o instanceOf Point) return false;
        return (this.x == ((Point)o).x) && (this.y == ((Point)o).y);
    }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

You can't ever have 2 return statements one after another, because the second one is always unreachable. You can use this instead, evaluating if x equals o.x and y equals o.y:

 public boolean equals(Object o) {
        return this.x == ((Point)o).x && this.y == ((Point)o).y;
    }
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
0

The 'return' statement exits the method, which is why any code further down will never be reached. Also, it's good practice to check if the passed object is of the correct class, to avoid class cast exceptions:

public boolean equals(Object o) {
    if (o instanceof Point) {
        Point po = (Point) o;
        return this.x == po.x && this.y == po.y;
    }
    return false;
}

The argument needs to be 'Object' and not 'Point' so this method overrides the Object.equals method

0

you are using

return this.x == ((Point)o).x;
return this.y == ((Point)o).y;

This code will return after checking for x but will never reach y.

So, in order to make your code work correctly use

return (this.x == ((Point)o).x)&&(this.y == ((Point)o).y);

This code will check for x and y both and then return

Saheb
  • 1,392
  • 3
  • 13
  • 33
0

If you override equals, you SHOULD/MUST override hashCode() also!

public boolean equals(Object o) {
   if(o==this){return true;}
   if(!o instanceof Point){return false;}
   Point p = (Point)o;
   return (this.x==p.x && this.y==p.y);
}
user2693979
  • 2,482
  • 4
  • 24
  • 23