Class Polynomial

java.lang.Object
  extended by Polynomial

public class Polynomial
extends java.lang.Object

Polynomial is a class to represent polynomials over variable x. For now, limited to integer coefficients and exponents. Each term is represented by a Term object.


Constructor Summary
Polynomial()
          Constructor for objects of class Polynomial
Polynomial(java.lang.String init)
          Constructor with initial polynomial as String.
 
Method Summary
 void addTerm(Term t)
          Add a term to this polynomial.
 boolean equals(java.lang.Object obj)
          Compare this object to the specified object.
 double evaluate(int x)
          Evaluates the polynomial for given value of variable x.
static Polynomial product(Polynomial a, Polynomial b)
          Produces new polynomial which is the product of the two argument polynomials.
static Polynomial sum(Polynomial a, Polynomial b)
          Produces new polynomial which is the sum of the two argument polynomials.
 java.lang.String toString()
          Produce String representation of a Polynomial.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Polynomial

public Polynomial()
Constructor for objects of class Polynomial


Polynomial

public Polynomial(java.lang.String init)
           throws java.lang.IllegalArgumentException
Constructor with initial polynomial as String. Format is very restricted.

Postcondition: Polynomial is initialized with terms based on init String.

Parameters:
init - the initial polynomial. This format MUST be followed: each term consists of two integers separated by one or more spaces: an integer coefficient and an integer exponent. Likewise, each term must be separated by one or more spaces. The terms can be given in any order. For example "3 0 5 -2 -1 1" represents 3+5x^-2-x
Throws:
java.lang.IllegalArgumentException - if the argument string is not properly formatted.
Method Detail

addTerm

public void addTerm(Term t)
             throws java.lang.IllegalArgumentException
Add a term to this polynomial. If this polynomial already has a term with the same exponent as the argument, the argument term will be combined with the existing one.

Precondition: t != null

Postcondition:this.equals(sum(OLD, polynomial consisting of term t))

Parameters:
t - a term to add into this polynomial.
Throws:
java.lang.IllegalArgumentException - if the argument is null.

evaluate

public double evaluate(int x)
Evaluates the polynomial for given value of variable x.

Parameters:
x - the x-value at which to evaluate.
Returns:
value of the expression for that x.

sum

public static Polynomial sum(Polynomial a,
                             Polynomial b)
                      throws java.lang.IllegalArgumentException
Produces new polynomial which is the sum of the two argument polynomials. The sum is a polynomial consisting of all terms from both polynomials, with terms having same exponent combined. For example, the sum of x^2+2x-1 and 2x^3+3x is 2x^3+x^2+3x+2x-1 = 2x^3+x^2+5x-1.

Preconditions: a != null && b != null

Parameters:
a - a polynomial
b - a polynomial
Returns:
a polynomial representing the sum of a and b.
Throws:
java.lang.IllegalArgumentException - if either argument is null.

product

public static Polynomial product(Polynomial a,
                                 Polynomial b)
                          throws java.lang.IllegalArgumentException
Produces new polynomial which is the product of the two argument polynomials. Product is a polynomial whose terms are formed by from the product of each term in the first polynomial with all the terms of the second, combining terms with the same exponent. For example, the product of 2x+3 and 4x-1 is (2x * 4x) + (2x * -1) + (3 * 4x) + (3 * -1) = 8x^2-2x+12x-3 = 8x^2+10x-3.

Preconditions: a != null && b != null

Parameters:
a - a polynomial
b - a polynomial
Returns:
a polynomial representing the product of a and b.
Throws:
java.lang.IllegalArgumentException - if either argument is null.

equals

public boolean equals(java.lang.Object obj)
Compare this object to the specified object. The result is true if and only if the argument is not null and is a Polynomial object, has the same number of terms as this object and every term of the argument is equal to a term of this object.

Overrides:
equals in class java.lang.Object
Parameters:
obj - The object to compare with.
Returns:
true if the objects are equivalent based on the above criteria; false otherwise.

toString

public java.lang.String toString()
Produce String representation of a Polynomial. Terms are listed in decreasing order by exponent (e.g. highest exponent first) with no spaces between terms. Any terms with 0 as coefficient are not included.

Overrides:
toString in class java.lang.Object
Returns:
String representing the polynomial.