java - How can I fix this "plus" method in Polynomial class using BigInteger -


i appreciate help. able finish modifying in class biginteger format except compose method. can me last part why not working correctly? appreciate it, thanks.

import java.math.biginteger;  public class polynomial { private biginteger[] coef;  // coefficients private int deg;     // degree of polynomial (0 0 polynomial)    /** creates constant polynomial p(x) = 1.   */ public polynomial(){     coef = new biginteger[1];     coef[0] = new biginteger("1");     deg = 0; }    /** creates linear polynomial of form p(x) =  x + a.   */ public polynomial(int a){     coef = new biginteger[2];     coef[1] = new biginteger("1");     coef[0] = new biginteger(integer.tostring(a));     deg = 1; }  /** creates polynomial p(x) = * x^b.   */ public polynomial(int a, int b) {     coef = new biginteger[b+1];     for(int = 0; < b; i++){         if(coef[i] == null)             coef[i] = new biginteger("0");      }     coef[b] = new biginteger(integer.tostring(a));     deg = degree(); }   /** return degree of polynomial (0 constant polynomial).   */ public int degree() {     int d = 0;     (int = 0; < coef.length; i++)         if (coef[i] != null) d = i; // check make sure works     return d; }  /** return composite of polynomial , b, i.e., return this(b(x))  - compute using horner's method.   */ public polynomial compose(polynomial b) {     polynomial = this;     polynomial c = new polynomial(0, 0);     (int = a.deg; >= 0; i--) {         polynomial term = new polynomial(a.coef[i].intvalue(), 0);         c = term.plus(b.times(c));     }     return c; }    public polynomial times(polynomial b) {     polynomial = this;     polynomial c = new polynomial(0, a.deg + b.deg);     (int = 0; <= a.deg; i++)         (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 textual representation of polynomial.   */ public string tostring() {     if (deg ==  0) return "" + coef[0];     if (deg ==  1) return coef[1] + "x + " + coef[0];     string s = coef[deg] + "x^" + deg;     (int = deg-1; >= 0; i--) {         if      (coef[i] == null) continue;         else if (coef[i].intvalue()  > 0) s = s + " + " + ( coef[i]);         else if (coef[i].intvalue()  < 0) s = s + " - " + (coef[i].negate());         if(coef[i].intvalue() != 0)             if      (i == 1) s = s + "x";         else if (i >  1) s = s + "x^" + i;     }     return s; }  public static void main(string[] args) {    polynomial p = new polynomial(1,2);    polynomial q = new polynomial(2,3);     polynomial t    = p.compose(q); // incorrect    system.out.println("p(q(x))     = " + t);  // incorrect     }  } 

what think problem tostring() not align defaulting mechanism. meaning, assign default value of '0's not check 0 values in following lines:

if      (i == 1) s = s + "x"; else if (i >  1) s = s + "x^" + i; 

it gets piled 0 coefficient values. add condition of checking non-zero coefficient only:

if (coef[i].intvalue() != 0)  if      (i == 1) s = s + "x";  else if (i >  1) s = s + "x^" + i; 

this should work, haven't tested can try testing , post results.

edit:

well, tried code , seems give correct information above condition in place:

6x^7 + 2x^3 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -