Question: Where in my class code (Java) is causing the error messages? Please help rewrite the code! import java.util.ArrayList; /** * This is the Polynomial Implementation

Where in my class code (Java) is causing the error messages? Please help rewrite the code!

Where in my class code (Java) is causing the error messages? Please

help rewrite the code! import java.util.ArrayList; /** * This is the Polynomial

import java.util.ArrayList;

/** * This is the Polynomial Implementation class that implements the Polynomial interface. */ public class PolynomialImpl implements Polynomial {

private final ArrayList terms;

/** * This is the constructor. */ public PolynomialImpl() { terms = new ArrayList(); }

/** * This method adds the resulting term to the Polynomial. * @param coeff the coefficient of the term. * @param power the power of the term (the exponent). */ @Override public void addTerm(int coeff, int power) { if (coeff != 0) { Term newTerm = new Term(coeff, power); addTerm(newTerm, 0); } }

/** * This method is the helper method for addTerm(int coeff, int power) method. * * @param newTerm new term to add * @param termNum term index in the terms list */ private void addTerm(Term newTerm, int termNum) { if (termNum >= terms.size()) { this.terms.add(termNum, newTerm); } else { if (this.terms.get(termNum).power > newTerm.power) { addTerm(newTerm, termNum + 1); } else if (this.terms.get(termNum).power == newTerm.power) { this.terms.get(termNum).coeff += newTerm.coeff; if (this.terms.get(termNum).coeff == 0) { this.terms.remove(termNum); } } else { this.terms.add(termNum, newTerm); } } }

/** * This method removes any and all terms in the Polynomial with the given power. * @param power the power of the term to remove */ @Override public void removeTerm(int power) { removeTerm(power, 0); }

/** * This method is a helper for removeTerm(int power) method. * @param power the power of the term to remove * @param termNum term index in the terms list */ private void removeTerm(int power, int termNum) { if (termNum power) { removeTerm(power, termNum + 1); } else if (this.terms.get(termNum).power == power) { this.terms.remove(termNum); } } }

/** *This method computes the degree of the Polynomial. * @return return of the degree value */ @Override public int getDegree() { if (!this.terms.isEmpty()) { return this.terms.get(0).power; } else { throw new IllegalArgumentException("A term with negative power should not work, but did. "); } }

/** * This method computes the coefficient of a term with the given power. * @param power the power of the term to find * @return coefficient */ @Override public int getCoefficient(int power) { return getCoefficient(power, 0); }

/** * This method is a helper method for getCoefficient(int power) method. * @param power the power of the term to find * @param termNum the term index in the terms list * @return the value of 0 */ private int getCoefficient(int power, int termNum) { if (termNum >= terms.size()) { return 0; } if (this.terms.get(termNum).power == power) { return this.terms.get(termNum).coeff; } else if (this.terms.get(termNum).power > power) { return getCoefficient(power, termNum + 1); } else { return 0; } }

/** * This method adds this Polynomial with the given Polynomial * and return the resulting Polynomial. * @param p the Polynomial to add to this Polynomial * @return the sum of adding two Polynomials */ @Override public Polynomial add(Polynomial p) { //Create Polynomial to hold the sum Polynomial sum = new PolynomialImpl();

//Add this Polynomial to sum add(sum, this, 0);

//Add Polynomial p to sum add(sum, p, 0);

//Return sum return sum; }

/** * This method is a helper method for the add(Polynomial p) method. * @param p1 the Polynomial to which Polynomial (p2) will be added * @param p2 the Polynomial to which Polynomial (p1) will be added * @param termNum the term index in the terms list */ private void add(Polynomial p1, Polynomial p2, int termNum) { if (termNum >= ((PolynomialImpl) p2).terms.size()) { return; } p1.addTerm(((PolynomialImpl) p2).terms.get(termNum).coeff, ((PolynomialImpl) p2).terms.get(termNum).power); add(p1, p2, termNum + 1); }

@Override public double evaluate(double x) { /*if (coeff != null) { for (int i = getCoefficient().length - 1; i >= 0; --i); value = getCofficient[i] + (x * value); }*/ return x; }

@Override public String toString() { if (terms.isEmpty()) { return "0"; } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i

//INNER CLASS static class Term {

private int coeff; private final int power;

/** * This is the Parameterized constructor. * @param coeff the coefficient of the Term * @param power this is the power of the Term */ public Term(int coeff, int power) { this.coeff = coeff; this.power = power; }

@Override public String toString() { if (this.coeff == 0) { return "0"; } else if (power == 0) { if (this.coeff Failed: testinvalidPolynomialCreation By Term(GraderPolynomialVisible Test) Message: A term with negative power should not work, but did. Failed: testEvaluate(GraderPolynomialVisible Test) Message: For f(x)=15x^26 f(1.01) should be 19.428844724511094 but is 1.01 expected: but was: Failed: testinvalidPolynomialCreation By Term(GraderPolynomialVisible Test) Message: A term with negative power should not work, but did. Failed: testEvaluate(GraderPolynomialVisible Test) Message: For f(x)=15x^26 f(1.01) should be 19.428844724511094 but is 1.01 expected: but was:

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!