【问题标题】:Trying to create an ADT that pairs two objects [duplicate]试图创建一个配对两个对象的 ADT [重复]
【发布时间】:2014-11-04 08:10:44
【问题描述】:

我正在研究一种在 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()+")";
    }
}

【问题讨论】:

  • 你说的“无法计算反向或等于”是什么意思?请更详细地解释。另外,请阅读FAQHow to Ask。您的书面问题听起来太像“请为我做这项工作”。
  • 好吧,我先从equals开始。我假设它用于确定另一对是否等于这对,那么为什么它需要一个对象而不是一对?如何将随机对象与一对对象进行比较?
  • 至于反向,它应该返回一个pairInterface,所以我认为我应该工作但编译器要求在this.snd()和this.fst()之间使用分号。这是为什么呢?
  • 对于反向,看起来你的导师正试图在 javadoc 中给你一个重要的线索。
  • @GenericJon 谢谢线索是新的对吗?甚至没有意识到他在谈论新的关键字,这让我一头雾水。

标签: java types adt abstract


【解决方案1】:
public PairInterface<T2,T1> reverse()
    {
        return PairInterface<this.snd(),this.fst()>;//Pair<second;first>
    }

首先,这个不能退货

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

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

请注意,我们在此处使用关键字 new 实例化一个对象,并且我们将构造函数的参数放在括号之间,而不是放在 之间。

我还不太明白你剩下的问题是关于什么的,但一旦我明白了,我会更新这篇文章。但是,我不会放弃解决方案,因为正如您帖子中的 cmets 所指出的,您似乎正试图让我们完成您的工作。

【讨论】:

  • 嘿,我想我在没想到要添加关键字 new 之前已经找到了相反的方法。谢谢你的帮助顺便说一句我真的很想学习这个,但是从教科书上却一无所获。
  • 对于反向它应该采用两个“对”并比较它们。第三个 if 语句是我添加的。即使它接受一个对象,我假设该对象必须是 Pair 才能等于另一对,所以我使用 .fst 和 .snd 方法将它们(通过 .equals 方法)与 this.fst 进行比较和this.snd。我是否接近正确或完全偏离基础?
  • 查看更多...我是否应该对 otherObject 做一些事情以使其成为一对?我假设这就是“@return true if this pair is equal to aPair”的意思,但我不确定
  • 瞧,你解决了!在确定对象是一对之后,你必须“把它变成一对”。您可以通过将其转换为这样的 Pair 来做到这一点: ( (Pair) otherObject )
  • 我试过了,但它不起作用:'Pair aPair = new (Pair)otherObject;'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 2020-06-07
相关资源
最近更新 更多