Tutorial 6: Array Implementation of Polynomials


Introduction

This tutorial covers:


Below is a UML diagram describing the design of ArrayPoly, an implementation of polynomials using arrays.

ArrayPoly.png (17522 bytes)

Note that the abstract behavior of polynomials represented by APolynomial is still the same as in homework #1.  It's only the concrerte implementation that is changed.   As a result all algorithms on APolynomial represented by IVisitor should plugin and work unmodified.  This illustrates the importance and usefulness of separating the interface (abstract behavior) from the implementation.  The private constructor and methods in ArrayPoly are used to support some of the public methods.  Though in this lab you are asked to write them.  You do not have to write and use them in your own implementation.

Exercises:

1. Write the code for ArrayPoly (double[] coefs, int degree).  This assigns the array coefs to the coefficient array and degree to the degree of this ArrayPoly.   Though trivial as it is, it is designed to save memory by sharing coefficients between polynomials whenever possible, exploiting the fact that polynomials are immutable objects.

2. Write the code for void copyFrom (ArrayPoly poly).  This is to copy the coefficient array from the parameter poly to this ArrayPoly.  This is used when sharing coefficients is not possible.

3.  Write the code for int getLowerDegree () to compute the degree of the lower order polynomial.

4. Food for thoughts: Ignore for the moment the public methods of ArrayPoly and all the visitors on APolynomial that you currently have.  Write a method for ArrayPoly to compute and return the sum polynomial of the receiver and the parameter polynomial by manipulating the coefficient array directly.

 

dxnguyen@cs.rice.edu
revised 02/28/00