A polynomial of a single variablex with integer coefficients is an expression of the form
p(x) = c0+c1 x+c2 x+ ...+cn xn, where c,i = 0, 1, ...,n, are integers.
Create a class for polynomials up to thenth degree. A specification of the methods for this class is provided next:
■ public Polynomial(int maxDegree)
Constructs a new polynomial of degree maxDegree with all of the coefficients set to zero
public Polynomial(int[] coef)
Constructs a new polynomial with the corresponding coefficients passed in thecoef array, with the highest degree ascoef[0] and the constant term incoef [coef. length-1 ]. So the array (3,2, 1) creates the polynomial 3x2 + 2x + 1.
a public int getCoefficient(int power)
Returns an integer representing the coefficient of thexf, on’er termm public void setCoefficient(int coef, int power)
Sets the coefficient of thex^om i term to coef
■ public String toString()
Returns thestring representation of the polynomial. For example,3x2+2x+7 would be returned as 3*x^2 +2 * x + 1 or, more simply, 3*x^2 + 2x +
1. Any term whose coefficient is zero should not appear in the string unless the polynomial has only a single constant term of zero.
■ public double evaluate(double x)
Evaluates the polynomial for the value x and returns the resultp(x)
■ public static Polynomial derivative(Polynomial p)
Returns aPolynomial representing the derivative of the polynomial p
■ public double bisection(double a, double b)
throws java.lang.IllegalArgumentException
Returns adouble representing the root of thePolynomial using the Bisection Method. The bisection method requires two initial pointsa andb such thatp (x) andp(x) have opposite signs; if they do not, then java, lang. IllegalArgumentException should be thrown. Though there could be multiple roots between a and b, the method will return the first root it finds such that evaluating the polynomial atpfrootj<o.000001.
One method that can be used to easily evaluate a polynomial is based upon Horner’s rule. Note that the fourth degree polynomial2x4+ +x3-4x2 + 3x + 5 can be evaluated as
result = (((((((2 * x) + 1) * x) - 4) * x) + 3) * x) + 5
or, in more general terms
result = (((((((c4 * x) + c3) * x) + c2) * x) + cx) * x) + c0
To find the derivative of a polynomial, you simply find the derivative of each term in the polynomial. The derivative of a termCjX1 is(¡*c‘)x~i l. So for example, the derivative of
p(x) = 4x~ + 3x + 1 isp'(x) = 8x +3
To test thePolynomial class, create a second class calledTestPolynomial. This class should support the following interaction with the user (user input is shaded):
Please enter the polynomial you wish to work with. You will be prompted to enter the coefficient for each term in the polynomial. You may enter zero if the term is absent from the polynomial.
What degree polynomial would you like to create? 4
Please enter the coefficients:
Coefficient for x^4: 2
Coefficient for x^3: 1
Coefficient for x^2: -4
Coefficient for x^ l: 3
Coefficient for x^0: 5
p(x) = 2x^4 + x^3 - x^2 + 3x + 5
What would you like to do with this polynomial?
(E/e) Evaluate it for a particular value of x
(D/d) Get the derivative of the polynomial
(R/r) Find the root of the polynomial
Enter option: e
For what value of x would you like to evaluate the polynomial? 2.5
p(2.5) = 81.25
Would you like to select another option? (y/n) y
What would you like to do with this polynomial?
(E/e) Evaluate it for a particular value of x
(D/d) Get the derivative of the polynomial
(R/r) Find the root of the polynomial
Enter option: d
p'(x) = 8 x^3 + 3 x^2 - 8 x + 3
Would you like to select another option? (y/n) n
Would you like to enter another polynomial? (y/n) n
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.