hw3
Class RatNum

java.lang.Object
  extended by java.lang.Number
      extended by hw3.RatNum
All Implemented Interfaces:
Serializable, Comparable<RatNum>

public final class RatNum
extends Number
implements 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
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
RatNum(int n)
           
RatNum(int n, int d)
           
 
Method Summary
 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(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.
 String toString()
           
static RatNum valueOf(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)
Effects:
Constructs a new RatNum = n.

RatNum

public RatNum(int n,
              int d)
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 Comparable<RatNum>
Returns:
a negative number if this < rn, 0 if this = rn, a positive number if this > rn.
Requires:
rn != null

doubleValue

public double doubleValue()
Approximates the value of this rational.

Specified by:
doubleValue in class 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 Number

floatValue

public float floatValue()
Returns a float approximation for this. This method is specified by our superclass, Number.

Specified by:
floatValue in class 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 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.

Returns:
a RatNum equal to (this + arg). If either argument is NaN, then returns NaN.
Requires:
arg != null

sub

public RatNum sub(RatNum arg)
Subtraction operation.

Returns:
a RatNum equal to (this - arg). If either argument is NaN, then returns NaN.
Requires:
arg != null

mul

public RatNum mul(RatNum arg)
Multiplication operation.

Returns:
a RatNum equal to (this * arg). If either argument is NaN, then returns NaN.
Requires:
arg != null

div

public RatNum div(RatNum arg)
Division operation.

Returns:
a RatNum equal to (this / arg). If arg is zero, or if either argument is NaN, then returns NaN.
Requires:
arg != null

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 RatNum and 'this' and 'obj' represent the same rational number. Note that NaN = NaN for RatNums.

toString

public String toString()
Overrides:
toString in class 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(String ratStr)
Makes a RatNum from a string describing it.

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.
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.