Class Term

java.lang.Object
  extended by Term
All Implemented Interfaces:
java.lang.Comparable<Term>

public class Term
extends java.lang.Object
implements java.lang.Comparable<Term>

Class to hold one term of a polynomial over x. Both coefficient and exponent are ints.


Constructor Summary
Term(int coef, int expon)
          Constructor for objects of class Term.
Term(Term t)
          Constructor for objects of class Term.
 
Method Summary
 void combine(Term term)
          Combines this term and the argument term back into this term.
 int compareTo(Term term)
          Imposes "natural ordering" on Term objects.
 boolean equals(java.lang.Object obj)
          Compare this object to the specified object.
 int getCoefficient()
          Fetch coefficient value.
 int getExponent()
          Fetch exponent value.
static Term product(Term a, Term b)
          Returns a new Term formed by the product of two Terms.
 void setCoefficient(int coef)
          Replace coefficient value with new one.
static Term sum(Term a, Term b)
          Returns a new Term formed by the sum of two Terms.
 java.lang.String toString()
          Returns a String representing this term, with fully signed coefficient and using nothing for multiplication and ^ for exponentiation.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Term

public Term(int coef,
            int expon)
Constructor for objects of class Term. Represents coef * x expon

Postconditions:
getCoefficient() == coef
getExponent() == expon

Parameters:
coef - the coefficient value
expon - the exponent value

Term

public Term(Term t)
Constructor for objects of class Term. Represents coef * x expon

Postconditions:
getCoefficient() == t.getCoefficient()
getExponent() == t.getExponent()

Parameters:
t - Term whose contents will be copied into the new one
Method Detail

getCoefficient

public int getCoefficient()
Fetch coefficient value.

Returns:
the coefficient value

getExponent

public int getExponent()
Fetch exponent value.

Returns:
the exponent value

setCoefficient

public void setCoefficient(int coef)
Replace coefficient value with new one.

Postcondition: getCoefficient() == coef

Parameters:
coef - the new coefficient value.

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 Term object that contains the same exponent and coefficient as this object.

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

compareTo

public int compareTo(Term term)
              throws java.lang.IllegalArgumentException
Imposes "natural ordering" on Term objects. This permits arrays and collections of Term objects to be sorted and arranged by Collections and Arrays static methods. Very nice! Ordering is based primarily on exponent value and secondarily on coefficient value (e.g. if exponents are equal, decision is based on coefficients).

Precondition: term != null

Specified by:
compareTo in interface java.lang.Comparable<Term>
Parameters:
term - The Term against which this Term is compared
Returns:
Returns -1 if this object is less than argument object, 0 if they are equal and 1 if this object is greater than argument object.
Throws:
java.lang.IllegalArgumentException - if the argument is null.

combine

public void combine(Term term)
             throws java.lang.IllegalArgumentException
Combines this term and the argument term back into this term. The argument term is not affected. Terms are combined by adding their coefficients.

Precondition: term != null && getExponent() == term.getExponent()

Postcondition: getCoefficient() == OLD.getCoefficient() + term.getCoefficient()

Parameters:
term - the term to be folded into this term.
Throws:
java.lang.IllegalArgumentException - if the argument is null or terms have different exponents.

sum

public static Term sum(Term a,
                       Term b)
                throws java.lang.IllegalArgumentException
Returns a new Term formed by the sum of two Terms. The sum is a term formed by adding the coefficients of two Terms that have the same exponent.

Precondition: a != null && b != null && a.getExponent() == b.getExponent()

Parameters:
a - first term in the sum.
b - second term in the sum.
Returns:
a new Term containing the sum of the two argument terms.
Throws:
java.lang.IllegalArgumentException - if the argument is null or terms have different exponents.

product

public static Term product(Term a,
                           Term b)
                    throws java.lang.IllegalArgumentException
Returns a new Term formed by the product of two Terms. The product (multiplication) is a term formed by multiplying the coefficients and adding the exponents. For example, the product of 3x^2 and 4x is 12x^3.

Precondition: a != null && b != null

Parameters:
a - first term in the product.
b - second term in the product.
Returns:
a new Term containing the product of the two argument terms.
Throws:
java.lang.IllegalArgumentException - if the argument is null.

toString

public java.lang.String toString()
Returns a String representing this term, with fully signed coefficient and using nothing for multiplication and ^ for exponentiation.

Overrides:
toString in class java.lang.Object
Returns:
a String equivalent of this term