A polynomial is made of several terms, each term having a coefficient and a variable raised to a power. Polynomials are one-variable if all their terms contain only one variable. An example of such a polynomial is f(x) = 3x4 - 5x3 + 2x - 4. This polynomial has four terms.

The degree of a polynomial is defined as the highest power of the variable in a term. In the above example, the degree of the polynomial is 4. The polynomials we deal with have only positive, integral powers. The coefficients of our polynomials are also integral numbers.

Two polynomials are the same if they contain the same terms.

Several algebraic operations are possible with polynomials. The simplest one is evaluating the polynomial for a specific value of the variable. For example, for a polynomial
f(x)=3x4 - 5x3 + 2x - 4, its value at x = 2.5 is defined as
f(2.5) = 3 * (2.5)4 - 5 * (2.5)3 + 2 + (2.5) - 4 which is 40.0625.

Polynomials can be added together to create a new polynomial. The addition is performed by combining all the terms and adding the coefficients of the terms with the same power. For example 3x4 - 5x3 + 2x -4 + 2x3 + 2x2 + 4 = 3x4 - 3x3 + 2x2 + 2x. The degree of the sum is the maximum of the degrees of the two polynomials.

Download Polynomial.java Package: polynomial Start by the provided Polynomial interface and implement it in a class called PolynomialImpl. Beyond implementing the Polynomial interface, your implementation should have the following features/obey these constraints:

  • You are not allowed to use any of Java's existing list implementations, interfaces, arrays, or otherwise any collection classes or maps to implement polynomials.
  • You are required to represent your polynomial as a recursive union.
  • A polynomial should only store terms with non-zero coefficients.
  • This class should have a constructor with no parameters that create a polynomial with no terms, i.e., the polynomial 0.
  • This class should have another constructor that takes a polynomial as a string, parses it, and creates the polynomial accordingly. This string contains the polynomial, with each term separates by a space. The following examples should work with your constructor:
    • "4x^3 +3x^1 -5"
    • "-3x^4 -2x^5 -5 +11x^1"
    • "102"
    • "+3x^4 -2x^5 -5 -2x^4 +11x^1"
  • For this implementation, any term with a negative power is invalid.
  • This class must provide a way to determine if two polynomials are the same.
  • For the add method, if the current polynomial and the parameter are not implemented as the same concrete type, your solution should throw an IllegalArgumentException.
  • You are not allowed to implement the add method using a mix of string manipulation and the constructors below.
  • This class should include a toString method that returns a string representation of the polynomial. The following examples should help you infer the required format:
    • creates the string "5x^2 +4x^1 -2"
    • creates the string "-50x^3 +1x^2 +3"
    • creates the string "2x^5 -3x^2 +4x^1 -10"
    • The empty polynomial should create the string 0
    • Hint: You may find the Scanner class helpful to do this.
  • Testing Whenever you write a class, you should also write tests for that class that proves not only that your code CAN work but that it WILL ALWAYS work. Additionally, your tests should be sufficient to convince someone else that your code works correctly.

Polynomial.java starter code

package polynomial;

/**
* This interface represents all the operations offered by a polynomial. A
* polynomial is defined here as a function of one variable. The polynomial is a
* weighted sum of terms (the weights are numeric). Each term may either be an
* integer power of that variable, or some function in that variable, but never
* both (i.e. (log x)^2 is not allowed).
*/

public interface Polynomial {
/**
* Add this polynomial to another and return the result as another polynomial.
*
* @param other the other polynomial to be added
* @return the resulting polynomial
* @throws IllegalArgumentException if parameter is not the same concrete type
* as the current object.
*/
Polynomial add(Polynomial other) throws IllegalArgumentException;

/**
* Add a term to this polynomial with the specified coefficient and power.
*
* @param coefficient the coefficient of the term to be added
* @param power the power of the term to be added
* @throws IllegalArgumentException if the power is negative
*/
void addTerm(int coefficient, int power) throws IllegalArgumentException;

/**
* Determines if this polynomial is the same as the parameter polynomial.
*
* @param poly the polynomial to use
* @return true if this polynomial is of the same concrete type and has the same
* terms as the parameter, false otherwise
*/
boolean isSame(Polynomial poly);

/**
* Evaluate the value of this polynomial at the given value of the variable.
*
* @param x the value at which the polynomial is to be evaluated.
* @return the value of the polynomial at x
*/
double evaluate(double x);

/**
* Return the coefficient of the term with the given power.
*
* @param power the power whose coefficient is sought
* @return the coefficient at the given power
*/
int getCoefficient(int power);

/**
* Get the degree of this polynomial.
*
* @return the degree of this polynomial as a whole number
*/
int getDegree();
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.