How to manipulate Polynomials

A polynomial of degree n has the form
a0 + a1x + a2x2 + … + anxn
where a0, a1, …, an are numeric constants called the coefficients of the polynomial and an ? 0. For example,
1 + 3x – 7x3 + 5x4
is a polynomial of degree 4 with integer coefficients.

Write and test a polynomial class called MyPolynomial. Your class will represent a polynomial as a linked list of terms and provide constructors and methods as described below.

MyPolynomial class

The class will encapsulate a linked list for storing the terms and a linked list iterator for use by successive calls to addTerm.

Constructor

A no-argument constructor will suffice.

Methods

The following public methods are required:

? addTerm(Term t) will add a term (see below) following those already stored. The term will be added in ascending order according to the exponent.
For example, if the following statements are executed in sequence, the polynomial produced will look like: 1 + -3x^2 + 2x^3
poly.addTerm(aTerm); // Given aTerm has been declared as – Term aTerm = new Term(1,0);
poly.addTerm(new Term(-3,2));
poly.addTerm(new Term(2,3));
? evaluate(int n) will return the value of the polynomial for a given integer value n.
For the polynomial above, executing poly.evaluate(2) will return 5
? add(MyPolynomial p) will accept another polynomial as parameter and return the sum of the current polynomial and the polynomial p.
For example, given that p is the polynomial 1 + 2x^2, the call poly.add(p) will produce
2 + -x^2 + 2x^3
? subtract(MyPolynomial p) will accept another polynomial as parameter and return the difference between the current polynomial and the polynomial p.
For example, given that p is the polynomial 1 + 2x^2, the call poly.subtract(p) will produce
-5x^2 + 2x^3
? multiply(MyPolynomial p) will accept another polynomial as parameter and return the product of the current polynomial and polynomial p.
For example, given that p is the polynomial 1 + 2x^2, the call poly.multiply(p) will produce 1 + -x^2 + 2x^3 + -6x^4 + 4x^5
? toString() will create a string for displaying the list of terms.
For example, System.out.println(poly) will print 1 + -3x^2 + 2x^3
? main will provide the code for testing the class.

Term class

This class will encapsulate the coefficient and exponent of a term, the values for which are integers.

Constructor

A constructor accepting two integer values (for the coefficient and exponent respectively) is required.

Methods

The following methods are required:

? evaluate(int n) will return the value of a term for an integer value n.
? sameExponentAs(Term t) will return a boolean expressing the equality of the exponent between the current term and term t. (Only compare the exponent, NOT the coefficient.) This method will be called in adding two polynomials.
? toString() will create a string for displaying the term in the form of ax^n eg. 3x^2.
? multiply(Term t) will return the product of the current term and term t.
? add(Term t) will return the sum of the current term and term t.

Test your program using a file containing at least the following data.

polys.data

1 0 :: 5 2 -3 6
2 2 -3 4 :: 1 1 4 5
-1 1 -2 2 -3 3 :: 1 1 2 2 3 3
1 1 1 2 1 3 1 4 :: 1 1 1 2 1 3 1 4

In this file a double colon is used to indicate the end of a list of coefficient and exponent for a polynomial. There will be two polynomials per lines. For each pair of polynomials your test code should provide the following details:
? A list of its terms;
? The value of the polynomials when x = 2;
? The sum of the polynomials;
? The difference of the polynomials;
? The product of the polynomials.
[3923 byte] By [Samantha Ann] at [2007-11-15 20:13:57]