org.niffty
Class Rational

java.lang.Object
  |
  +--org.niffty.Rational

public class Rational
extends java.lang.Object

A Rational in an immutable object for representing a rational number with a numerator and denominator. The ration is always kept in its simplified form, with the denominator positive. A Rational is immutable. Therefore you can pass a Rational to another method and know that it won't modify it. An IntRatio, on the other hand, can be modified and can do more arithmetic.

See Also:
IntRatio

Constructor Summary
Rational(int numerator, int denominator)
          Creates new Rational with the given numerator and denominator.
Rational(IntRatio r)
          Creates new Rational with the numerator and denominator from the given IntRatio.
 
Method Summary
 int compareTo(java.lang.Object object)
          Compare this Rational to object and return -1 if this is less than object, 1 if this is greater than object, or zero if they are equal.
 double doubleValue()
          Return numerator/denominator as a double value.
 boolean equals(java.lang.Object object)
          Return true of the result of compareTo for the object is zero.
 int getD()
          Return the denominator
 int getN()
          Return the numerator
 java.lang.String toString()
          Returns string as "numerator/denominator"
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational(int numerator,
                int denominator)
Creates new Rational with the given numerator and denominator. If denominator is zero (which doesn't make sense), this sets numerator to zero and denominator to 1.


Rational

public Rational(IntRatio r)
Creates new Rational with the numerator and denominator from the given IntRatio.

Method Detail

compareTo

public int compareTo(java.lang.Object object)
Compare this Rational to object and return -1 if this is less than object, 1 if this is greater than object, or zero if they are equal.

Throws:
java.lang.ClassCastException - if object is not an IntRatio or Rational.

equals

public boolean equals(java.lang.Object object)
Return true of the result of compareTo for the object is zero. If the object is not an IntRatio or Rational, this uses the default Object.equals().

Overrides:
equals in class java.lang.Object
See Also:
compareTo(java.lang.Object)

doubleValue

public final double doubleValue()
Return numerator/denominator as a double value.


toString

public java.lang.String toString()
Returns string as "numerator/denominator"

Overrides:
toString in class java.lang.Object

getN

public final int getN()
Return the numerator


getD

public final int getD()
Return the denominator