views:

113

answers:

3

The instructions say: Create an equals method that takes an object reference and returns true if the given object equals this object. * Hint: You'll need 'instanceof' and cast to a (Position)

I have:

class Position {

    private double x,y;
    private int id;

public boolean instanceOf(Object a)
    {
        boolean isInstance;
        if ((Position)a instanceof Position)
            isInstance=true;
        else
            isInstance=false;
        return isInstance;
    }

But I'm doing it wrong, and I dont know what my problem is. I'm new to objects so this is kind of confusing me..

+1  A: 

He clearly said create an equals() method, and you got it right here as well. But even after all this you created a method named instanceOf(). Why?

Moreover, you may get a ClassCastException on this line, in case some other type of object is passed.

    if ((Position)a instanceof Position)

Which means, precisely, that you should only cast the object to Position, after making sure that the object passed is of type Position. Which means now its good to go further with comparison. If its not of type Position in the first place, then why should you bother comparing it further. Because it can never be equal. I hope you are getting my point.

[Edited to answer the question in comment]

Well, the actual instance should be of type Position. Once we know that its of type Position using instanceof operator, we know its safe to cast it to type Position from type Object. Now the question is why we need to cast? Because initially the instance was of type Object, and we were not sure about the actual type. You might already know that in Java you can assign any type to Object type, because Object is a parent of every class in Java. Hence, before applying instanceof we were not sure about the type of instance.

In case, you don't cast it, you will never be able to access its properties, namely, x, y, and id. You can try accessing any of these on actual object without casting. You will know you can't. Hence, you can't compare those either. I hope, this made it clear.

Adeel Ansari
So if it is of type Position, why do I have to cast it into type Position?
fprime
@fprime: See the addendum.
Adeel Ansari
+1  A: 

As this is a homework question, I won't give a detail answer. You have to implement a equals() method, not a instanceOf() method. I think this link help:

The code example in them should be sufficient. Ask again if you need more hint.

J-16 SDiZ
Hmm ok so the only thing I'm missing is this line in their example: Car c = (Car) o; How would I implement this in my case?
fprime
Ok your second link helped. However, I'm confused about this line:if(this == obj)....what exactly does that do, whats 'this' in this case?
fprime
@fprime: I think you need a good beginner book.
Adeel Ansari
@fprime: the `if(this == obj)` is just an optimization. it still works without this line.
J-16 SDiZ
@StackTrace: you mean hashCode(), right? ;)
rob
@rob That is right.. deleted and posted again
Jayan
You need to override equals - and - hashCode() .. Since this is homework, next problem may related to hashCode() :) – StackTrace 9 hours ago
Jayan
A: 
public boolean equals(Object a) {
    if ((a instanceof Position) && (((Position)a).x == this.x) 
        && ((Position)a).y == this.y)
            && ((Position)a).id == this.id)) {
        return true;
    } else {
        return false;
    }
}

Depends on what makes a Position equal to another. In this case is equal if x, y and id are the same.

Seitaridis