You need to work with fractions supplied by the user, such as 3 4/5 and 134/21. Your application needs to parse, multiply, and reduce fractions.
Use Commons Lang's Fraction
class to parse and manipulate fractions. The following code demonstrates
the parsing of a String containing a
fraction:
import org.apache.commons.lang.math.Fraction; String userInput = "23 31/37"; Fraction fraction = Fraction.getFraction( userInput ); double value = fraction.doubleValue( );
The String "23 31/37" is
converted to a double value of
23.837837. A Fraction object is created by calling the
Fraction.getFraction( ) method, and
double value of the Fraction object is obtained with fraction.doubleValue( ).
The Fraction class provides a
number of operations that can be used to simplify the following
expression to an improper fraction. The following code evaluates the
expression in Figure 8-1 using
Fraction:
import org.apache.commons.lang.math.Fraction; Fraction numer1 = Fraction.getFraction( 3, 4 ); Fraction numer2 = Fraction.getFraction( 51, 3509 ); Fraction numerator = numer1.multiplyBy( numer2 ); Fraction denominator = Fraction.getFraction( 41, 59 ); Fraction fraction = numerator.divideBy( denominator ); Fraction result = fraction.reduce( ); System.out.println( "as Fraction: " + result.reduce( ).toString( ) ); System.out.println( "as double: " + result.doubleValue( ) );
The previous example creates an instance of Fraction by calling the static getFraction(int numerator, int denominator)
method. Fraction objects are then multiplied and divided with the
multiplyBy( ) and divideBy( ) methods of Fraction. And, the final call to reduce( ) reduces the Fraction to the smallest possible denominator.
This example executes and prints the following output to the
console:
Expression as Fraction: 9027/575476 Expression as double: 0.015686145034719084
An improper fraction is a fraction such that X/Y > 1 (i.e.,
"135/23" or "3/2"). Fraction provides
the ability to convert improper fractions to proper fractions as
demonstrated in the following example:
import org.apache.commons.lang.math.Fraction; String userInput = "101/99"; String properString = Fraction.getFraction(userInput).toProperString( ); // properString is now "1 2/99"
Fraction does not
automatically reduce contents, and it is important to call reduce( ) before performing any arithmetic
with the Fraction class to reduce
the risk of overflow. For example, Fraction.getFraction( 10000, 100000 ).pow( 6
) should equal 1.0E-6,
but, because Fraction simply
multiplies each numerator and denominator without reducing the
fraction, the result of this statement will be 1.0. When raised to the power of 6, the Fraction object quickly becomes Fraction.getFraction(Integer.MAX_VALUE,
Integer.MAX_VALUE) or 1.0. Call reduce(
) liberally or you may have occasion to curse this
Fraction class.
Table 8-1 lists a sampling of
methods available on the Fraction
class.
Table 8-1. Methods on Commons Lang Fraction
|
Method |
Description |
|---|---|
|
|
Returns the absolute value of a |
|
|
Adds two |
|
|
Subtracts the parameter from the current |
|
|
Multiplies the parameter by the current |
|
|
Divides the current |
|
|
Reduces the |
|
|
Returns -1 * |
|
|
Swaps the numerator and denominator |
|
|
Returns the numerator |
|
|
Returns the denominator |
|
|
Returns the proper numerator |
|
|
Returns the proper whole number |
|
|
Raises a |
For more information about downloading Commons Lang, see Recipe 1.1.
