Assignment #1: Basic Classes
Implement a class, Polynomial, in package
cop3337 to represent single-variable polynomials.
The following test program illustrates the usage of the class.
import cop3337.Polynomial;
class Assign1
{
public static void main( String [ ] args )
{
Polynomial [ ] arr = { new Polynomial( "3" ),
new Polynomial( "x+5" ),
new Polynomial( "x^2+2x+3" ),
new Polynomial( "x^2+x^2+x^2" ),
new Polynomial( "x+2" ),
new Polynomial( "x^3-2x^2+5x^1+1" )
};
Polynomial sum = new Polynomial( );
Polynomial prod = new Polynomial( "1" );
for( Polynomial p : arr )
{
sum = sum.add( p );
prod = prod.multiply( p );
System.out.println( "This: " + p + " total sum is " + sum + " product is " + prod );
}
Polynomial p1 = new Polynomial( "x+5" );
Polynomial p2 = new Polynomial( "x-5" );
Polynomial p3 = new Polynomial( "x^2-25" );
Polynomial p4 = p2.multiply( p1 );
Polynomial p5 = p4.subtract( p3 );
System.out.println( "p1=" + p1 );
System.out.println( "p2=" + p2 );
System.out.println( "p3=" + p3 );
System.out.println( "p4=" + p4 );
System.out.println( "p5=" + p5 );
System.out.println( "p3.equals(p4): " + p3.equals( p4 ) );
System.out.println( "p5 is 0: " + p5.equals( Polynomial.ZERO ) );
}
}
The output of this program is intended to be as follows:
This: 3 total sum is 3 product is 3
This: x+5 total sum is x+8 product is 3x+15
This: x^2+2x+3 total sum is x^2+3x+11 product is 3x^3+21x^2+39x+45
This: 3x^2 total sum is 4x^2+3x+11 product is 9x^5+63x^4+117x^3+135x^2
This: x+2 total sum is 4x^2+4x+13 product is 9x^6+81x^5+243x^4+369x^3+270x^2
This: x^3-2x^2+5x+1 total sum is x^3+2x^2+9x+14 product is 9x^9+63x^8+126x^7+297x^6+828x^5+1548x^4+1719x^3+270x^2
p1=x+5
p2=x-5
p3=x^2-25
p4=x^2-25
p5=0
p3.equals(p4): true
p5 is 0: true
Polynomial Class Sketch
Here is a rough outline of the Polynomial class:
package cop3337;
public class Polynomial
{
public static final Polynomial ZERO = new Polynomial( );
public Polynomial( )
{
degree = 0;
coeff = new int[ 1 ];
coeff[ 0 ] = 0;
}
public Polynomial( String str )
{ /* YOU MUST PROVIDE */ }
// Makes an independent copy
public Polynomial( Polynomial other )
{ /* YOU MUST PROVIDE */ }
public Polynomial negate( )
{ /* YOU MUST PROVIDE */ }
public Polynomial add( Polynomial rhs )
{ /* YOU MUST PROVIDE */ }
public Polynomial subtract( Polynomial rhs )
{ /* YOU MUST PROVIDE */ }
public Polynomial multiply( Polynomial rhs )
{ /* YOU MUST PROVIDE */ }
public boolean equals( Object other )
{ /* YOU MUST PROVIDE */ }
public String toString( )
{ /* YOU MUST PROVIDE */ }
private int [ ] coeff;
private int degree;
}
The functionality of the Polynomial class is as follows:
- Provide at least three constructors: a zero-parameter constructor
that makes the polynomial zero, a constructor that makes a separate
independent copy of an existing polynomial, and a constructor that
creates a polynomial based on a String specification.
The last constructor can throw an exception if the String
specification is invalid, and you can make a design decision
on what a valid specification is.
- negate returns the negative of this polynomial.
- add, subtract, and multiply
return a new polynomial that is the sum, difference, or product, respectively,
of this polynomial and another polynomial, rhs.
None of these methods change either of the original polynomials.
- equals and toString follow the standard
contract for these functions.
For toString you can write either a simple implementation
or a more elaborate version that returns a more compact, easily read formula.
For instance, x^5-3x^2+1 might be output less nicely, but more easily, as
1x^5 + 0x^4 + 0x^3 + -3x^2 + 0x^1 + 1x^0.
Make the output look as nice as you can.
-
The polynomial is represented by two fields. One, degree,
represents the degree of the polynomial. Thus
x^2+2x+1 is degree 2,
3x+5 is degree 1, and
4 is degree 0.
Zero is automatically degree 0.
The second field, coeff, represents the coefficients
(coeff[i] represents the coefficient of x^i).