Package hw4

Class RatNum

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<RatNum>

    public final class RatNum
    extends java.lang.Number
    implements java.lang.Comparable<RatNum>
    RatNum represents an immutable rational number. It includes all of the elements in the set of rationals, as well as the special "NaN" (not-a-number) element that results from division by zero.

    The "NaN" element is special in many ways. Any arithmetic operation (such as addition) involving "NaN" will return "NaN". With respect to comparison operations, such as less-than, "NaN" is considered equal to itself, and larger than all other rationals.

    Examples of RatNums include "-1/13", "53/7", "4", "NaN", and "0".

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static RatNum NaN
      A constant holding a Not-a-Number (NaN) value of type RatNum
      static RatNum ZERO
      A constant holding a zero value of type RatNum
    • Constructor Summary

      Constructors 
      Constructor Description
      RatNum​(int n)  
      RatNum​(int n, int d)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      RatNum add​(RatNum arg)
      Addition operation.
      int compareTo​(RatNum rn)
      Compares two RatNums.
      RatNum div​(RatNum arg)
      Division operation.
      double doubleValue()
      Approximates the value of this rational.
      boolean equals​(java.lang.Object obj)
      Standard equality operation.
      float floatValue()
      Returns a float approximation for this.
      int hashCode()
      Standard hashCode function.
      int intValue()
      Returns an integer approximation for this.
      boolean isNaN()
      Returns true if this is NaN
      boolean isNegative()
      Returns true if this is negative.
      boolean isPositive()
      Returns true if this is positive.
      long longValue()
      Returns a long approximation for this.
      RatNum mul​(RatNum arg)
      Multiplication operation.
      RatNum negate()
      Returns the additive inverse of this RatNum.
      RatNum sub​(RatNum arg)
      Subtraction operation.
      java.lang.String toString()  
      static RatNum valueOf​(java.lang.String ratStr)
      Makes a RatNum from a string describing it.
      • Methods inherited from class java.lang.Number

        byteValue, shortValue
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NaN

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

        public static final RatNum ZERO
        A constant holding a zero value of type RatNum
    • Constructor Detail

      • RatNum

        public RatNum​(int n)
        Parameters:
        n - The value of the new RatNum.
        Spec.effects:
        Constructs a new RatNum = n.
      • RatNum

        public RatNum​(int n,
                      int d)
        Parameters:
        n - The numerator of the new RatNum.
        d - The denominator of the new RatNum.
        Spec.effects:
        If d = 0, constructs a new RatNum = NaN. Else constructs a new RatNum = (n / d).
    • Method Detail

      • isNaN

        public boolean isNaN()
        Returns true if this is NaN
        Returns:
        true iff this is NaN (not-a-number)
      • isNegative

        public boolean isNegative()
        Returns true if this is negative.
        Returns:
        true iff this < 0.
      • isPositive

        public boolean isPositive()
        Returns true if this is positive.
        Returns:
        true iff this > 0.
      • compareTo

        public int compareTo​(RatNum rn)
        Compares two RatNums.
        Specified by:
        compareTo in interface java.lang.Comparable<RatNum>
        Parameters:
        rn - The RatNum to be compared.
        Returns:
        a negative number if this < rn, 0 if this = rn, a positive number if this > rn.
        Spec.requires:
        rn != null
      • doubleValue

        public double doubleValue()
        Approximates the value of this rational.
        Specified by:
        doubleValue in class java.lang.Number
        Returns:
        a double approximation for this. Note that "NaN" is mapped to Double.NaN, and the Double.NaN value is treated in a special manner by several arithmetic operations, such as the comparison and equality operators. See the Java Language Specification, section 4.2.3, for more details.
      • intValue

        public int intValue()
        Returns an integer approximation for this. The rational number is rounded to the nearest integer.
        Specified by:
        intValue in class java.lang.Number
      • floatValue

        public float floatValue()
        Returns a float approximation for this. This method is specified by our superclass, Number.
        Specified by:
        floatValue in class java.lang.Number
      • longValue

        public long longValue()
        Returns a long approximation for this. This method is specified by our superclass, Number. The value returned is rounded to the nearest long.
        Specified by:
        longValue in class java.lang.Number
      • negate

        public RatNum negate()
        Returns the additive inverse of this RatNum.
        Returns:
        a Rational equal to (0 - this).
      • add

        public RatNum add​(RatNum arg)
        Addition operation.
        Parameters:
        arg - The other value to be added.
        Returns:
        a RatNum equal to (this + arg). If either argument is NaN, then returns NaN.
        Spec.requires:
        arg != null
      • sub

        public RatNum sub​(RatNum arg)
        Subtraction operation.
        Parameters:
        arg - The value to be subtracted.
        Returns:
        a RatNum equal to (this - arg). If either argument is NaN, then returns NaN.
        Spec.requires:
        arg != null
      • mul

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

        public RatNum div​(RatNum arg)
        Division operation.
        Parameters:
        arg - The divisor.
        Returns:
        a RatNum equal to (this / arg). If arg is zero, or if either argument is NaN, then returns NaN.
        Spec.requires:
        arg != null
      • 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 return.
      • 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 if and only if 'obj' is an instance of a RatNum and 'this' and 'obj' represent the same rational number. Note that NaN = NaN for RatNums.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String representing this, in reduced terms. The returned string will either be "NaN", or it will take on either of the forms "N" or "N/M", where N and M are both integers in decimal notation and M != 0.
      • valueOf

        public static RatNum valueOf​(java.lang.String ratStr)
        Makes a RatNum from a string describing it.
        Parameters:
        ratStr - A string of the format described in the @spec.requires clause.
        Returns:
        NaN if ratStr = "NaN". Else returns a RatNum r = ( N / M ), letting M be 1 in the case where only "N" is passed in.
        Spec.requires:
        'ratStr' is an instance of a string, with no spaces, of the form:
        • "NaN"
        • "N/M", where N and M are both integers in decimal notation, and M != 0, or
        • "N", where N is an integer in decimal notation.