Java Reference
In-Depth Information
Java provides data types for integers and floating-point numbers, but not for rational num-
bers. This section shows how to design a class to represent rational numbers.
Since rational numbers share many common features with integers and floating-point num-
bers, and Number is the root class for numeric wrapper classes, it is appropriate to define
Rational as a subclass of Number . Since rational numbers are comparable, the Rational
class should also implement the Comparable interface. Figure 13.8 illustrates the Rational
class and its relationship to the Number class and the Comparable interface.
java.lang.Number
1
Rational
java.lang.Comparable<Rational>
1
Add, Subtract, Multiply, Divide
Rational
-numerator: long
-denominator: long
The numerator of this rational number.
The denominator of this rational number.
+Rational()
Creates a rational number with numerator 0 and denominator 1.
Creates a rational number with a specified numerator and
denominator.
Returns the numerator of this rational number.
Returns the denominator of this rational number.
Returns the addition of this rational number with another.
+Rational(numerator: long,
denominator: long)
+getNumerator(): long
+getDenominator(): long
+add(secondRational: Rational):
Rational
+subtract(secondRational:
Rational): Rational
+multiply(secondRational:
Rational): Rational
+divide(secondRational:
Rational): Rational
+toString(): String
Returns the subtraction of this rational number with another.
Returns the multiplication of this rational number with another.
Returns the division of this rational number with another.
Returns a string in the form “numerator/denominator.” Returns
the numerator if denominator is 1.
Returns the greatest common divisor of n and d.
-gcd(n: long, d: long): long
F IGURE 13.8
The properties, constructors, and methods of the Rational class are illustrated in UML.
A rational number consists of a numerator and a denominator. There are many equivalent
rational numbers—for example, 1/3 = 2/6 = 3/9 = 4/12 . The numerator and the denomi-
nator of 1/3 have no common divisor except 1 , so 1/3 is said to be in lowest terms .
To reduce a rational number to its lowest terms, you need to find the greatest common
divisor (GCD) of the absolute values of its numerator and denominator, then divide both the
numerator and denominator by this value. You can use the method for computing the GCD of
two integers n and d , as suggested in Listing 5.9, GreatestCommonDivisor.java. The numera-
tor and denominator in a Rational object are reduced to their lowest terms.
As usual, let us first write a test program to create two Rational objects and test its meth-
ods. Listing 13.12 is a test program.
L ISTING 13.12
TestRationalClass.java
1 public class TestRationalClass {
2
/** Main method */
3
public static void main(String[] args) {
4
// Create and initialize two rational numbers r1 and r2
5
Rational r1 = new Rational( 4 , 2 );
create a Rational
create a Rational
6
Rational r2 = new Rational( 2 , 3 );
7
8
// Display results
 
 
Search WWH ::




Custom Search