Package ptolemy.math

Class 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$
    Author:
    Stephen Neuendorffer, Adam Cataldo
    Pt.AcceptedRating:
    Yellow (cxh)
    Pt.ProposedRating:
    Green (neuendor)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Fraction ZERO
      The value of zero as a Fraction.
    • Constructor Summary

      Constructors 
      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 Summary

      All 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.
      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.
      boolean greaterThan​(Fraction testInput)
      Test if this Fraction is greater than the input.
      int hashCode()
      Return a hash code value for 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.
      double toDouble()
      Convert the fraction to a double.
      float toFloat()
      Convert the fraction to a float.
      java.lang.String toString()
      Convert the fraction to a readable string.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ZERO

        public 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

      • 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.
        Parameters:
        i - The numerator.
      • Fraction

        public 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.
      • Fraction

        public Fraction​(Fraction f)
        Create a new fraction with the same value as the given fraction.
        Parameters:
        f - The given Fraction.
    • Method Detail

      • add

        public 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.
      • divide

        public 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.
      • equals

        public boolean equals​(java.lang.Object b)
        Compare this fraction with the given object.
        Overrides:
        equals in class java.lang.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.
      • greaterThan

        public 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.
      • hashCode

        public 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:
        hashCode in class java.lang.Object
        Returns:
        A hash code value for this Coordinate
      • 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
      • toDouble

        public double toDouble()
        Convert the fraction to a double.
        Returns:
        The double value.
      • toFloat

        public float toFloat()
        Convert the fraction to a float.
        Returns:
        The float value.
      • toString

        public java.lang.String toString()
        Convert the fraction to a readable string.
        Overrides:
        toString in class java.lang.Object
      • _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).