haloid2010
haloid2010

Reputation: 87

Basic java equals() override help needed

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.

Upvotes: 1

Views: 635

Answers (6)

Ikigai programmeh
Ikigai programmeh

Reputation: 398

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
}

Upvotes: 0

rossum
rossum

Reputation: 15685

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

Upvotes: 0

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

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.

Upvotes: 0

Giovanni
Giovanni

Reputation: 4015

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?

Upvotes: 0

Beau Grantham
Beau Grantham

Reputation: 3456

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;
}

Upvotes: 1

Michael Berry
Michael Berry

Reputation: 72274

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

Upvotes: 4

Related Questions