2014-09-10 68 views
0

我正在研究Java中的抽象數據類型。它應該有兩個對象,並將它們組合在一起。這應該不到30分鐘,但我一直在這裏工作了3個半小時。我相信我有正確的前兩種方法,但我無法弄清楚逆向或平等。你可以看到我試圖在代碼是什麼:試圖創建一個將兩個對象配對的ADT

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

你是什麼意思由_「無法找出相反或等於」_?請詳細解釋。另外,請閱讀[FAQ]和[Ask]。你寫的問題聽起來很像「請爲我做的工作」。 – 2014-09-10 15:42:08

+0

那麼,我會從平等開始。我假設它用於確定另一對是否等於這一對,那麼爲什麼它需要一個對象而不是一對?我怎樣才能比較隨機對象與一對? – 2014-09-10 15:44:11

+0

只要相反,它應該返回一個pairInterface,所以我認爲我應該工作,但編譯器要求在this.snd()和this.fst()之間插入一個分號。這是爲什麼? – 2014-09-10 15:48:31

回答

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。你可以這樣做,如下所示: return new pair(this.snd(),this.fst());

請注意,我們在此處使用關鍵字實例化一個對象,並且我們給出括號內構造函數的參數,而不是將它們放置在<>之間。

我不太明白你的問題的其餘部分是什麼,但我會一旦更新這個帖子。然而,我不會放棄這個解決方案,因爲正如你在帖子上的評論中指出的那樣,你看起來像是在試圖讓我們去做你的工作。

+0

嘿,我想我找到了相反的,我曾嘗試過對,然後我只是不想添加關鍵字新。謝謝你的幫助順便說一句,我真的很感興趣,學習這一點,但從教科書越來越沒有。 – 2014-09-10 18:15:55

+0

爲了扭轉它應該採取兩個「對」並比較它們。第三個if語句是我添加的語句。即使它需要一個對象,我假設該對象必須是一對Pair才能與另一對相同,所以我使用.fst和.snd方法將它們(通過.equals方法)與this.fst進行比較和這個。我接近有這個正確或完全偏離基地? – 2014-09-10 18:19:53

+0

看着它更多...我是否應該爲其他對象做些什麼來使它成爲一對?我假設這是什麼意思「@返回true,如果這一對等於aPair」,但我不知道 – 2014-09-10 18:23:44

相關問題