Java 尝试创建两个对象配对的ADT

Java 尝试创建两个对象配对的ADT,java,types,adt,abstract,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

我正在研究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()+")";
    }
}
公共类对实现PairInterface
{
//要做的事情:您的实例变量在这里。
公共T1优先;
公众T2秒;
公共对(第一个T1,第二个T2)
{
第一个=第一个;
秒=一秒;
}
/*获取此对的第一个元素。
*@返回此对的第一个元素。
*/
公共T1 fst()
{
先把这个还给我;
}
/*获取此对的第二个元素。
*@返回此对中的第二个元素。
*/
公共T2 snd()
{
把这个还给我;
}
/*获取表示相反顺序的新对
*这一对的。例如
*对(x,y)是(y,x)。
*@按相反顺序返回新的一对
*/
公共对接口反向()
{
返回pairnterface;//对
}
/*检查两对是否相等。请注意
*(a,b)等于对(x,y)当且仅当a为
*等于x,b等于y。
* 
*请注意,如果忘记实现此方法,则
*编译器不会抱怨,因为您的类继承了此
*方法从类对象中删除。
* 
*@param otherObject要与此对象进行比较的对象。
*@如果此对等于aPair,则返回true。否则
*返回false。
*/
公共布尔等于(对象其他对象)
{
if(otherObject==null)
{
返回false;
}
如果(getClass()!=otherObject.getClass())
{
返回false;
}
if(otherObject.fst.equals)(this.fst)&&
otherObject.snd.equals(this.snd))
{
返回true;
}否则{
返回false;
}
}
/*生成表示此对的字符串。请注意
*表示这对(x,y)的字符串是“(x,y)”。在这里
*除非x或y或两者都包含空格,否则不包含空格
*他们自己。
* 
*请注意,如果忘记实现此方法,则
*编译器不会抱怨,因为您的类继承了此
*方法从类对象中删除。
* 
*@返回表示此对的字符串。
*/
公共字符串toString()
{
返回“(“+first.toString()+”,“+second.toString()+”)”;
}
}
公共配对接口反向()
{
返回pairnterface;//对
}
首先,你不能退回这个

return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
返回PairInterface//一对
接口定义类应该提供的方法,但不实现这些方法。因此,不可能实例化接口。但是,可以返回的是一个实现PairInterface的对象。您可以按如下方式执行此操作: 返回新的对(this.snd(),this.fst())

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


我还不太明白你剩下的问题是关于什么的,但一旦我明白了,我会更新这篇文章。不过,我不会给出解决方案,因为正如你在帖子上的评论所指出的那样,看起来你是在试图让我们来做你的工作。

你说的“无法计算反向或相等”是什么意思?请详细解释。另外,阅读和。你写的问题听起来太像“请为我做这项工作”。好吧,我从平等开始。我假设它用于确定另一对是否等于这一对,那么为什么它接受一个对象而不是一对?如何将一个随机对象与一对对象进行比较?相反,它应该返回一个pairInterface,所以我认为我的方法应该有效,但编译器要求在这个.snd()和这个.fst()之间使用分号。为什么?反过来说,你的指导老师似乎想在javadoc中给你一个大的线索。@GenericJon谢谢,这个线索是新的,对吗?我甚至没有意识到他说的是“新”这个关键词,这让我很困惑。嘿,我想我找到了相反的答案,我之前试过配对,但我没有想到添加“新”这个关键词。谢谢你的帮助顺便说一句,我真的很有兴趣学习这个,但从课本上什么也学不到。相反,它应该取两个“对”并比较它们。第三个if语句是我添加的语句。即使它接收一个对象,我也假设该对象必须是一对才能与另一对相等,所以我使用.fst和.snd方法(通过.equals方法)将它们与this.fst和this.snd进行比较。我是接近正确还是完全偏离基准了?看得更多。。。我是不是应该对另一个对象做点什么,让它变成一对?我假设这就是“@return true if this pair等于aPair”的意思,但我不确定,你解决了它!在确定对象是一对之后,您必须“使其成为一对”。您可以通过如下方式将其强制转换为一对:((Pair)otherObject)我尝试了这个方法,但它不起作用:'Pair aPair=new(Pair)otherObject;'
return PairInterface<this.snd(),this.fst()>;//Pair<second;first>