Java Reference
In-Depth Information
Write a test program that creates an array of four objects (two circles and two
rectangles) and computes their total area using the sumArea method.
*13.13 ( 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 13.9
*13.14
( Demonstrate the benefits of encapsulation ) Rewrite the Rational class in
Listing 13.13 using a new internal representation for the numerator and denomina-
tor. 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.
*13.15
( Use BigInteger for the Rational class ) Redesign and implement the
Rational class in Listing 13.13 using BigInteger for the numerator and
denominator.
*13.16
( Create a rational-number calculator ) Write a program similar to Listing 7.9,
Calculator.java. Instead of using integers, use rationals, as shown in Figure 13.10a.
You will need to use the split method in the String class, introduced in
Section 10.10.3, 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 13.10
(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.
*13.17
( Math: The Complex class ) A complex nu mbe r is a number in the form a
+
bi ,
where a and b are real numbers and i is
1. 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:
2-
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
-
+
 
 
Search WWH ::




Custom Search