Question: Write a class for representing polynomials. A polynomial has the following general form anxn + an1xn1 + an2xn3 + ...a2 + x2 + a1x +

Write a class for representing polynomials. A polynomial has the following general form
anxn + an1xn1 + an2xn3 + ...a2 + x2 + a1x + a0
Any polynomial can be uniquely determined by listing its coefficients (a0, a1, ..., an). Your
class must have the following methods
evaluate(x): evaluates the polynomial with given input
change coef(n,a): changes the nth coefficient of the polynomial to be a. degree(): returns the degree of the polynomial
The constructor should take one list of coefficients. Element 0 of this list represents, coefficient a0; element 1, represents a1, etc.
In addition, your class should behave well when:
(a) It is used with the print() method
(b) Multiply a polynomial by a constant using * operator
(c) A polynomial is added to or subtracted from another polynomial using the operators + and -
Note: Printing a polynomial is not straight forward. Keep it simple first: print it in the most straightforward and easy way, even if the output does not look very good. You can go back and make it better, if you want, but only after you have finished the rest of this assignment.
Test your function with the following line of code.
poly1 = Polynomial([-3, 2])
print(poly1)
poly2 = Polynomial([3, -1, 5])
print(poly2)
poly3 = poly1 * 3
print(poly3)
poly4 = poly2 + poly1
print(poly4)
poly5 = poly4 - poly2
print(poly5)

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!