Polynomial - throw new Overflow( ); for( int i = 0; i...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import DataStructures.Overflow; /* * This code doesn't really do much, and abstraction is not built in. * Thus, I haven't bothered testing it exhaustively. */ public class Polynomial { public static final int MAX_DEGREE = 100; public static int max( int a, int b ) { return a > b ? a : b; } public Polynomial( ) { zeroPolynomial( ); } public void zeroPolynomial( ) { for( int i = 0; i <= MAX_DEGREE; i++ ) coeffArray[ i ] = 0; highPower = 0; } public Polynomial add( Polynomial rhs ) { Polynomial sum = new Polynomial( ); sum.highPower = max( highPower, rhs.highPower ); for( int i = sum.highPower; i >= 0; i-- ) sum.coeffArray[ i ] = coeffArray[ i ] + rhs.coeffArray[ i ]; return sum; } public Polynomial multiply( Polynomial rhs ) throws Overflow { Polynomial product = new Polynomial( ); product.highPower = highPower + rhs.highPower; if( product.highPower > MAX_DEGREE )
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: throw new Overflow( ); for( int i = 0; i &lt;= highPower; i++ ) for( int j = 0; j &lt;= rhs.highPower; j++ ) product.coeffArray[ i + j ] += coeffArray[ i ] * rhs.coeffArray[ j ]; return product; } public void print( ) { for( int i = highPower; i &gt; 0; i-- ) System.out.print( coeffArray[ i ] + &quot;x^&quot; + i + &quot; + &quot; ); System.out.println( coeffArray[ 0 ] ); } private int coeffArray[ ] = new int [ MAX_DEGREE + 1 ]; private int highPower = 0; public static void main( String [ ] args ) { Polynomial p = new Polynomial( ); Polynomial q; p.highPower = 1; p.coeffArray[ 0 ] = 1; p.coeffArray[ 1 ] = 1; try { q = p.multiply( p ); p = q.multiply( q ); } catch( Overflow e ) { System.out.println( &quot;Unexpected overflow&quot; ); } q = p.add( p ); q.print( ); } }...
View Full Document

Page1 / 2

Polynomial - throw new Overflow( ); for( int i = 0; i...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online