Package ptolemy.math
Class Fraction
- java.lang.Object
- 
- ptolemy.math.Fraction
 
- 
 public class Fraction extends java.lang.ObjectA 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$
- Author:
- Stephen Neuendorffer, Adam Cataldo
- Pt.AcceptedRating:
- Yellow (cxh)
- Pt.ProposedRating:
- Green (neuendor)
 
- 
- 
Constructor SummaryConstructors Constructor Description Fraction()Create a new fraction with the value zero (0/1).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.Fraction(Fraction f)Create a new fraction with the same value as the given fraction.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_simplify()Reduce the fraction to lowest terms by dividing the Numerator and Denominator by their Greatest Common Divisor.Fractionadd(Fraction b)Add this fraction to the given fraction.Fractiondivide(Fraction b)Divide this fraction by the given fraction.booleanequals(java.lang.Object b)Compare this fraction with the given object.intgetDenominator()Return the denominator of this fraction.intgetNumerator()Return the numerator of this fraction.booleangreaterThan(Fraction testInput)Test if this Fraction is greater than the input.inthashCode()Return a hash code value for this Fraction.Fractioninverse()Find the multiplicative inverse of this fraction.static intlcm(int u, int v)Finds the least common multiple(LCM) of two integers.Fractionmultiply(Fraction b)Multiply this fraction by the given fraction.Fractionnegate()Find the additive inverse of this fraction.Fractionsubtract(Fraction b)Subtract the given fraction from this fraction.doubletoDouble()Convert the fraction to a double.floattoFloat()Convert the fraction to a float.java.lang.StringtoString()Convert the fraction to a readable string.
 
- 
- 
- 
Field Detail- 
ZEROpublic static final Fraction ZERO The value of zero as a Fraction. It is arguable as to whether or not this is needed. It may reduce the number of object creations, and increase speed, depending how often a zero fraction is needed. This may become useful when this class is made into a Token.
 
- 
 - 
Constructor Detail- 
Fractionpublic Fraction() Create a new fraction with the value zero (0/1).
 - 
Fractionpublic Fraction(int i) Create a new fraction with the value i/1.- Parameters:
- i- The numerator.
 
 - 
Fractionpublic Fraction(int numerator, int denominator)Create a new fraction in lowest terms with the value Numerator/Denominator.- Parameters:
- numerator- The numerator.
- denominator- The denominator.
- Throws:
- java.lang.ArithmeticException- If the denominator is specified to be zero.
 
 - 
Fractionpublic Fraction(Fraction f) Create a new fraction with the same value as the given fraction.- Parameters:
- f- The given Fraction.
 
 
- 
 - 
Method Detail- 
addpublic Fraction add(Fraction b) Add this fraction to the given fraction.- Parameters:
- b- The given Fraction.
- Returns:
- The answer as another fraction in lowest terms.
 
 - 
dividepublic Fraction divide(Fraction b) Divide this fraction by the given fraction.- Parameters:
- b- 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.
 
 - 
equalspublic boolean equals(java.lang.Object b) Compare this fraction with the given object.- Overrides:
- equalsin class- java.lang.Object
- Returns:
- True if the given object is a fraction and equal to this fraction.
 
 - 
getDenominatorpublic int getDenominator() Return the denominator of this fraction.
 - 
getNumeratorpublic int getNumerator() Return the numerator of this fraction.
 - 
greaterThanpublic boolean greaterThan(Fraction testInput) Test if this Fraction is greater than the input.- Parameters:
- testInput- The input to compare against.
- Returns:
- True if this Fraction is greater than the input.
 
 - 
hashCodepublic int hashCode() Return a hash code value for this Fraction. This method returns the bitwise and of the hashcode of the denominator and the numerator.- Overrides:
- hashCodein class- java.lang.Object
- Returns:
- A hash code value for this Coordinate
 
 - 
inversepublic 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.
 
 - 
lcmpublic 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.
 - 
multiplypublic Fraction multiply(Fraction b) Multiply this fraction by the given fraction.- Returns:
- The answer as another fraction in lowest terms.
 
 - 
negatepublic Fraction negate() Find the additive inverse of this fraction.- Returns:
- The answer as another fraction in lowest terms
 
 - 
subtractpublic Fraction subtract(Fraction b) Subtract the given fraction from this fraction.- Returns:
- The answer as another fraction in lowest terms
 
 - 
toDoublepublic double toDouble() Convert the fraction to a double.- Returns:
- The double value.
 
 - 
toFloatpublic float toFloat() Convert the fraction to a float.- Returns:
- The float value.
 
 - 
toStringpublic java.lang.String toString() Convert the fraction to a readable string.- Overrides:
- toStringin class- java.lang.Object
 
 - 
_simplifyprotected 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).
 
- 
 
-