com.marringtons.number
Class Fraction

java.lang.Object
  extended bycom.marringtons.number.Fraction
All Implemented Interfaces:
Comparable

public class Fraction
extends Object
implements Comparable

This is an immutable class representing fractions as 3 longs. Fractions are always maintained in reduced form. Fractions are truly magic things. 1/3 is way more accurate than 0.3333333333333. It is also easier to say and understand. Fractions multiply and divide way more quickly than floating point, but add more slowly (if denominators don't match). Have fun.

Author:
Paul Marrington

Field Summary
 int after
          Set by string constructor to the first non-space character after the number parsed.
 long denominator
          Denominator for the fractional number (the 3 in 1-2/3)
 boolean empty
          Set by string constructor if the string did not contain a fractional number.
 long numerator
          Numerator for the fractional number (the 2 in 2/3)
 
Constructor Summary
Fraction(long wholePart, long numerator, long denominator)
          Constructor must fill final components of the fraction.
Fraction(String string)
          Constructor to create fraction from a string.
 
Method Summary
 int compareTo(Object other)
           
 Fraction divide(Fraction by)
          Divide one fraction by another.
 boolean equals(Object other)
           
static long greatestCommonDenominator(long numerator, long denominator)
           
 int hashCode()
           
 Fraction inverse()
          Invert the fraction (2/3 becomes 1-1/3).
 Fraction minus(Fraction less)
          Subtraction ine Fraction from another.
 Fraction negative()
          Negate a fraction
 Fraction plus(Fraction add)
          Add two fractions together.
 Fraction times(Fraction by)
          Multiply two fractions.
 String toHTML()
          String representation of a fractional number - making fractional part a superscript.
 String toString()
          String representation of a fractional number is 1, 1-1/3 or 2/5.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

numerator

public final long numerator
Numerator for the fractional number (the 2 in 2/3)


denominator

public final long denominator
Denominator for the fractional number (the 3 in 1-2/3)


empty

public final transient boolean empty
Set by string constructor if the string did not contain a fractional number.
 String myInput = "1 2/3";
 Fraction fraction = new Fraction( myInput);
 if (fraction.empty) Log( "Please enter a valid fraction");
 


after

public final transient int after
Set by string constructor to the first non-space character after the number parsed.
 String myInput = "1 2/3 abc";
 Fraction fraction = new Fraction( myInput);
 check( myInput.substring( fraction.after).equals( "abc");
 

Constructor Detail

Fraction

public Fraction(long wholePart,
                long numerator,
                long denominator)
Constructor must fill final components of the fraction.

Parameters:
wholePart - The 1 in 1-2/3
numerator - The 2 in 1-2/3
denominator - The 3 in 1-2/3

Fraction

public Fraction(String string)
Constructor to create fraction from a string. The string can be a fraction with or without a whole part or a decimal. If the string

Parameters:
string -
Method Detail

greatestCommonDenominator

public static long greatestCommonDenominator(long numerator,
                                             long denominator)
Parameters:
numerator - Top part of the fraction
denominator - Lower part of the fraction
Returns:
greatest common denominator - to reduce fraction to it's best value.

toString

public String toString()
String representation of a fractional number is 1, 1-1/3 or 2/5.

Returns:
String representation.
See Also:
Object.toString()

toHTML

public String toHTML()
String representation of a fractional number - making fractional part a superscript.

Returns:
HTML representation.
See Also:
Object.toString()

compareTo

public int compareTo(Object other)
Specified by:
compareTo in interface Comparable
See Also:
Comparable.compareTo(java.lang.Object)

negative

public Fraction negative()
Negate a fraction

Returns:
a fraction representing the negative of this one.

inverse

public Fraction inverse()
Invert the fraction (2/3 becomes 1-1/3).

Returns:
Inverse of fraction.

plus

public Fraction plus(Fraction add)
Add two fractions together.

Parameters:
add - Fraction to add.
Returns:
Normalised sum of both fractions.

minus

public Fraction minus(Fraction less)
Subtraction ine Fraction from another.

Parameters:
less - Fraction to subtract from this one.
Returns:
Normalised difference between both fractions.

times

public Fraction times(Fraction by)
Multiply two fractions.

Parameters:
by - Fraction to multiply by.
Returns:
Normalised multiplication of both fractions.

divide

public Fraction divide(Fraction by)
Divide one fraction by another.

Parameters:
by - Fraction to divide with.
Returns:
Normalised result of fractional division.

equals

public boolean equals(Object other)
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
See Also:
Object.hashCode()


Copyright © 2005 Paul Marrington http://library.marringtons.com