Package hw4

Class 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

      Fields 
      Modifier and Type Field Description
      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

      Constructors 
      Constructor Description
      RatTerm​(RatNum c, int e)  
    • 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)
        Parameters:
        c - the coefficient of the RatTerm to be constructed.
        e - the exponent of the RatTerm to be constructed.
        Spec.effects:
        Constructs a new RatTerm t, with t.coeff = c, and if c.equals(RatNum.ZERO), then t.expt = 0, otherwise t.expt = e
        Spec.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.
        Parameters:
        d - The value at which to evaluate this term.
        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.
        Parameters:
        arg - The other value to be added.
        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.
        Spec.requires:
        arg != null
      • sub

        public RatTerm sub​(RatTerm arg)
        Subtraction operation.
        Parameters:
        arg - The value to be subtracted.
        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.
        Spec.requires:
        arg != null
      • mul

        public RatTerm mul​(RatTerm arg)
        Multiplication operation.
        Parameters:
        arg - The other value to be multiplied.
        Returns:
        a RatTerm equals to (this * arg). If either argument is NaN, then returns NaN.
        Spec.requires:
        arg != null
      • div

        public RatTerm div​(RatTerm arg)
        Division operation.
        Parameters:
        arg - The divisor.
        Returns:
        a RatTerm equals to (this / arg). If arg is zero, or if either argument is NaN, then returns NaN.
        Spec.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.
        Parameters:
        termStr - A string of the format described in the @spec.requires clause.
        Returns:
        a RatTerm t such that t.toString() = termStr
        Spec.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
        Parameters:
        obj - The object to be compared for equality.
        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.