COP 3337 Assignment 5

advertisement
COP 3337
Pestaina
Assignment 5: Recursion
Due: Midnight, Sunday 3/28
Spring 2010
Overview
A (univariate) polynomial is a sequence of terms. Each term consists of a non-zero
coefficient and a non-negative integer exponent. The following are all examples of
polynomials of a single variable, x, with integer coefficients…
Example 1: 4
Example 2: x4 - 2x + 3
Example 3: 2x3 - x2 + 5x - 2
Example 4: 2x7 - 5x5 + 3x4 - 15x2 + 19x - 6
A polynomial term can also be written as a pair (exponent, coefficient). The polynomial
of Example 3 has 4 terms. Its terms may be written as
1st term: (3, 2)
2nd term: (2, -1)
3rd term: (1, 5)
4th term: (0, -2)
For this assignment, you will implement 2 classes. The class, PolynomialTerm, will give
a representation of a polynomial term with integer coefficients. The class, Polynomial,
will represent a univariate polynomial with integer coefficients. A client program to test
your implementations is provided.
Specification
1. Write the PolynomialTerm class, to include the following features:
 2 instance variables, of type int, for the exponent and coefficient.
 An accessor method for each instance variable.
 A copy() method to return a copy of a PolynomialTerm.
 A plus( .. ) method to return the sum of 2 PolynomialTerms.
 A times( .. ) method to return the product of 2 PolynomialTerms.
2. Write the Polynomial class to include the following features:
 A single instance variable of type ArrayList<PolynomialTerm>. The instance
variable stores the terms of the polynomial, in descending exponent order,
 A default constructor that creates the zero-polynomial (no terms).
 A constructor public Polynomial(int[] data).The data parameter is an array of
alternating exponents and coefficients; each pair of consecutive ints defines one
PolynomialTerm. E.g. [3, 2, 2, -1, 1, 5, 0, -2] for Example 3 above.
 A predicate method, isZero(), to return true iff a Polynomial is the zeropolynomial
 A recursive copy() method to return a deep copy of a Polynomial.
 A recursive plus( .. ) method to return the sum of 2 Polynomials.
 A recursive times( .. ) method to return the product of a Polynomial with a
PolynomialTerm.
 A recursive times( .. ) method to return the product of 2 Polynomials.
 Suitable helper methods!
Download