Create a class Polynomial that is used to evaluate a polynomial function of x:
!
P(x) = a0 + a1x + a2x2 +!+ an"1xn"1 + anxn . The coefficients ai are floating-point numbers,
the exponents of x are integers, and the largest exponent n—called the degree of the
polynomial—is greater than or equal to zero. The class has the attributes
• degree—the value of the largest exponent n
• coefficients—an array of the coefficients ai
and the following methods:
• Polynomial(max)—a constructor that creates a polynomial of degree max whose
coefficients are all zero
• setConstant(i, value)—sets the coefficient ai to value
• evaluate(x)—returns the value of the polynomial for the given value x
For example, the polynomial
P(x) = 3 + 5 x + 2 x3 is of degree 3 and has coefficients a0 = 3, a1 = 5, a2 = 0, and a3 = 2.
The invocation:
evaluate(7) computes
!
3+ 5 " 7 + 0 " 72 + 2 " 73 = 3+ 35 + 0 + 686 = 724 and returns
the result 724. A possible dialogue between the program and the user follows:
Constructing a polynomial:
3+ 5x + 2x^3
Evaluating the polynomial at 0 should give 3
Got: 3.0
Evaluating the polynomial at 1 should give 10
Got: 10.0
Evaluating the polynomial at 7 should give 724
Got: 724.0
Evaluating the polynomial at 1/2 should give 5.75