CSC 132 Fall 2000
Homework 2
Due: 9 a.m. 14 September 2000
30 points
"Poly wants a double."
Modify the IntArrayPoly class from Lab 1 to create the DoubleArrayPoly class. My solution to Lab 1 is available in IntArrayPoly.java on eccentric download. It will differ from the original class in three respects.
1. All coefficients are doubles, and the value of x is now a double for evaluation purposes.
2. Change implementation to a more efficient way of using arrays.
3. Change format of the comments to be in a form usable by javadoc.
The array implementation of Lab 1 is very space-inefficient for storing "sparce polynomials", e.g. polynomials with high degree but few terms. Space can be saved by explicitly storing two values for each term: the coefficient and the exponent. In the original implementation, the exponent was not stored explicitly, but was implied from the array index. By storing both explicitly, in theory no storage will be wasted (only terms with non-zero coefficient are stored). The tradeoff is that algorithms to process the data may run more slowly. The coefficient/exponent pairs may be either stored in a pair of arrays, or you may create a simple class to hold both and use an array of objects of that class. You will want to keep a count of the number of terms, so an additional instance variable is needed.
There is no requirement that the terms be stored in any particular order, but it will still be necessary for the Print method to print terms in increasing order of exponent value.
Since your methods will know in advance the maximum degree of the polynomial but not the number of terms, the constructors should allocate space for 5 terms. If InsertTerm is called and the array is full, increase the capacity by 5 more. See code for ensureCapacity on page 128 of the text for an example of this. Basically, you allocate a bigger array, copy the contents of the smaller one into the bigger one, then change the reference to the bigger one.
DoublePolyDriver.java is on eccentric download for your use in testing this class. When finished, submit DoubleArrayPoly.java to your upload folder.
Allocation of the 30 points is the same as before: 5 points documentation, 5 points design and code structure, 20 points correct and reliable results. See Homework 1 for explanations.
[ Assignments | CSC 132 | Peter Sanderson | Computer Science | SMSU ]
Last reviewed: 8 September 2000
Peter Sanderson (
PeteSanderson@smsu.edu )