8.1. Using Fractions

Problem

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.

Solution

Use Jakarta 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( ).

Discussion

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( ) );
Expression to be evaluated with Fraction

Figure 8-1. Expression to be evaluated with Fraction

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"

Warning

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

abs( )

Returns the absolute value of a Fraction

add(Fraction fraction)

Adds two Fraction objects together

subtract(Fraction fraction)

Subtracts the parameter from the current Fraction

multiplyBy(Fraction fraction)

Multiplies the parameter by the current Fraction

divideBy(Fraction fraction)

Divides the current Fraction by the parameter

reduce( )

Reduces the Fraction to the smallest denominator

negate( )

Returns -1 * Fraction

invert( )

Swaps the numerator and denominator

getNumerator( )

Returns the numerator

getDenominator( )

Returns the denominator

getProperNumerator( )

Returns the proper numerator

getProperWhole( )

Returns the proper whole number

pow( )

Raises a Fraction to the specified power

See Also

For more information about downloading Commons Lang, see Recipe 1.1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.8