Java Reference
In-Depth Information
*15.15
(
Enable the
Course
class cloneable
) Rewrite the
Course
class in Listing 10.6
to add a
clone
method to perform a deep copy on the
students
field.
Section 15.9
*15.16
(
Demonstrate the benefits of encapsulation
) Rewrite the
Rational
class in
Listing 15.13 using a new internal representation for the numerator and denom-
inator. Create an array of two integers as follows:
private long
[] r =
new long
[
2
];
Use
r[0]
to represent the numerator and
r[1]
to represent the denominator.
The signatures of the methods in the
Rational
class are not changed, so a
client application that uses the previous
Rational
class can continue to use this
new
Rational
class without being recompiled.
*15.17
(
Use
BigInteger
for the
Rational
class
) Redesign and implement the
Rational
class in Listing 15.11 using
BigInteger
for the numerator and
denominator.
*15.18
(
Create a rational-number calculator
) Write a program similar to Listing 9.5, Cal-
culator.java. Instead of using integers, use rationals, as shown in Figure 15.12a.
You will need to use the
split
method in the
String
class, introduced in Section
9.2.6, Converting, Replacing, and Splitting Strings, to retrieve the numerator
string and denominator string, and convert strings into integers using the
Integer.parseInt
method.
y-axis
2 + 3i
x-axis
3 - 2i
(a)
(b)
F
IGURE
15.12
(a) The program takes three arguments (operand1, operator, and operand2) from the command line and
displays the expression and the result of the arithmetic operation. (b) A complex number can be interpreted as a point in
a plane.
*15.19
(
Math: The
Complex
class
) A complex nu
mbe
r is a number in the form
where
a
and
b
are real numbers and
i
is The numbers
a
and
b
are known
as the real part and imaginary part of the complex number, respectively. You can
perform addition, subtraction, multiplication, and division for complex numbers
using the following formulas:
a
+
bi
,
2-
1.
a
+
bi
+
c
+
di
=
(
a
+
c
)
+
(
b
+
d
)
i
a
+
bi
-
(
c
+
di
)
=
(
a
-
c
)
+
(
b
-
d
)
i
(
a
+
bi
)
*
(
c
+
di
)
=
(
ac
-
bd
)
+
(
bc
+
ad
)
i
bd
)/(
c
2
d
2
)
ad
)
i
/(
c
2
d
2
)
(
a
+
bi
)/(
c
+
di
)
=
(
ac
+
+
+
(
bc
-
+