Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use method?

Tags:

java

generics

I have an assignment to implement a generic Pair interface that represents ordered pairs like (x,y). I'm trying to write the equals method, but I have to make the parameter a general object instead of a pair, so I don't know how to get the coordinates of it. I get a symbol not found error when I try to compile because the Object class doesn't have a fst and snd method. Should I be throwing an exception or something? Note: My professor gave us a template and I'm just filling in the methods, I don't think I'm allowed to change params or methods.

Here's relevant code:

    public class Pair<T1,T2> implements PairInterface<T1,T2>
{
private T1 x;
private T2 y;

public Pair(T1 aFirst, T2 aSecond)
{
    x = aFirst;
    y = aSecond;
}

/**
 * Gets the first element of this pair.
 * @return the first element of this pair.
 */
public T1 fst()
{
    return x;
}

/**
 * Gets the second element of this pair.
 * @return the second element of this pair.
 */
public T2 snd()
{
    return y;
}

...

    /**
 * Checks whether two pairs are equal. Note that the pair
 * (a,b) is equal to the pair (x,y) if and only if a is
 * equal to x and b is equal to y.
 * @return true if this pair is equal to aPair. Otherwise
 * return false.
 */
public boolean equals(Object otherObject)
{
    if(otherObject == null)
    {
        return false;
    }

    if(getClass() != otherObject.getClass())
    {
        return false;
    }
    T1 a = otherObject.fst();
    T2 b = otherObject.snd();
    if (x.equals(a) && y.equals(b))
    {
        return true;
    }
    else 
    {
        return false;
    }
}

These are the errors I get:

    ./Pair.java:66: cannot find symbol
    symbol  : method fst()
    location: class java.lang.Object
    T1 a = otherObject.fst();
                      ^
    ./Pair.java:67: cannot find symbol
    symbol  : method snd()
    location: class java.lang.Object
    T2 b = otherObject.snd();
                      ^
like image 608
doublediamondz Avatar asked Apr 17 '26 08:04

doublediamondz


1 Answers

The parameter to the equals method is an Object, which is not guaranteed to have your methods fst or snd, hence the compiler error. To be able to call those methods, you need to have a Pair object.

It is standard practice to test the class of the object passed in, to see if it's even the same class as this, returning false if it's not the same class. Usually that is done with instanceof, followed by a cast if true. The cast allows the compiler to treat the object as if it is the class that you say it is. This allows the compiler to find the methods you want to call.

if(otherObject == null)
{
    return false;
}

// Use instanceof
if(!(otherObject instanceof Pair))
{
    return false;
}
// Cast - use <?, ?>; we don't know what they are.
Pair<?, ?> otherPair = (Pair<?, ?>) otherObject;
Object a = otherPair.fst();
Object b = otherPair.snd();
// Simplified return
return (x.equals(a) && y.equals(b));
like image 144
rgettman Avatar answered Apr 19 '26 21:04

rgettman