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.

4
  • 3
    What is the question exactly? Also, that code is apparently from the Point class, not the Line one.
    – Viruzzo
    Commented Nov 23, 2011 at 13:16
  • 2
    I don't get what your problem is, actually... Commented Nov 23, 2011 at 13:17
  • 2
    .equals() takes an Object. You need to make sure the object is not null and the correct type.
    – Gray
    Commented Nov 23, 2011 at 13:17
  • Sorry for it being horribly worded. I think I have it all sorted out.
    – haloid2010
    Commented Nov 23, 2011 at 13:21

6 Answers 6

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.)

3
  • 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
    Commented Nov 23, 2011 at 13:21
  • No problem - see here for an in depth explanation: stackoverflow.com/questions/27581/… Commented Nov 23, 2011 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. Commented Nov 23, 2011 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;
}
0
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?

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.

1
  • Overload? Did you mean override?
    – javanna
    Commented Nov 23, 2011 at 13:49
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) { ... }
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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.