org.niffty
Class IntRatio

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

public class IntRatio
extends java.lang.Object

IntRatio is a mathematical class for representing a rational number. The ratio is alway kept in its simplified form with the denominator positive. You can modify the value of an IntRatio and do arithmetic on it. A Rational, on the other hand, holds a numerator and denominator but is immutable.

See Also:
Rational

Constructor Summary
IntRatio(int numerator, int denominator)
          Creates new IntRatio with the given numerator and denominator.
IntRatio(IntRatio r)
          Creates new IntRatio from the given IntRatio.
IntRatio(Rational r)
          Creates new IntRatio from the given Rational.
 
Method Summary
 void add(IntRatio r)
          Set this ratio to this + r
 void add(Rational r)
          Set this ratio to this + r
 int compareTo(java.lang.Object object)
          Compare this ratio 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.
static int gcd(int a, int b)
          Return the greatest common denomitator of abs(a) and abs(b).
 int getD()
          Return the denominator
 int getN()
          Return the numerator
 void set(int numerator, int denominator)
          Set this ratio with the given numerator and denominator.
 void set(IntRatio r)
          Set the value of this IntRatio to the given IntRatio.
 void set(Rational r)
          Set the value of this IntRatio to the given Ratioanal.
 void sub(IntRatio r)
          Set this ratio to this - r
 void sub(Rational r)
          Set this ratio to this - r
 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

IntRatio

public IntRatio(int numerator,
                int denominator)
Creates new IntRatio with the given numerator and denominator.

Throws:
java.lang.ArithmeticException - if denominator is zero.

IntRatio

public IntRatio(IntRatio r)
Creates new IntRatio from the given IntRatio.


IntRatio

public IntRatio(Rational r)
Creates new IntRatio from the given Rational.

Method Detail

set

public void set(int numerator,
                int denominator)
Set this ratio with the given numerator and denominator.

Throws:
java.lang.ArithmeticException - if denominator is zero.

set

public void set(IntRatio r)
Set the value of this IntRatio to the given IntRatio.


set

public void set(Rational r)
Set the value of this IntRatio to the given Ratioanal.


getN

public int getN()
Return the numerator


getD

public int getD()
Return the denominator


add

public void add(IntRatio r)
Set this ratio to this + r


sub

public void sub(IntRatio r)
Set this ratio to this - r


add

public void add(Rational r)
Set this ratio to this + r


sub

public void sub(Rational r)
Set this ratio to this - r


compareTo

public int compareTo(java.lang.Object object)
Compare this ratio 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.


gcd

public static int gcd(int a,
                      int b)
Return the greatest common denomitator of abs(a) and abs(b). If a is zero, return abs(b) or if b is zero, return abs(a). (If both are zero, return zero.)


toString

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

Overrides:
toString in class java.lang.Object