hw3
Class RatPoly

java.lang.Object
  extended by hw3.RatPoly

public final class RatPoly
extends Object

RatPoly represents an immutable single-variate polynomial expression. RatPolys are sums of RatTerms with non-negative exponents.

Examples of RatPolys include "0", "x-10", and "x^3-2*x^2+5/3*x+3", and "NaN".


Field Summary
static RatPoly NaN
          A constant holding a Not-a-Number (NaN) value of type RatPoly
static RatPoly ZERO
          A constant holding a zero value of type RatPoly
 
Constructor Summary
RatPoly()
           
RatPoly(int c, int e)
           
RatPoly(RatTerm rt)
           
 
Method Summary
 RatPoly add(RatPoly p)
          Addition operation.
 RatPoly antiDifferentiate(RatNum integrationConstant)
          Returns the antiderivative of this RatPoly.
 int degree()
          Returns the degree of this RatPoly.
 RatPoly differentiate()
          Return the derivative of this RatPoly.
 RatPoly div(RatPoly p)
          Division operation (truncating).
 boolean equals(Object obj)
          Standard equality operation.
 double eval(double d)
          Returns the value of this RatPoly, evaluated at d.
 RatTerm getTerm(int deg)
          Gets the RatTerm associated with degree 'deg'
 int hashCode()
          Standard hashCode function.
 double integrate(double lowerBound, double upperBound)
          Returns the integral of this RatPoly, integrated from lowerBound to upperBound.
 boolean isNaN()
          Returns true if this RatPoly is not-a-number.
 RatPoly mul(RatPoly p)
          Multiplication operation.
 RatPoly negate()
          Return the additive inverse of this RatPoly.
 RatPoly sub(RatPoly p)
          Subtraction operation.
 String toString()
          Returns a string representation of this RatPoly.
static RatPoly valueOf(String polyStr)
          Builds a new RatPoly, given a descriptive String.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NaN

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


ZERO

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

Constructor Detail

RatPoly

public RatPoly()
Effects:
Constructs a new Poly, "0".

RatPoly

public RatPoly(RatTerm rt)
Requires:
rt.getExpt() >= 0
Effects:
Constructs a new Poly equal to "rt". If rt.getCoeff() is zero, constructs a "0" polynomial.

RatPoly

public RatPoly(int c,
               int e)
Requires:
e >= 0
Effects:
Constructs a new Poly equal to "c*x^e". If c is zero, constructs a "0" polynomial.
Method Detail

degree

public int degree()
Returns the degree of this RatPoly.

Returns:
the largest exponent with a non-zero coefficient, or 0 if this is "0".
Requires:
!this.isNaN()

getTerm

public RatTerm getTerm(int deg)
Gets the RatTerm associated with degree 'deg'

Returns:
the RatTerm of degree 'deg'. If there is no term of degree 'deg' in this poly, then returns the zero RatTerm.
Requires:
!this.isNaN()

isNaN

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

Returns:
true if and only if this has some coefficient = "NaN".

negate

public RatPoly negate()
Return the additive inverse of this RatPoly.

Returns:
a RatPoly equal to "0 - this"; if this.isNaN(), returns some r such that r.isNaN()

add

public RatPoly add(RatPoly p)
Addition operation.

Returns:
a RatPoly, r, such that r = "this + p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
Requires:
p != null

sub

public RatPoly sub(RatPoly p)
Subtraction operation.

Returns:
a RatPoly, r, such that r = "this - p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
Requires:
p != null

mul

public RatPoly mul(RatPoly p)
Multiplication operation.

Returns:
a RatPoly, r, such that r = "this * p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
Requires:
p != null

div

public RatPoly div(RatPoly p)
Division operation (truncating).

Returns:
a RatPoly, q, such that q = "this / p"; if p = 0 or this.isNaN() or p.isNaN(), returns some q such that q.isNaN()

Division of polynomials is defined as follows: Given two polynomials u and v, with v != "0", we can divide u by v to obtain a quotient polynomial q and a remainder polynomial r satisfying the condition u = "q * v + r", where the degree of r is strictly less than the degree of v, the degree of q is no greater than the degree of u, and r and q have no negative exponents.

For the purposes of this class, the operation "u / v" returns q as defined above.

The following are examples of div's behavior: "x^3-2*x+3" / "3*x^2" = "1/3*x" (with r = "-2*x+3"). "x^2+2*x+15 / 2*x^3" = "0" (with r = "x^2+2*x+15"). "x^3+x-1 / x+1 = x^2-x+2 (with r = "-3").

Note that this truncating behavior is similar to the behavior of integer division on computers.

Requires:
p != null

differentiate

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

Returns:
a RatPoly, 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()

The derivative of a polynomial is the sum of the derivative of each term.


antiDifferentiate

public RatPoly antiDifferentiate(RatNum integrationConstant)
Returns the antiderivative of this RatPoly.

Returns:
a RatPoly, q, such that dq/dx = this and the constant of integration is "integrationConstant" In other words, q is the antiderivative of this. If this.isNaN() or integrationConstant.isNaN(), then return some q such that q.isNaN()

The antiderivative of a polynomial is the sum of the antiderivative of each term plus some constant.

Requires:
integrationConstant != null

integrate

public double integrate(double lowerBound,
                        double upperBound)
Returns the integral of this RatPoly, integrated from lowerBound to upperBound.

The Fundamental Theorem of Calculus states that the definite integral of f(x) with bounds a to b is F(b) - F(a) where dF/dx = f(x) NOTE: Remember that the lowerBound can be higher than the upperBound.

Returns:
a double that is the definite integral of this with bounds of integration between lowerBound and upperBound. If this.isNaN(), or either lowerBound or upperBound is Double.NaN, return Double.NaN.

eval

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

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

toString

public String toString()
Returns a string representation of this RatPoly.

Overrides:
toString in class Object
Returns:
A String representation of the expression represented by this, with the terms sorted in order of degree from highest to lowest.

There is no whitespace in the returned string.

If the polynomial 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 poly is in the form "(-)T(+|-)T(+|-)...", where "(-)" refers to a possible minus sign, if needed, and "(+|-)" refer to either a plus or minus sign, as needed. For each term, T takes the form "C*x^E" or "C*x" where C > 0, UNLESS: (1) the exponent E is zero, in which case T takes the form "C", or (2) the coefficient C is one, in which case T takes the form "x^E" or "x". In cases were both (1) and (2) apply, (1) is used.

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


valueOf

public static RatPoly valueOf(String polyStr)
Builds a new RatPoly, given a descriptive String.

Returns:
a RatPoly p such that p.toString() = polyStr
Requires:
'polyStr' is an instance of a string with no spaces that expresses a poly in the form defined in the toString() method.

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


hashCode

public int hashCode()
Standard hashCode function.

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

equals

public boolean equals(Object obj)
Standard equality operation.

Overrides:
equals in class Object
Returns:
true if and only if 'obj' is an instance of a RatPoly and 'this' and 'obj' represent the same rational polynomial. Note that all NaN RatPolys are equal.