1

I am currently working on a simple line program in java. There will be no actual GUI, so it is completely text based.

We are required to have a point class and a line class that includes to point objects.

The problem I encountered was involving the equals method for my point class. Considering each point only has two POSITIVE int values, an x and a y, and I'm having issues here, I'm worried I will have problems when I have to compare lines, which will involve comparing the points, the int width and the string color.

This is what the code for my equals method for my point class looks.

@Override
public boolean equals(Point that) {
    if(this==that)
        return true;
    //if
    if(this.x==that.getX() && this.y==that.getY())
        return true;
    return false;
}

Any and all help would be appreciated.

javanna
  • 53,926
  • 12
  • 135
  • 121
haloid2010
  • 87
  • 1
  • 1
  • 6

6 Answers6

4

The signature needs to contain Object, not Point. You then need the obvious checks to make sure the object is in fact a point and it's non-null.

Apart from that, as you've put it there I don't see any problem with the method, it's reflexive, symmetric, consistent and transitive as far as I can make out. If your class used doubles then I'd say put a delta value in when comparing them - but obviously with ints that's not a problem.

The indirect problem though is that you really should override hashcode as well to behave in the same way, otherwise you could run into strange issues when adding your points to collections that make use of hashcode() (by contract they're expected to compare the objects in the same way.)

Michael Berry
  • 61,291
  • 17
  • 134
  • 188
  • Thanks for that. I was wondering why the IDE was telling me it wasn't overriding equals. The other thing I don't understand is how/why the hashcode needs/is edited. – haloid2010 Nov 23 '11 at 13:21
  • No problem - see here for an in depth explanation: http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java – Michael Berry Nov 23 '11 at 13:22
  • The method in the question will throw a null pointer exception if the object to compare is null. Be sure to check for null. – Beau Grantham Nov 23 '11 at 13:24
1

For a simple class, containing only two integers, the following hashCode and equals methods are appropriate:

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    result = prime * result + x;
    result = prime * result + y;
    return result;
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Point other = (Point) obj;
    if (x != other.x)
        return false;
    if (y != other.y)
        return false;
    return true;
}
Beau Grantham
  • 3,339
  • 5
  • 29
  • 43
0

Let me try this:

@Override
public boolean equals(Object o) 
{
    //Cast the object o to Point instance
    if(!(o instanceof Point))
        return false;
    Point obj = (Point)o;
    if(obj==null)
        return false;//Checking if null
    else if.....//test properties using cast object obj
}
Tom
  • 14,120
  • 16
  • 41
  • 47
0

You showed the Line class equals method but I don't think a Line can equals to a Point. Line can contain a Point. did you mean Point equals method?

Giovanni
  • 3,561
  • 1
  • 19
  • 25
0

Your question seems to be about performance and the Object.equals() method. You should read this:

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode%28%29

Your class should override the hashCode() method.

Alexis Dufrenoy
  • 11,106
  • 10
  • 73
  • 122
0

Look at the definition of the Object.equals() method. In particular look at the type of the parameter. Any override of equals() must have an Object parameter.

public boolean equals(Object that) { ... }
Kara
  • 5,650
  • 15
  • 48
  • 55
rossum
  • 14,325
  • 1
  • 19
  • 34