A polynomial class using arrays

advertisement
CS46B
Polynomials with arrays
Implement a polynomial class using arrays. (See Data Structures and Other Objects
Using Java, 2nd edition, by Michael Main, Programming Project 8, page 163).. Use a
partially filled array, coef, to store the coefficients and also have an instance variable to
indicate the degree of the polynomial and another instance variable, currentPower,
indicating the current power. For example if the polynomial were 2 + 3 x + 4 x2 the
degree would be 2 (note the effective size of the array would be degree + 1). Note that
usually coef.length will be bigger than degree since you should be using a partially filled
array. If one was looking at the linear term (3 x ), perhaps to change it or print it out, the
currentPower instance variable would be 1. Call your class Polynomial. In addition to an
add method and an evaluate function have a polynomial multiply method. Evaluate
should be a class method but add and multiply should be static methods whose
parameters are two polynomials and return a polynomial. Include an equals, clone and
toString method.
Have a separate method polyInput that handles input, asking the user for the degree first
and then the coefficients of the polynomial. Do not use Horstmann’s consoleReader.
Assume that as input to polyInput the user first types in the polynomial degree and then
the polynomial coefficients in order of increasing power. You may assume that the user
must type in zero for missing powers. For example 2 + 3 x 2 would be typed in as 2 0
3. Allow the coefficients to be input on one or several lines (you will need to use
stringTokenizer if you don’t use easyReader or doubleInput if you do use easyReader).
Also create and use in polyInput a method “public void setCoef(double coefValue)” that
sets the coefficient indicated by currentPower to coefValue.
Have a separate method polyOutput that will output a polynomial in a reasonable format.
You could simply write out the coefficients in order (for example “2 0 3”) or get fancier
and write out something like “2 + 3 x 2” or “2 + 3 x^2” if you prefer. If you use the first
style clearly label what your output means.
You can do multiplication of polynomials several ways. In one way if
f ( x) 

n
i 0
ai x i and g ( x)  i 0 bi x i
m
Then h( x)  f ( x) g ( x)  k 0 ck x k has coefficients
m n
ck   j max( 0,k n ) ak  j b j ,
min( k , m )
k  0,, m  n.
A second way is to use following idea: if
f ( x) 

n
i 0
ai x i and
g ( x)  i 0 bi x i then h( x)  f ( x) g ( x)  k 0 ck x k has coefficients that can be
m
calculated by:
Set all c coefficients to zero.
for ( i = 0; i <= n ; i++)
for ( j = 0 ; j <= m; j++)
ci+j += ai bj;
m n
This second ways mimics one way to multiply two polynomials when doing the
multiplication by hand.
Include description of the invariant of the ADT in your code and use javadoc comments
in the style suggested by Main. Otherwise use good style as described in the first
program assignment.
Set the initial capacity of your arrays initially to 2 (to force you to resize some of the
arrays in the runs below).
Create a class TestPolynomial with a main program that will do the following:
Input the degree and coefficients of a polynomial f(x)
Input the degree and coefficients of a polynomial g(x)
Output the degree and coefficients of f(x) + g(x).
Output the degree and coefficients of h(x) = f(x) * g(x).
Output the value of h(3).
Also let p(x) be a clone of f(x) and use an if statement involving equals to write out
whether p(x) is the same as f(x) and whether p(x) is the same as g(x).
Do the following runs
a) f(x) = 1 + 4x + 5 x2, g(x) = 1 + 4x + 5 x2
b) f(x) = 3, g(x) = 1 + 4x + 5 x2
c) f(x) = 3 + 4 x – x2 + 2 x4, g(x) = 1 –3x2 – x3
d) f(x) = 8, g(x) = 4
Note the grader may try other runs so you should test them.
Turn in a listing of your source code, output of all of the above runs, javadoc
documentation for your Polynomial class and a disk with Polynomial.java,
TestPolynomial.java, as well as EasyReader or FormatWriter, if you use them. Place all
these in the same directory (so delete the package statements from EasyReader and
FormatWriter). You may develop your program using any Java but test it with TextPad
and Sun’s Java (jdk 1.3 or 1.4) before turning it in. The grader will be using these and
will deduct points if your code does not run on his / her computer. Clip your disk to your
output with a “black clip”.
Download