Java Reference
In-Depth Information
figure 3.13
BigRational add
,
subtract
,
multiply
,
divide
1
public BigRational add( BigRational other )
2
{
3
BigInteger newNumerator =
4
num.multiply( other.den ).add(
5
other.num.multiply( den ) );
6
BigInteger newDenominator = den.multiply( other.den );
7
8
return new BigRational( newNumerator, newDenominator );
9
}
10
11
public BigRational subtract( BigRational other )
12
{
13
return add( other.negate( ) );
14
}
15
16
public BigRational multiply( BigRational other )
17
{
18
BigInteger newNumer = num.multiply( other.num );
19
BigInteger newDenom = den.multiply( other.den );
20
21
return new BigRational( newNumer, newDenom );
22
}
23
24
public BigRational divide( BigRational other )
25
{
26
BigInteger newNumer = num.multiply( other.den );
27
BigInteger newDenom = den.multiply( other.num );
28
29
return new BigRational( newNumer, newDenom );
30
}
Observe that the
BigRational
class has no mutators: routines such as add
simply return a new
BigRational
that represents a sum. Thus
BigRational
is an
immutable type.
packages
3.8
Packages
are used to organize similar classes. Each package consists of a set of
classes. Two classes in the same package have slightly fewer visibility restric-
tions among themselves than they would if they were in different packages.
Java provides several predefined packages, including
java.io
,
java.lang
,
and
java.util
. The
java.lang
package includes the classes
Integer
,
Math
,
String
, and
System
, among others. Some of the classes in the
java.util
package
A
package
is used
to organize a col-
lection of classes.
Search WWH ::
Custom Search