Consider a class Polynomial, which represents polynomials of a single variable up to the fourth power (x4). The sole instance variable of a Polynomial is an array of size 5, that stores doubles. Each element of the array represents the coefficients of the Polynomial at the corresponding power. For example:
Element 0 is the coefficient of the x0 term Element 1 is the coefficient of the x1 term Element 2 is the coefficient of the x2 term Element 3 is the coefficient of the x3 term Element 4 is the coefficient of the x4 term
Thus the polynomial 3.0 – 1.0x +2.0x2 would be represented by the array:
3.0 -1.0 2.0 0.0 0.0
Write the class Polynomial that includes the following methods:
A default constructor that initializes all coefficients to zero.
A constructor that takes five doubles as its formal parameters and initializes the coefficients to the value of the corresponding parameters.
A constructor that takes an Polynomial object as its formal parameter and creates a new polynomial that has coefficients that are identical to the Polynomial object passed in.
An accessor method that takes in a parameter n and returns the nth coefficient.
A mutator method that has 2 formal parameters n and a new coefficient, and sets the nth coefficient to the new value being passed in.
A method called add that computes the sum of two polynomials and returns a new polynomial. The signature of this method should be: public Polynomial add(Polynomial p)
A method called evaluate that computes the value of the polynomial with the value passed as a parameter. The signature of this method should be: public double evaluate(double x)
A method called derivative that returns the derivative of the polynomial. The signature of this method should be: public Polynomial derivative()
A method called numberOfTerms that returns the number of non-zero terms of the polynomial.
A toString method such that the statement System.out.println(somePolynomialObject) would display the polynomial as an equation but only the non zero terms are displayed and the + sign for the first terms does not appear. For example:
if the array contains 3.0 -1.0 2.0 0.0 0.0 The output should be: 3.0 – 1.0x^1 + 2.0x^2
if the array contains 7.0 0.0 2.0 0.0 4.0 The outputshould be: 7.0 + 2.0x^2 + 4.0x^4
Write a driver program that will :
Create an array of 8 Polynomials
Prompt the user for the coefficients for the first two polynomials (store these in locations 0 and 1 of your array of Part 1 above)
Randomly generates the coefficients for the next 3 polynomials (store these in locations 2, 3 and 4). The coefficicents should be between 0.0 and 100.0.
In location 5 of your array, stores the sum of the polynomials in location 0 and 2
In location 6 of your array, stores a polynomial whose coeficients are 3 times those of the polynomial in location 1
In location 7, store the derivative of the polynomial in location 4
Prompt the user for a double value x, then print the 8 polynomials along with the number of terms in each polynomial and the value of the polynomial when evaluated with x. For example if the user entered x=3.0, the output for the polynomial
3.0 -1.0 2.0 0.0 0.0 should be :
Polynomial 3.0 – 1.0x^1 + 2.0x^2 has 3 terms and evalutes to 18.0 for x = 3.0
It is not our intention to break the school's academic policy. Projects posted are only 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 fill out the form.
Please provide a valid email address and we'll get back to you in less than 24 hours.
We will be sending an invoice through PayPal upon confirmation.
We are a non profit organization however we need an amount to keep this organization running,
and to be able to complete our research and development.