or
Sign in to turn on 1-Click ordering.
More Buying Choices
Have one to sell? Sell yours here
Jakarta Commons Cookbook
 
See larger image
 
Tell the Publisher!
I’d like to read this book on Kindle

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Jakarta Commons Cookbook [Paperback]

Timothy M. O'Brien

RRP: £34.50
Price: £29.33 & this item Delivered FREE in the UK with Super Saver Delivery. See details and conditions
You Save: £5.17 (15%)
  Special Offers Available
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
In stock but may require up to 2 additional days to deliver.
Dispatched from and sold by Amazon.co.uk. Gift-wrap available.
Only 1 left in stock--order soon (more on the way).
‹  Return to Product Overview

Product Description

Product Description

As a Java developer, you're always looking for tools to help in your web and application development. Maybe you've heard of the Jakarta Commons open source Java tools. The Jakarta Commons is an open source Jakarta subproject where developers create and maintain a collection of twenty-plus independent Java components and utilities that serve useful purposes. Jakarta Commons packages include utilities for web, XML, networking, building and testing applications, and some that help other packages work better together. Jakarta Commons packages are designed to be reusable. Each one is a time saver by itself, and when used in combination the results can be very powerful.

But in searching for information on the Jakarta Commons tools, you find it hard to locate documentation relevant to your needs. You don't have lots of time to spend searching for information on a specific Jakarta Commons tool to determine what it does and how to incorporate it in your code. If you are looking for a single source for clear information and samples on how to use the Jakarta Commons tools, then the Jakarta Commons Cookbook is for you.

The Jakarta Commons Cookbook summarizes each of the available Jakarta Commons packages and contains practical and efficient recipes for making the most out of the Jakarta Commons open source Java tools. You don't have to be an expert, since the book explains how and why to use a utility, pitfalls to avoid, and where to look for additional information on Jakarta Commons utilities. It introduces design possibilities and explores combining Jakarta Commons utilities in novel assemblies to create complex applications. The book offers detailed code samples and insider tips--making it a valuable resource whether you are an expert Java developer or a novice. If you want to quickly learn how to use Jakarta Commons timing-saving utilities or have an invaluable resource for Jakarta Commons questions and techniques, then the Jakarta Commons Cookbook is for you.

From the Publisher

This collection provides expert tips for using the utilities of the Java-based Jakarta Commons open source project. You don't have to be an expert, the book's solution-based format contains code examples for a wide variety of web, XML, network, testing, and application projects. If you want to learn how to use Jakarta Commons utilities to create powerful Java applications and tools, the Jakarta Commons Cookbook is for you.

About the Author

Tim O'Brien is an active committer in the Jakarta Commons, a sub-project of the Apache Software Foundation's Jakarta project. As a consultant, Tim tries to encourage the adoption of open-source software, and nudge organizations to view community participation as an essential strategy. In addition to his professional responsibilities, he is a Bass/Baritone who sings frequently in the Chicagoland area. Tim discovered programming on a Basic Four, TRS-80, and Commodore 64 in his hometown of Wellesley, Massachusetts; subsequently, studying Computer Engineering at the University of Virginia

Excerpted from Jakarta Commons Cookbook by Timothy M. O'Brien. Copyright © 2004. Reprinted by permission. All rights reserved.

CHAPTER 8 Math

8.0 Introduction
In recent years, Java has lost its reputation as a language suffering from serious performance problems. Although the debate still rages on and various benchmarks show conflicting results, improvements to the JVM, the compiler, and a more intelligent garbage collector have boosted performance to levels on par with C++. Java has never been the traditional language-of-choice for scientific computing, numerical methods, or high-performance computing, but, as performance improves, there are fewer reasons to avoid using Java for numerical computing. It is no longer inconceivable to consider Java when implementing systems involving complex mathematics, and Jakarta Commons contains two projects that provide some very basic math capabilities: Commons Lang and Commons Math.

The first four recipes in this chapter deal with math utilities found in Commons Lang. This includes a class that represents fractions, finding the minimum and maximum values in an array, representing a range of numbers, and convenient ways to retrieve different random variables. Commons Lang was introduced in Chapter 1, and instructions for downloading and installing Commons Lang can be found in Recipe 1.1. The remainder of this chapter deals with Jakarta Commons Math. Recipes involving Commons Math deal with complex numbers, the calculation of univariate statistics, solving a system of linear equations, and establishing a relationship between two independent variables. Instructions for downloading and installing Commons Math can be found in Recipe 8.5.

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( ) );

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.01568614503471908

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.

8.2 Finding the Maximum and Minimum in an Array

Problem
You need to retrieve the maximum and minimum values from a double[], float[],
long[], int[], short[], or byte[].

Solution
Use Commons Lang NumberUtils.max( ) and NumberUtils.min( ) to retrieve the minimum or maximum values from an array of primitives. The following code retrieves the minimum and maximum values from a double[]:

import org.apache.commons.lang.math.NumberUtils;

double[] array = {0.2, 0.4, 0.5, -3.0, 4.223, 4.226};

double max = NumberUtils.max( array ); // returns 4.226
double min = NumberUtils.min( array ); // returns -3.0

Discussion
NumberUtils.min( ) and NumberUtils.max( ) both accept double[], float[], long[],
int[], short[], and byte[]. If the array is empty or null, both NumberUtils.min( ) and
NumberUtils.max( ) will return an IllegalArgumentException.

Jakarta Commons Math also contains a class that can find the minimum and maximum value in a double[]. The following example uses the Max and Min classes from Commons Math to evaluate a double[]:

import org.apache.commons.math.stat.univariate.rank.Max;
import org.apache.commons.math.stat.univariate.rank.Min;

double[] array = {0.2, 0.4, 0.5, -3.0, 4.223, 4.226};

Max maximum = new Max( );
Min minimum = new Min( );

double max = maximum.evaluate( array, 0, array.length );
double min = minimum.evaluate( array, 0, array.length );

‹  Return to Product Overview

Amazon.co.uk Privacy Statement Amazon.co.uk Delivery Information Amazon.co.uk Returns & Exchanges