calculator
Class RatTerm

java.lang.Object
  extended by calculator.RatTerm

public final class RatTerm
extends java.lang.Object

RatTerm is an immutable representation of a term in a single-variable polynomial expression. The term has the form C*x^E where C is a rational number and E is an integer.

A RatTerm, t, can be notated by the pair (C . E), where C is the coefficient of t, and E is the exponent of t.

The zero RatTerm, (0 . 0), is the only RatTerm that may have a zero coefficient. For example, (0 . 7) is an invalid RatTerm and an attempt to construct such a RatTerm (through the constructor or arithmetic operations on existing RatTerms) will return the semantically equivalent RatTerm (0 . 0). For example, (1 . 7) + (-1 . 7) = (0 . 0).

(0 . 0), (1 . 0), (1 . 1), (1 . 3), (3/4 . 17), (7/2 . -1), and (NaN . 74) are all valid RatTerms, corresponding to the polynomial terms "0", "1", "x", "x^3", "3/4*x^17", "7/2*x^-1" and "NaN*x^74", respectively.


Field Summary
static RatTerm NaN
          A constant holding a Not-a-Number (NaN) value of type RatTerm
static RatTerm ZERO
          A constant holding a zero value of type RatTerm
 
Constructor Summary
RatTerm(RatNum c, int e)
           
 
Method Summary
 RatTerm add(RatTerm arg)
          Addition operation.
 RatTerm antiDifferentiate()
          Returns the antiderivative of this RatTerm.
 RatTerm differentiate()
          Return the derivative of this RatTerm.
 RatTerm div(RatTerm arg)
          Division operation.
 boolean equals(java.lang.Object obj)
          Standard equality operation.
 double eval(double d)
          Returns the value of this RatTerm, evaluated at d.
 RatNum getCoeff()
          Gets the coefficient of this RatTerm.
 int getExpt()
          Gets the exponent of this RatTerm.
 int hashCode()
          Standard hashCode function.
 boolean isNaN()
          Returns true if this RatTerm is not-a-number.
 boolean isZero()
          Returns true if this RatTerm is equal to 0.
 RatTerm mul(RatTerm arg)
          Multiplication operation.
 RatTerm negate()
          Negation operation.
 RatTerm sub(RatTerm arg)
          Subtraction operation.
 java.lang.String toString()
          Returns a string representation of this RatTerm.
static RatTerm valueOf(java.lang.String termStr)
          Builds a new RatTerm, given a descriptive String.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NaN

public static final RatTerm NaN
A constant holding a Not-a-Number (NaN) value of type RatTerm


ZERO

public static final RatTerm ZERO
A constant holding a zero value of type RatTerm

Constructor Detail

RatTerm

public RatTerm(RatNum c,
               int e)
Effects:
Constructs a new RatTerm t, with t.coeff = c, and if c.equals(RatNum.ZERO), then t.expt = 0, otherwise t.expt = e
Requires:
c != null
Method Detail

getCoeff

public RatNum getCoeff()
Gets the coefficient of this RatTerm.

Returns:
the coefficient of this RatTerm.

getExpt

public int getExpt()
Gets the exponent of this RatTerm.

Returns:
the exponent of this RatTerm.

isNaN

public boolean isNaN()
Returns true if this RatTerm is not-a-number.

Returns:
true if and only if this has NaN as a coefficient.

isZero

public boolean isZero()
Returns true if this RatTerm is equal to 0.

Returns:
true if and only if this has zero as a coefficient.

eval

public double eval(double d)
Returns the value of this RatTerm, evaluated at d.

Returns:
the value of this polynomial when evaluated at 'd'. For example, "3*x^2" evaluated at 2 is 12. if (this.isNaN() == true), return Double.NaN

negate

public RatTerm negate()
Negation operation.

Returns:
a RatTerm equals to (-this). If this is NaN, then returns NaN.

add

public RatTerm add(RatTerm arg)
Addition operation.

Returns:
a RatTerm equals to (this + arg). If either argument is NaN, then returns NaN.
Throws:
java.lang.IllegalArgumentException - if (this.expt != arg.expt) and neither argument is zero or NaN.
Requires:
(arg != null) && ((this.expt == arg.expt) || (this.isZero() || arg.isZero() || this.isNaN() || arg.isNaN())).

sub

public RatTerm sub(RatTerm arg)
Subtraction operation.

Returns:
a RatTerm equals to (this - arg). If either argument is NaN, then returns NaN.
Throws:
java.lang.IllegalArgumentException - if (this.expt != arg.expt) and neither argument is zero or NaN.
Requires:
(arg != null) && ((this.expt == arg.expt) || (this.isZero() || arg.isZero() || this.isNaN() || arg.isNaN())).

mul

public RatTerm mul(RatTerm arg)
Multiplication operation.

Returns:
a RatTerm equals to (this * arg). If either argument is NaN, then returns NaN.
Requires:
arg != null

div

public RatTerm div(RatTerm arg)
Division operation.

Returns:
a RatTerm equals to (this / arg). If arg is zero, or if either argument is NaN, then returns NaN.
Requires:
arg != null

differentiate

public RatTerm differentiate()
Return the derivative of this RatTerm.

Returns:
a RatTerm that, q, such that q = dy/dx, where this == y. In other words, q is the derivative of this. If this.isNaN(), then return some q such that q.isNaN()

Given a term, a*x^b, the derivative of the term is: (a*b)*x^(b-1) for b > 0 and 0 for b == 0 (Do not worry about the case when b < 0. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)


antiDifferentiate

public RatTerm antiDifferentiate()
Returns the antiderivative of this RatTerm.

Returns:
a RatTerm, q, such that dq/dx = this where the constant of intergration is assumed to be 0. In other words, q is the antiderivative of this. If this.isNaN(), then return some q such that q.isNaN()

Given a term, a*x^b, (where b >= 0) the antiderivative of the term is: a/(b+1)*x^(b+1) (Do not worry about the case when b < 0. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)


toString

public java.lang.String toString()
Returns a string representation of this RatTerm.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of the expression represented by this.

There is no whitespace in the returned string.

If the term is itself zero, the returned string will just be "0".

If this.isNaN(), then the returned string will be just "NaN"

The string for a non-zero, non-NaN RatTerm is in the form "C*x^E" where C is a valid string representation of a RatNum (see RatNum's toString method) and E is an integer. UNLESS: (1) the exponent E is zero, in which case T takes the form "C" (2) the exponent E is one, in which case T takes the form "C*x" (3) the coefficient C is one, in which case T takes the form "x^E" or "x" (if E is one) or "1" (if E is zero).

Valid example outputs include "3/2*x^2", "-1/2", "0", and "NaN".


valueOf

public static RatTerm valueOf(java.lang.String termStr)
Builds a new RatTerm, given a descriptive String.

Returns:
a RatTerm t such that t.toString() = termStr
Requires:
'termStr' is an instance of a string with no spaces that expresses a RatTerm in the form defined in the toString() method.

Valid inputs include "0", "x", and "-5/3*x^3", and "NaN".


hashCode

public int hashCode()
Standard hashCode function.

Overrides:
hashCode in class java.lang.Object
Returns:
an int that all objects equal to this will also.

equals

public boolean equals(java.lang.Object obj)
Standard equality operation.

Overrides:
equals in class java.lang.Object
Returns:
true iff 'obj' is an instance of a RatTerm and 'this' and 'obj' represent the same RatTerm. Note that all NaN RatTerms are equal.