2014-09-10 2 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]를 읽으십시오. 서면으로하는 질문은 "나를 위해 일해주세요."와 너무 비슷합니다. –

+0

글쎄, 나는 equals로 시작할 것이다. Im은 다른 쌍이이 쌍과 같은지를 결정하는 데 사용된다고 가정하므로 개체와 쌍을 가져 가지 않는 이유는 무엇입니까? 임의의 객체를 한 쌍으로 어떻게 비교할 수 있습니까? –

+0

까지 반대로, 그 pairInterface를 반환해야하기 때문에 내가 무슨 일을해야한다고 생각하지만 컴파일러는 this.snd()와 this.fst() 세미콜론 inbetween을 요구하고있다. 왜 그런가요? –

답변

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());

키워드 new으로 개체를 인스턴스화하고, <> 사이에 배치하는 대신 대괄호로 생성자에 대한 인수를 제공한다는 점에 유의하십시오.

귀하의 질문에 대한 나머지 내용은 아직 이해가되지 않지만 일단 본인이이 게시물을 업데이트하겠습니다. 나는 그 해결책을 포기하지 않을 것입니다. 왜냐하면 당신의 게시물에 대한 의견에서 지적했듯이, 우리가 당신의 일을하도록하려는 것처럼 보입니다.

+0

이봐, 나는 새로운 키워드를 추가하기 전에 쌍을 시도해 보았을 때 반대라고 생각했습니다. 도움을 주셔서 감사합니다. 저는 이것을 배우는 것에 정말로 관심이 있었지만 교과서 어디에도 없었습니다. –

+0

그 반대의 경우 두 개의 "쌍"을 가져 와서 비교해야합니다. 세 번째 if 문은 내가 추가 한 문입니다. 객체를 가져 오는 경우에도 객체가 다른 쌍과 동일해야 객체가 쌍이어야한다고 가정하므로 .fst 및 .snd 메서드를 사용하여 this.fst와 (.equals 메서드를 통해) 비교합니다. 이 .nd. 이 정확하거나 완전히 벗어난 근거에 가깝습니까? –

+0

더 자세히 보시려면 ... otherObject에 페어를 만들기 위해 뭔가를해야합니까? "이 쌍이 aPair와 같으면 true를 반환하지만 –

관련 문제