Search code examples
javatypesadtabstract

Trying to create an ADT that pairs two objects


I'm working on an Abstract Data Type called Pair in Java. It's supposed to take two objects and group them together in this data type. This is supposed to take less than 30 minutes, but I have been working at it for 3 and a half hours. I believe I have the first two methods right, but I cannot figure out reverse or equals. You can see what I attempted in the code:

public class Pair<T1,T2> implements PairInterface<T1,T2>
{
    // TO DO: Your instance variables here.
    public T1 first;
    public T2 second;

    public Pair(T1 aFirst, T2 aSecond)
    {
        first = aFirst;
        second = aSecond;
    }

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

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

    /* Gets a NEW pair the represents the reversed order
     * of this pair. For example, the reverse order of the
     * pair (x,y) is (y,x).
     * @return a NEW pair in reverse order
     */
    public PairInterface<T2,T1> reverse()
    {
        return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
    }

    /* 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.
     * 
     * Note that if you forget to implement this method, your
     * compiler will not complain since your class inherits this
     * method from the class Object.
     * 
     * @param otherObject the object to be compared to this object.
     * @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;
        }

        if (otherObject.fst.equals(this.fst) &&
            otherObject.snd.equals(this.snd))
        {
            return true;
        } else {
            return false;
        }
    }

    /* Generates a string representing this pair. Note that
     * the String representing the pair (x,y) is "(x,y)". There
     * is no whitespace unless x or y or both contain whitespace
     * themselves.
     * 
     * Note that if you forget to implement this method, your
     * compiler will not complain since your class inherits this
     * method from the class Object.
     * 
     * @return a string representing this pair.
     */
    public String toString()
    {
        return "("+first.toString()+","+second.toString()+")";
    }
}

Solution

  • public PairInterface<T2,T1> reverse()
        {
            return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
        }
    

    First of all, you can't return this

    return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
    

    Interfaces define methods that a class should offer, but do not implement these methods. Therefore, it is impossible to instantiate an interface. What can be returned however is an object that implements your PairInterface. You can do this as follows: return new Pair(this.snd(), this.fst());

    Note that we instantiate an object here with the keyword new, and that we give the arguments for the constructor between brackets, instead of placing them between < >.

    I don't quite understand yet what the rest of your question is about, but I'll update this post once I do. I won't however give away the solution, since, as pointed out in the comments on your post, it looks like you're trying to get us to do your work.