Java Reference
In-Depth Information
Sections 15.4-15.8
*15.7
(
Enable
GeometricObject
comparable
) Modify the
GeometricObject
class to implement the
Comparable
interface, and define a static
max
method
in the
GeometricObject
class for finding the larger of two
GeometricObject
objects. Draw the UML diagram and implement the new
GeometricObject
class. Write a test program that uses the
max
method to find
the larger of two circles and the larger of two rectangles.
*15.8
(
The
ComparableCircle
class
) Define a class named
ComparableCircle
that extends
Circle
and implements
Comparable
. Draw the UML diagram and
implement the
compareTo
method to compare the circles on the basis of area.
Write a test class to find the larger of two instances of
ComparableCircle
objects.
*15.9
(
The
Colorable
interface
) Design an interface named
Colorable
with a
void
method named
howToColor()
. Every class of a colorable object must
implement the
Colorable
interface. Design a class named
Square
that
extends
GeometricObject
and implements
Colorable
. Implement
howToColor
to display the message
Color all four sides
.
Draw a UML diagram that involves
Colorable
,
Square
, and
GeometricObject
. Write a test program that creates an array of five
GeometricObject
s. For each object in the array, invoke its
howToColor
method if it is colorable.
*15.10
(
Revise the
MyStack
class
) Rewrite the
MyStack
class in Listing 11.9 to per-
form a deep copy of the
list
field.
*15.11
(
Enable
Circle
comparable
) Rewrite the
Circle
class in Listing 15.2 to
extend
GeometricObject
and implement the
Comparable
interface. Over-
ride the
equals
method in the
Object
class. Two
Circle
objects are equal if
their radii are the same. Draw the UML diagram that involves
Circle
,
GeometricObject
, and
Comparable
.
*15.12
(
Enable
Rectangle
comparable
) Rewrite the
Rectangle
class in Listing 15.3
to extend
GeometricObject
and implement the
Comparable
interface. Over-
ride the
equals
method in the
Object
class. Two
Rectangle
objects are
equal if their areas are the same. Draw the UML diagram that involves
Rectangle
,
GeometricObject
, and
Comparable
.
VideoNote
Redesign the
Rectangle
class
*15.13
(
The
Octagon
class
) Write a class named
Octagon
that extends
GeometricObject
and implements the
Comparable
and
Cloneable
inter-
faces. Assume that all eight sides of the octagon are of equal size. The area can
be computed using the following formula:
area
=
(2
+
4/
2
2)
*
side
*
side
Draw the UML diagram that involves
Octagon
,
GeometricObject
,
Comparable
, and
Cloneable
. Write a test program that creates an
Octagon
object with side value
5
and displays its area and perimeter. Create a new object
using the
clone
method and compare the two objects using the
compareTo
method.
*15.14
(
Sum the areas of geometric objects
) Write a method that sums the areas of all
the geometric objects in an array. The method signature is:
public static double
sumArea(GeometricObject[] a)
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.