2011-09-21 82 views
0

我在繼承工作中遇到了一些問題。在父類中,數組係數是私有的。我有一些訪問方法,但我仍然無法使其工作。Java中的繼承問題

import java.util.ArrayList; 
public class Poly { 

private float[] coefficients; 
public static void main (String[] args){ 
    float[] fa = {3, 2, 4}; 
    Poly test = new Poly(fa); 

} 

public Poly() { 
    coefficients = new float[1]; 
    coefficients[0] = 0; 
} 

public Poly(int degree) { 
    coefficients = new float[degree+1]; 
    for (int i = 0; i <= degree; i++) 
     coefficients[i] = 0; 
} 


public Poly(float[] a) { 
    coefficients = new float[a.length]; 
    for (int i = 0; i < a.length; i++) 
     coefficients[i] = a[i]; 
} 

public int getDegree() { 
    return coefficients.length-1; 
} 

public float getCoefficient(int i) { 
    return coefficients[i]; 
} 

public void setCoefficient(int i, float value) { 
    coefficients[i] = value; 
} 

public Poly add(Poly p) { 
    int n = getDegree(); 
    int m = p.getDegree(); 
    Poly result = new Poly(Poly.max(n, m)); 
    int i; 

     for (i = 0; i <= Poly.min(n, m); i++) 
      result.setCoefficient(i, coefficients[i] + p.getCoefficient(i)); 
     if (i <= n) { 
      //we have to copy the remaining coefficients from this object 
      for (; i <= n; i++) 
       result.setCoefficient(i, coefficients[i]); 
     } else { 
      // we have to copy the remaining coefficients from p 
      for (; i <= m; i++) 
       result.setCoefficient(i, p.getCoefficient(i)); 
     } 
    return result; 
} 

public void displayPoly() { 
    for (int i=0; i < coefficients.length; i++) 
     System.out.print(" "+coefficients[i]); 
    System.out.println(); 
} 

private static int max (int n, int m) { 
    if (n > m) 
     return n; 
    return m; 
} 

private static int min (int n, int m) { 
    if (n > m) 
     return m; 
    return n; 
} 

public Poly multiplyCon (double c){ 
    int n = getDegree(); 
    Poly results = new Poly(n); 
    for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient 
     results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient 
     } 

    return results; 
    } 

    public Poly multiplyPoly (Poly p){ 
    int n = getDegree(); 
    int m = p.getDegree(); 
    Poly result = null; 
    for (int i = 0; i <= n; i++){ 
     Poly tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method 
     if (result == null){ 
      result = tmpResult; 
     } else { 
      result = result.add(tmpResult); 
     } 
    } 
    return result; 
} 
    public void leadingZero() { 
    int degree = getDegree(); 
    if (degree == 0) return; 
    if (coefficients[degree] != 0) return; 
    // find the last highest degree with non-zero cofficient 
    int highestDegree = degree; 
    for (int i = degree; i <= 0; i--) { 
     if (coefficients[i] == 0) { 
       highestDegree = i -1; 
     } else { 
       // if the value is non-zero 
       break; 
     } 
    } 
    float[] newCoefficients = new float[highestDegree + 1]; 
    for (int i=0; i<= highestDegree; i++) { 
      newCoefficients[i] = coefficients[i]; 
    } 
    coefficients = newCoefficients; 
} 

    public Poly differentiate(){ 
    int n = getDegree(); 
    Poly newResult = new Poly(n); 
    if (n>0){ //checking if it has a degree 
     for (int i = 1; i<= n; i++){ 
      newResult.coefficients[i-1]= coefficients[i] * (i); // shift degree by 1 and multiplies 
    } 
    return newResult; 

    } else { 
    return new Poly(); //empty 
    } 
    } 


    public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly 
    int oldPolyDegree = this.getDegree(); 
    int newPolyDegree = oldPolyDegree + degree; 
    Poly newResult = new Poly(newPolyDegree); 
    //set all coeff to zero 
    for (int i = 0; i<= newPolyDegree; i++){ 
     newResult.coefficients[i] = 0; 
    } 
    //shift by n degree 
    for (int j = 0; j <= oldPolyDegree; j++){ 
     newResult.coefficients[j+degree] = coefficients[j] * (float)c; 
    } 

    return newResult; 
} 
} 

任何人都可以幫助我修復從上面那個繼承的第二類嗎?我似乎無法讓我的乘法和第二課的方法正常工作。

public class QuadPoly extends Poly 
{ 
private float [] quadcoefficients; 
public QuadPoly() { 
    super(2); 

} 
public QuadPoly(int degree) { 
    super(2); 
} 
public QuadPoly(float [] f) { 
    super(f); 
    if (getDegree() > 2){ 
     throw new IllegalArgumentException ("Must be Quadratic"); 
    } 

} 
public QuadPoly(Poly p){ 
    super(p.coefficients); 
    for (int i = 0; i < coefficients.length; i++){ 
     if (coefficients[i] < 0){ 
      throw new Exception("Expecting positive coefficients!"); 
     } 
     } 
} 
// public QuadPoly(Poly p){ 
    // super(p.coefficients); 
//} 
public QuadPoly addQuad (QuadPoly p){ 
    return new QuadPoly(super.add(p)); 
} 

public QuadPoly multiplyQuadPoly (QuadPoly f){ 
    if (quadcoefficients.length > 2){ 
     throw new IllegalArgumentException ("Must be Quadratic"); 
    } 

    return new QuadPoly(super.multiplyPoly(f)); 
} 
+1

你用上面的代碼得到的錯誤是什麼? – Saket

+0

好吧,現在,當我嘗試「super(p.coefficients)」時,父類的數組係數是私有的。你可以給我一些提示,試圖讓第二課中的加法和乘法方法起作用嗎? –

+2

我在同一個24小時內在相同的作業分配中看到來自同一個OP的4個帖子有點困擾。請參閱http://stackoverflow.com/questions/7476502/multiplying-polynomial-by-constant-in-java,http://stackoverflow.com/questions/7489384/errors-in-polynomial-class,http:// stackoverflow .COM /問題/ 7476502 /乘以多項式按恆定在Java的。 –

回答

1

我會使係數protected或使用訪問器方法。

我不會拋出一個普通的檢查Exception。 IllegalArgumentException將是更好的選擇。

什麼是quadcoefficients?他們似乎沒有設置在任何地方。

+0

哎呀。這不應該在那裏。我打算爲這個類創建第二個數組,但是這種方式背離了繼承的目的。有沒有一種訪問器方法可以讓我訪問整個數組?我遇到的問題是我不確定如何讓添加和乘法方法工作。我應該儘可能地使用父類中的方法。 –

+0

我明白了,你沒有寫保利。 ;)你必須改用getter和setter方法。 –

0

你把係數private。我不會改變這一點,但我想補充一個getter方法爲Poly類:

public class Poly { 
    //somecode here 
    public float[] getCoefficients(){ 
     return this.coefficients; 
    } 
} 

然後,我會在其他代碼getter方法使用它;

public QuadPoly(Poly p){ 
    super(p.getCoefficients); 
    //some more code here 
} 

即使你做出係數protected,你想到達另一個對象,這是一個參數係數場。所以它與繼承和問題無關。

+0

如果你這樣做,他們可以簡單地改變。我會強烈考慮發送防禦性副本或不可修改的集合。讓他們原始訪問一個私有的可變變量是一個非常糟糕的決定。 – corsiKa