2013-09-05 2 views
0

이 길이와 인수의 크기의 합과 크기가 같은 새로운 길이를 반환하는 add (Length) 메서드를 만들어야합니다. 나는 귀하의 요구 사항에 상황에 따라 다르다ADT에서 this에 인수 추가하기

public class Length implements Comparable<Length>{ 

    private final double length; //private! Do NOT add a getter 

    // This constructor must remain private 
    private Length(double l){ 
     length = l; 
    } 
    public double add(Length l){ 
     return ; 
    } 
    public double subtract(Length l){ 

    } 
    public double scale(double d){ 

    } 
    public double divide(Length l){ 

    } 
    public double Length(Position one, Position two){ 

    } 
    // TODO: For all constants, have a line: 
    // public static final Length ... = new Length(...); 


    // Use the @Override annotation on all methods 
    // That override a superclass method. 
    @Override 
    public boolean equals(Object other){ 
     //TODO 
    } 

    @Override 
    public int hashCode(){ 
     //TODO 
    } 

    @Override 
    public String toString(){ 
     //TODO 
    } 

    // If you are overriding a method from an interface, then Java 5 
    // says you CANNOT use Override, but Java 6 says you MAY. Either is OK. 
    // @Override 
    public int compareTo(Length other) { 
     //TODO 
    } 

    // TODO Write the rest of the methods for this class, and 
    // the other two classes. 

} 
+0

'Length' 객체는 현재 'Length'와 인수로 전달 된 것과 같은 길이의 변수를 반환해야합니다. –

+0

변경할 수없는 클래스를 만드시겠습니까? –

답변

1

을 추가하는 이중 또는 길이와 방법을 반환 할 필요가 있는지 확실하지,하지만 일반적으로 새 Length 객체를 반환 할 것입니다.

public Length add(Length other){ 
    // check that other is not null 
    return new Length(this.length + other.length); 
} 

다른 모든 수학적 방법에 대해서도 비슷한 작업을 수행 할 수 있습니다.

Rohit은 자신의 의견에 진술했듯이 length 필드를 수정할 수있는 메서드가 없으므로 대신 클래스를 변경하지 못하게합니다. 대신 새 Length 개체를 반환합니다.

관련 문제