Comp 212 Assignment # 2
Polynomials
Due Monday, September23, 2002 11:59 PM - No Late Submission will be accepted
This homework illustrates the use of abstraction to implement polynomial and turn it into what is coined as a "component software". It makes use of the visitor pattern, the composite pattern, and the abstract factory pattern.
A polynomial of one variable, p(x), is an expression of the form
anxn + an-1xn-1+ ... + a1x + a0,
where n is a non-negative integer, ak (k = 0.. n) are numbers, and an is a number not equal to 0.
n is called the order (or degree) of p(x),ak (k = 0.. n) are called the coefficients of p(x), and an is called the leading coefficient of p(x).
For examples:
We can describe polynomials in the following manner. There is an abstraction called polynomial. A constant polynomial is a polynomial with a leading coefficient and order (degree) 0. A non-constant polynomial is a polynomial with a leading coefficient, a positive order, and a lower order polynomial. The polynomial's intrinsic behavior is to provide the clients with its order -getOrder (), its leading coefficient - get LeadCoef (), and, in the case of a non-constant polynomial, its lower order polynomial - getLowerPoly (). Constant polynomials do not contain lower order polynomials and have no getLowerPoly() method.
Polynomials can be viewed as having an immutable linear recursive structure and can be implemented using the composite pattern. All extrinsic operations on polynomials can be implemented as visitors to polynomials. Below is a UML class diagram showing the design of polynomials as framework analogous to that of the immutable list structure, IList.
Click here to see the javadoc for the above framework..
Your task is to implement a concrete factory to manufacture IPoly objects and to write a few concrete IPolyOp as operations on IPoly.
package poly.op; import poly.*; /** * Holds the quotient/remainder pair in polynomial long division. */ public class QRPair { private IPoly _quotient; private IPoly _remainder; public QRPair(IPoly quotient, IPoly remainder) { _quotient = quotient; _remainder = remainder; } public final IPoly getQuotient() { return _quotient; } public final IPoly getRemainder() { return _remainder; } }
LongDiv should take an IPolyFact
as input parameter to the constructor. Use anonymous inner classes as helpers.
Package this concrete class in a package names poly.op.
EvalHorner
to evaluate the host polynomial at a "point" x using
Horner's algorithm:EvalHorner
should take a Double (the value x to be evaluated at) as input
parameter. Use
anonymous inner classes as helpers. Package this concrete class in a
package names poly.op.For each of the above problems, be sure to add test cases which cover all reasonable
cases. For instance, the visitor EvalHorner
should be tested on constant
polynomials. The client test program may be in the default (no-name) package.
Since polynomials are mathematical objects that are intrinsically immutable, all
polynomials in this exercise should NOT be modified once they are manufactured by a
factory.
As with all programs in this course, lack of good coding style (good style includes reasonable variable names, a header before each non-overridden function, reasonable indentation) will result in a substantial loss of points.
The homework is due Monday, September 23, 2002 11:59 PM . It is to be submitted electronically. The electronic submission project name is "poly". The complete homework set should contain the following: