Lab 6 — Lists

 

Due: Wednesday, March 8.

 

A univariate polynomial of degree d has the form:

cdxd + c d-1 x d-1 + c d-2 x d-2 + . . . + c0

where cd != 0. The cis are the coefficients, and d, d-1, . . . are the exponents. By definition d is a nonnegative integer. For this lab you may assume that the coefficients are also integers. Each cixi is a term of the polynomial. Your task is to develop a Java class to support arithmetic involving polynomials. For this exercise you will represent each polynomial as a list (c0, c1, c2, . . . cd) of coefficients.

Develop a Java class Polynomial that should have an instance data member degree, which is the degree of the polynomial. It may have other instance data members also. Your polynomial class should support the following operations:

  1. Polynomial() — Create the zero polynomial. The degree of this polynomial is 0 and it has no terms. It is the constructor.
  2. degree() — Return the degree of the polynomial.
  3. input(inStream) — Read in a polynomial from the input stream inStream. You may assume the input consists of the polynomial degree and a list of coefficients in ascending order of exponents.
  4. output() — Output the polynomial.
  5. add(b) — Add b to the polynomial and return the result.
  6. subtract(b) — Subtract the polynomial b and return the result.
  7. multiply(b) — Multiply the polynomial b and return the result.
  8. divide(b) — Divide the polynomial b and return the quotient.
  9. valueOf(x) — Return the value of the polynomial at point x.

Test your code! Hand in your code and output to convince me it works. Put your code in the dropbox with the usual naming conventions.