Package hw4
Class RatNum
- java.lang.Object
-
- java.lang.Number
-
- hw4.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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RatNumadd(RatNum arg)Addition operation.intcompareTo(RatNum rn)Compares two RatNums.RatNumdiv(RatNum arg)Division operation.doubledoubleValue()Approximates the value of this rational.booleanequals(java.lang.Object obj)Standard equality operation.floatfloatValue()Returns a float approximation for this.inthashCode()Standard hashCode function.intintValue()Returns an integer approximation for this.booleanisNaN()Returns true if this is NaNbooleanisNegative()Returns true if this is negative.booleanisPositive()Returns true if this is positive.longlongValue()Returns a long approximation for this.RatNummul(RatNum arg)Multiplication operation.RatNumnegate()Returns the additive inverse of this RatNum.RatNumsub(RatNum arg)Subtraction operation.java.lang.StringtoString()static RatNumvalueOf(java.lang.String ratStr)Makes a RatNum from a string describing it.
-
-
-
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:
compareToin interfacejava.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:
doubleValuein classjava.lang.Number- Returns:
- a double approximation for this. Note that "NaN" is
mapped to
Double.NaN, and theDouble.NaNvalue 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:
intValuein classjava.lang.Number
-
floatValue
public float floatValue()
Returns a float approximation for this. This method is specified by our superclass, Number.- Specified by:
floatValuein classjava.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:
longValuein classjava.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:
hashCodein classjava.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:
equalsin classjava.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:
toStringin classjava.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.
-
-