尝试创建一个将两个对象配对的ADT

时间:2014-09-10 15:32:33

标签: java types adt abstract

我正在使用Java中名为 Pair 的抽象数据类型。它应该采用两个对象并在这种数据类型中将它们组合在一起。这应该不到30分钟,但我已经工作了3个半小时。我相信我有前两种方法,但我无法弄清楚反向或平等。您可以在代码中看到我尝试的内容:

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()+")";
    }
}

1 个答案:

答案 0 :(得分:0)

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

首先,您无法退回

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

接口定义了类应该提供的方法,但是实现这些方法。因此,不可能实例化接口。但是,可以返回的是实现您的PairInterface的对象。你可以这样做:     返回新的Pair(this.snd(),this.fst());

请注意,我们在这里使用关键字 new 来实例化一个对象,并且我们在括号之间给出构造函数的参数,而不是将它们放在&lt;之间。 &GT;

我还不太了解你的其他问题是什么,但是一旦我这样做,我就会更新这篇文章。然而,我不会放弃解决方案,因为正如您在帖子上的评论中指出的那样,您似乎正试图让我们开展您的工作。