简体   繁体   中英

How can I fix this “plus” method in Polynomial class using BigInteger

I appreciate the help. I was able to finish modifying everything in this class into BigInteger format except for the compose method. Can anyone help me with this last part as to why it is not working correctly? I really appreciate it, thanks.

import java.math.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] = new BigInteger("1");
    deg = 0;
}



/** Creates the linear polynomial of the 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 the polynomial P(x) = a * x^b.
  */
public Polynomial(int a, int b) {
    coef = new BigInteger[b+1];
    for(int i = 0; i < b; i++){
        if(coef[i] == null)
            coef[i] = new BigInteger("0");

    }
    coef[b] = new BigInteger(Integer.toString(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] != null) d = i; // check to make sure this works
    return d;
}

/** 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].intValue(), 0);
        c = term.plus(b.times(c));
    }
    return c;
}

  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 a textual representation of this 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;
    for (int i = deg-1; i >= 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 I think is the problem is with your toString() itself as it does not align to your defaulting mechanism. Meaning, you assign default value of '0's but do not check for 0 values in the following lines:

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

It gets piled up even for 0 coefficient values. Add a 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, I haven't tested it but you can try testing and post the results.

EDIT:

Well, i just tried your code and seems to give the correct information with the above condition in place:

6x^7 + 2x^3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM