ptolemy.math
Class Fraction

java.lang.Object
  extended byptolemy.math.Fraction

public class Fraction
extends java.lang.Object

A class for representing fractions. Fractions are immutable and maintained in lowest terms, with a positive denominator. Thus, 1/2 and 2/4 are interpreted as different names for the same number. Any fraction with the value zero is expressed as 0/1 in lowest terms.

This class only represents fractions with a determined value, so fractions with a zero in the denominator are not allowed (including 0/0).

Since:
Ptolemy II 0.2
Version:
$Id: Fraction.java,v 1.29 2004/04/13 03:45:22 cxh Exp $
Author:
Stephen Neuendorffer
Accepted Rating:
Yellow (cxh)
Proposed Rating:
Green (neuendor)

Field Summary
static Fraction ZERO
           
 
Constructor Summary
Fraction()
          Create a new fraction with the value zero (0/1).
Fraction(Fraction f)
          Create a new fraction with the same value as the given fraction.
Fraction(int i)
          Create a new fraction with the value i/1.
Fraction(int Numerator, int Denominator)
          Create a new fraction in lowest terms with the value Numerator/Denominator.
 
Method Summary
protected  void _simplify()
          Reduce the fraction to lowest terms by dividing the Numerator and Denominator by their Greatest Common Divisor.
 Fraction add(Fraction b)
          Add this fraction to the given fraction.
 Fraction divide(Fraction b)
          Divide this fraction by the given fraction.
 boolean equals(java.lang.Object b)
          Compare this fraction with the given object.
 int getDenominator()
          Return the denominator of this fraction.
 int getNumerator()
          Return the numerator of this fraction.
 Fraction inverse()
          Find the multiplicative inverse of this fraction.
static int lcm(int u, int v)
          Finds the least common multiple(LCM) of two integers.
 Fraction multiply(Fraction b)
          Multiply this fraction by the given fraction.
 Fraction negate()
          Find the additive inverse of this fraction.
 Fraction subtract(Fraction b)
          Subtract the given fraction from this fraction.
 java.lang.String toString()
          Convert the fraction to a readable string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static Fraction ZERO
Constructor Detail

Fraction

public Fraction()
Create a new fraction with the value zero (0/1).


Fraction

public Fraction(int i)
Create a new fraction with the value i/1.


Fraction

public Fraction(int Numerator,
                int Denominator)
Create a new fraction in lowest terms with the value Numerator/Denominator.

Throws:
java.lang.ArithmeticException - If the denominator is specified to be zero.

Fraction

public Fraction(Fraction f)
Create a new fraction with the same value as the given fraction.

Method Detail

add

public Fraction add(Fraction b)
Add this fraction to the given fraction.

Returns:
The answer as another fraction in lowest terms.

divide

public Fraction divide(Fraction b)
Divide this fraction by the given fraction.

Returns:
The answer as another fraction in lowest terms.
Throws:
java.lang.ArithmeticException - If the fraction in the divisor has a value of zero.

equals

public boolean equals(java.lang.Object b)
Compare this fraction with the given object.

Returns:
True if the given object is a fraction and equal to this fraction.

getDenominator

public int getDenominator()
Return the denominator of this fraction.


getNumerator

public int getNumerator()
Return the numerator of this fraction.


inverse

public Fraction inverse()
Find the multiplicative inverse of this fraction.

Returns:
The answer as another fraction in lowest terms
Throws:
java.lang.ArithmeticException - If this fraction has a value of zero, in which case the multiplicative inverse cannot be represented.

lcm

public static int lcm(int u,
                      int v)
Finds the least common multiple(LCM) of two integers. If one of the numbers is negative, then the LCM is negative. If both of the numbers are negative, then the LCM is positive. the LCM is least in terms of absolute value.


multiply

public Fraction multiply(Fraction b)
Multiply this fraction by the given fraction.

Returns:
The answer as another fraction in lowest terms.

negate

public Fraction negate()
Find the additive inverse of this fraction.

Returns:
The answer as another fraction in lowest terms

subtract

public Fraction subtract(Fraction b)
Subtract the given fraction from this fraction.

Returns:
The answer as another fraction in lowest terms

toString

public java.lang.String toString()
Convert the fraction to a readable string.


_simplify

protected void _simplify()
Reduce the fraction to lowest terms by dividing the Numerator and Denominator by their Greatest Common Divisor. In addition the fraction is put in standard form (denominator greater than zero).