Question: Hello I am looking for some help with this toString method in this java program. It is supposed to build a string that represents a

Hello I am looking for some help with this toString method in this java program. It is supposed to build a string that represents a polynomial. For example the polynomial 4x^3 - 2x^2 + 9x - 4 is stored in an array as follows. -4 in the 0th position 9 in the first -2 in the second and 4 in the third. This is what I have so far I just dont know how to check to make sure if the signs are correct. I dont know if I am even on the right track. If the coefficient is 0 it should also be skipped over. The toString method I need help with is at the end of the program.

public class PolynomialClass { private double coeff[];

public Poly() { coeff = new double[0]; // degree -1 }

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

public double getCoeff(int n) { return (n >= 0 && n <= this.getDegree()) ? this.coeff[n] : 0.0; }

public void setCoeff(int n, double value) { if (n < 0) throw new ArithmeticException("Negative power: " + n);

if (n > this.getDegree()) { double temp[] = new double[n + 1]; for (int i = 0; i <= this.getDegree(); i++) temp[i] = this.coeff[i]; this.coeff = temp; }

coeff[n] = value; int degree = this.getDegree(); while (degree >= 0 && this.coeff[degree] == 0.0) degree--;

if (degree < this.getDegree()) { double temp[] = new double[degree + 1]; for (int i = 0; i <= degree; i++) temp[i] = this.coeff[i]; this.coeff = temp; } }

public Poly termMult(double coeff, int exp) { Poly newPoly = new Poly(); for (int i = this.getDegree(); i >= 0; i--) { double c = this.getCoeff(i) * coeff; int e = i + exp; newPoly.setCoeff(e, c); } return newPoly; }

public Poly add(Poly p2) { Poly total = new Poly(); int degree; if (this.getDegree() >= p2.getDegree()) { degree = this.getDegree(); } else { degree = p2.getDegree(); } for (int i = degree; i >= 0; i--) { total.setCoeff(i, this.getCoeff(i)+ p2.getCoeff(i)); } return total; }

public Poly negative() { Poly total = new Poly(); for (int i = this.getDegree(); i >= 0; i--) { total.setCoeff(i, this.getCoeff(i)*-1); } return total; }

public Poly sub(Poly p2) { return this.add(p2.negative()); }

public double evaluate(double x) { double total = 0; for (int i = this.getDegree(); i >= 0; i--) { total += Math.pow(x, i) * this.getCoeff(i); } return total; }

public String toString() { StringBuilder out = new StringBuilder(); for (int i = this.getDegree(); i >= 0; i--) { if (this.getCoeff(i) > 0) out.append(this.getCoeff(i) > 1? " ": " "); if (i > 1) { out.append(this.getCoeff(i)+"x^"+i+" "); } else if (i > 0) { out.append(this.getCoeff(i)+"x"); } else { out.append(this.getCoeff(i)); } } return out.toString(); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!