|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object hw3.RatTerm
public final class RatTerm
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(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. |
String |
toString()
Returns a string representation of this RatTerm. |
static RatTerm |
valueOf(String termStr)
Builds a new RatTerm, given a descriptive String. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final RatTerm NaN
public static final RatTerm ZERO
Constructor Detail |
---|
public RatTerm(RatNum c, int e)
Method Detail |
---|
public RatNum getCoeff()
public int getExpt()
public boolean isNaN()
public boolean isZero()
public double eval(double d)
public RatTerm negate()
public RatTerm add(RatTerm arg)
IllegalArgumentException
- if (this.expt != arg.expt) and neither argument is zero or
NaN.public RatTerm sub(RatTerm arg)
IllegalArgumentException
- if (this.expt != arg.expt) and neither argument is zero or
NaN.public RatTerm mul(RatTerm arg)
public RatTerm div(RatTerm arg)
public RatTerm differentiate()
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.)
public RatTerm antiDifferentiate()
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.)
public String toString()
toString
in class Object
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".
public static RatTerm valueOf(String termStr)
Valid inputs include "0", "x", and "-5/3*x^3", and "NaN".
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |