2012-08-11 3 views
0

모의 고사 질문에 붙어 있습니다. Power이라는 클래스를 만들었습니다.이 클래스는 숫자를 임의의 값으로 올릴 수있었습니다.이 하위 클래스가 주 클래스와 상호 작용하는 방법

질문의 세 번째 부분은 BoundedPowerPower으로 확장되는 또 다른 클래스를 만들 것을 요청합니다. MAX-X 변수를 받았고 (x는이 값을 초과 할 수 없음) BoundedPower 클래스는 다음을 수행해야한다고 말했습니다. Power 클래스처럼 동작하고 생성자를 사용하고 powN 메서드를 사용합니다.

내 코드가 아래와 같으므로 BoundedPower 클래스 작업을 수행하기 위해해야 ​​할 일이 확실하지 않습니다.

public class Power { 

    private double x = 0; 

    Power(double x) { 
     this.x = x; 
    } 

    public double getX() { 
     return x; 
    } 

    public double powN(int n) { 

     double result = 1.0; 
     for (int i = 0; i < n; i++) { 
      result = result * x; 
     } 
     return result; 

    } 

    public static void main(String[] args) { 

     Power p = new Power(5.0); 
     double d = p.powN(3); 
     System.out.println(d); 
    } 

} 


public class BoundedPower extends Power { 

    public static final double MAX_X = 1000000; 

    // invariant: x <= MAX_X 

    Power x; 

    BoundedPower(double x) { 
     super(x); 
     // this.x=x; x is private in Power class 

    } 

    public double powN(int n) { 

     if (x.getX() > MAX_X) { 

      return 0; 
     } else { 

      double result = 1.0; 
      for (int i = 0; i < n; i++) { 
       result = result * getX(); 
      } 
      return result; 
     } 
    } 

    public static void main(String[] args) { 

     BoundedPower bp = new BoundedPower(5); 
     double test = bp.powN(4); 
     System.out.println(test); 
    } 

} 

답변

1
public class BoundedPower extends Power { 

    public static final double MAX_X = 1000000; 

    BoundedPower(double x) { 
    super(x); 
    } 

    public double powN(int n) { 

    if (x.getX() > MAX_X) { 
     return 0; 
    } else { 
     return super.powN(n); 
    } 
    } 

    public static void main(String[] args) { 

    BoundedPower bp = new BoundedPower(5); 
    double test = bp.powN(4); 
    System.out.println(test); 
} 
} 

당신은 당신의 계산을 복사 할 필요가 없습니다 formular를 서브 클래스에 연결합니다 (그냥 super.powN(..)). BoundedPower 내에 Power의 다른 인스턴스가 필요하지 않습니다.

+0

그게 훨씬 쉽습니다, 나는 다음 번에 그 점을 염두에 두겠습니다. 감사 – nsc010

3

클래스의 Power 변수 x가 필요하지 않습니다. 어떤 BoundedPower 인스턴스는 Power 인스턴스이므로 Power에서 메서드를 참조하려면 super.blah()를 수행하고 x.getX()에 대해서는 super.getX()를 수행합니다.

또한 사용자 의견에서, 당신은 this.x = x가 private이기 때문에 실패했다고 말했다. 당신은 슈퍼 전화를 할 때, 거기에 X를 설정하는 슈퍼 클래스 (전원)의 생성자를 호출, 그래서 this.x 필요가 없다 = X

+0

그래 지금 작동 구축 할 의미가 있는지 여부를 확인, 난 슈퍼 호출을 사용하는 것을 잊었다. 생성자의 슈퍼 호출도 의미가 있습니다. 정리해 주셔서 감사합니다 – nsc010

1

이것은 그들이 생각했던 것을 아마 : 나는 다른 방식으로 그것을 할 것

public class Power { 
public double powN(double x, int n) { 
    double result = 1.0; 
    for (int i = 0; i < n; i++) { 
     result = result * x; 
    } 
    return result; 
    } 
} 

public class BoundedPower extends Power { 
    private final double maxX; 

    public BoundedPower(double maxX) { 
    this.maxX = maxX; 
    } 

    public double powN(double x, int n) { 
    if (x > maxX) { 
     throw new IllegalArgumentException("x value [" + x + "] " + 
     "greater than expected max [" + maxX + "]"); 
    } 
    return super.powN(x, n); 
    } 
} 
1

. BoundedPower 클래스는 바운드 된 x (최대 MAX_X)에서만 의미가 있습니다.

따라서, 나는 (즉 BoundedPower 객체가 무제한의 X 년대 존재하지 수)

하여 전원을 구현하는 방법을 제외하고 그래서 구현이 정확히 것 MAX_X보다 큰 X와 객체의 생성을 허용하지 않을 당신이 BoundedPower 인스턴스를 구축 : 먼저는 super.getX()이고, 그것이

public class BoundedPower extends Power { 

     private static final double MAX_X = 1000000; //makes no sense to be public 

     public static BoundedPower newBoundedPower(int n) 
            throws IllegalNumberException{ 

       if(x > MAX_X) throw new IllegalNumberException(); 
       return new BoundedPower(x); 
     } 

     private BoundedPower(double x) { 
       super(x); 
     } 
    } 
관련 문제