2014-02-12 36 views
-1

所以我已經在Int中完成了整個類,現在我不得不將它轉換爲BigInteger。主要目標是我可以將係數存儲爲BigIntegers以獲得較大的係數。我得到一個空指針錯誤的代碼,但我知道BigInteger是不可變的,需要這種格式。只是也許另一隻眼睛,或者我只是沒有正確地做到這一點。將整個程序從Int轉換爲BigInteger

public class Polynomial { 

private BigInteger[] coef; // coefficients 

private int deg;  // degree of polynomial (0 for the zero polynomial) 



    /** Creates the constant polynomial P(x) = 1. 
     */ 
    public Polynomial(){ 
     coef = new BigInteger[1]; 
     coef[0] = BigInteger.valueOf(1); 
     deg = 0; 
    } 



    /** Creates the linear polynomial of the form P(x) = x + a. 
     */ 
    public Polynomial(int a){ 
     coef = new BigInteger[2]; 
     coef[1] = BigInteger.valueOf(1); 
     coef[0] = BigInteger.valueOf(a); 
     deg = 1; 
    } 




    /** Creates the polynomial P(x) = a * x^b. 
     */ 
    public Polynomial(int a, int b) { 
     coef = new BigInteger[b+1]; 
     coef[b] = BigInteger.valueOf(a); 
     deg = degree(); 
    } 
    public Polynomial(BigInteger a, int b) { 
     coef = new BigInteger[b+1]; 
     coef[b] = a; 
     deg = degree(); 
    } 






    /** Return the degree of this polynomial (0 for the constant polynomial). 
     */ 
    public int degree() { 
     int d = 0; 
     for (int i = 0; i < coef.length; i++) 
      if (coef[i] != BigInteger.valueOf(0)) d = i; 
     return d; 
    } 





    /** Return the sum of this polynomial and b, i.e., return c = this + b. 
     */ 
    public Polynomial plus(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg)); 
     for (int i = 0; i <= a.deg; i++) c.coef[i] = c.coef[i].add(a.coef[i]); 
     for (int i = 0; i <= b.deg; i++) c.coef[i] = c.coef[i].add(b.coef[i]); 

     c.deg = c.degree(); 
     return c; 
    } 






    /** Return the difference of this polynomial and b, i.e., return (this - b). 
     */ 
    public Polynomial minus(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg)); 
     for (int i = 0; i <= a.deg; i++) c.coef[i] = c.coef[i].add(a.coef[i]); 
     for (int i = 0; i <= b.deg; i++) c.coef[i] = c.coef[i].subtract(b.coef[i]); 

     c.deg = c.degree(); 
     return c; 
    } 






    /** Return the product of this polynomial and b, i.e., return (this * b). 
     */ 
    public Polynomial times(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, a.deg + b.deg); 
     for (int i = 0; i <= a.deg; i++) 
      for (int j = 0; j <= b.deg; j++) 
       c.coef[i+j] = c.coef[i+j].add(a.coef[i].multiply(b.coef[j])); 
     c.deg = c.degree(); 
     return c; 
    } 






    /** Return the composite of this polynomial and b, i.e., return this(b(x)) - compute using Horner's method. 
     */ 
    public Polynomial compose(Polynomial b) { 
     Polynomial a = this; 
     Polynomial c = new Polynomial(0, 0); 
     for (int i = a.deg; i >= 0; i--) { 
      Polynomial term = new Polynomial(a.coef[i], 0); 
      c = term.plus(b.times(c)); 
     } 
     return c; 
    } 




    /** Return true whenever this polynomial and b are identical to one another. 
     */ 
    public boolean equals(Polynomial b) { 
     Polynomial a = this; 
     if (a.deg != b.deg) return false; 
     for (int i = a.deg; i >= 0; i--) 
      if (a.coef[i] != b.coef[i]) return false; 
     return true; 
    } 





    /** Evaluate this polynomial at x, i.e., return this(x). 
     */ 
    public int evaluate(int x) { 
     int p = 0; 
     for (int i = deg; i >= 0; i--){ 
      coef[i] = coef[i].add(BigInteger.valueOf(x * p)); 
     p = coef[i].intValue(); 
    } 
     return p; 
    } 






    /** Return the derivative of this polynomial. 
     */ 
    public Polynomial differentiate() { 
     if (deg == 0) return new Polynomial(0, 0); 
     Polynomial deriv = new Polynomial(0, deg - 1); 
     deriv.deg = deg - 1; 
     for (int i = 0; i < deg; i++) 
      deriv.coef[i] = coef[i + 1].multiply(BigInteger.valueOf(i+1));   
    return deriv; 
    } 





    /** Return a textual representationof this polynomial. 
     */ 
    public String toString() { 
     if (deg == 0) return "" + coef[0]; 
     if (deg == 1) return String.valueOf(coef[1]) + "x + " + String.valueOf(coef[0]); 
     String s = String.valueOf(coef[deg]) + "x^" + deg; 
     for (int i = deg-1; i > 0; i--) { 
      if  (coef[i].intValue() == 0) continue; 
      else if (coef[i].intValue() > 0) s = s + " + " + (coef[i].intValue()); 
      else if (coef[i].intValue() < 0) s = s + " - " + (-coef[i].intValue()); 
      if  (i == 1) s = s + "x"; 
      else if (i > 1) s = s + "x^" + i; 
     } 
     return s; 
    } 






    public static void main(String[] args) { 
     Polynomial zero = new Polynomial(1, 0); 

     Polynomial p1 = new Polynomial(4, 3); 
     Polynomial p2 = new Polynomial(3, 2); 
     Polynomial p3 = new Polynomial(-1, 0); 
     Polynomial p4 = new Polynomial(-2, 1); 
     Polynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 - 2x - 1 

     Polynomial q1 = new Polynomial(3, 2); 
     Polynomial q2 = new Polynomial(5, 0); 
     Polynomial q = q1.minus(q2);      // 3x^2 - 5 


     Polynomial r = p.plus(q); 
     Polynomial s = p.times(q); 
     Polynomial t = p.compose(q); 

     System.out.println("zero(x) =  " + zero); 
     System.out.println("p(x) =  " + p); 
     System.out.println("q(x) =  " + q); 
     System.out.println("p(x) + q(x) = " + r); 
     System.out.println("p(x) * q(x) = " + s); 
     System.out.println("p(q(x))  = " + t); 
     System.out.println("0 - p(x) = " + zero.minus(p)); 
     System.out.println("p(3)  = " + p.evaluate(3)); 
     System.out.println("p'(x)  = " + p.differentiate()); 
     System.out.println("p''(x)  = " + p.differentiate().differentiate()); 


     Polynomial poly = new Polynomial(); 

     for(int k=0; k<=4; k++){ 
      poly = poly.times(new Polynomial(-k)); 
     } 

     System.out.println(poly); 
    } 

} 
+0

在哪一行是空指針異常? –

+0

異常線程 「main」 顯示java.lang.NullPointerException \t在Polynomial.plus(polynomial.java:67) \t在Polynomial.main(polynomial.java:201) – JohnV

+0

我假設,如果它是有錯則在其他增變類中也是錯誤的。 – JohnV

回答

2

所以,當你初始化的BigInteger陣列,因爲你已經指定對象的數組(如果它是int[]則初始值爲0)的值null

你可以從你的構造看:

public Polynomial(int a, int b) { 
    coef = new BigInteger[b+1]; 
    coef[b] = BigInteger.valueOf(a); 
    deg = degree(); 
} 

你只分配coef[b],其他值保持null

因此在方法plus(Polynomial b)循環的第一次迭代,c.coef[0]爲null,因此NullPointerException當你的循環試圖調用c.coef[0].add(a.coef[0])

建議:定義一種方法,將數組中的所有BigInteger值初始化爲0,以與int[]的聲明一致並在您的構造函數中調用。例如:

private static void initializeBigIntegerArray(BigInteger[] bigIntegers) { 
    for (int i=0; i<bigIntegers.length; i++) { 
     // So you don't overwrite anything you assign explicitly 
     if (bigInteger[i] == null) { 
     bigIntegers[i] = BigInteger.ZERO; 
     } 
    } 
} 
+0

我如何將它與null比較?我不認爲這會起作用 – JohnV

+1

@JohnV你可以檢查一個值是否爲null。嘗試一下,看看自己。你在代碼中得到'NullPointerException'的原因是你試圖從一個'null'對象中調用一個方法,即對象沒有被實例化。在'initalizeBigIntegerArray'方法中,我沒有這樣做。 – xlm

+0

謝謝XLM,併爲我的愚蠢感到抱歉。我和你不一樣,以前從來不需要處理bigint。你幫了很多!我很感激。 – JohnV

1

回想一下,在Java對象的陣列實際上是引用對象陣列。所以你需要爲每個數組元素創建一個的BigInteger對象。您不分配的條目不是0,它們是空的。

1

因此,在plus方法中,您創建了這個多項式c,其背景數組包含一個零和幾個空值。然後,您繼續嘗試操作該多項式中的所有係數,包括所有這些空值。所以你調用了一個對象尚未創建的變量的方法,這就是你的空指針問題。

創建每個多項式時,請確保已爲支持陣列中的每個條目創建BigInteger