Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions