Java Reference
In-Depth Information
A method
getArea()
that returns the area of the rectangle.
■
A method
getPerimeter()
that returns the perimeter of the rectangle.
■
A method
contains(double x, double y)
that returns
true
if the
specified point (
x
,
y
) is inside this rectangle (see Figure 10.24a).
■
A method
contains(MyRectangle2D r)
that returns
true
if the speci-
fied rectangle is inside this rectangle (see Figure 10.24b).
■
A method
overlaps(MyRectangle2D r)
that returns
true
if the speci-
fied rectangle overlaps with this rectangle (see Figure 10.24c).
■
p
(a)
(b)
(c)
(d)
F
IGURE
10.24
A point is inside the rectangle. (b) A rectangle is inside another rectangle.
(c) A rectangle overlaps another rectangle. (d) Points are enclosed inside a rectangle.
Draw the UML diagram for the class and then implement the class. Write a test
program that creates a
MyRectangle2D
object
r1
(
new MyRectangle2D(2,
2, 5.5, 4.9)
), displays its area and perimeter, and displays the result of
r1.contains(3, 3)
,
r1.contains(new MyRectangle2D(4, 5, 10.5,
3.2))
, and
r1.overlaps(new MyRectangle2D(3, 5, 2.3, 5.4))
.
*10.14
(
The
MyDate
class
) Design a class named
MyDate
. The class contains:
The data fields
year
,
month
, and
day
that represent a date.
month
is
0-based, i.e.,
0
is for January.
■
A no-arg constructor that creates a
MyDate
object for the current date.
■
A constructor that constructs a
MyDate
object with a specified elapsed time
since midnight, January 1, 1970, in milliseconds.
■
A constructor that constructs a
MyDate
object with the specified year,
month, and day.
■
Three getter methods for the data fields
year
,
month
, and
day
, respectively.
■
■
A method named
setDate(long elapsedTime)
that sets a new date for
the object using the elapsed time.
Draw the UML diagram for the class and then implement the class. Write a
test program that creates two
MyDate
objects (using
new MyDate()
and
new
MyDate(34355555133101L)
) and displays their year, month, and day.
(
Hint
: The first two constructors will extract the year, month, and day from
the elapsed time. For example, if the elapsed time is
561555550000
milli-
seconds, the year is
1987
, the month is
9
, and the day is
18
. You may use the
GregorianCalendar
class discussed in Programming Exercise 9.5 to sim-
plify coding.)
*10.15
(
Geometry: the bounding rectangle
) A bounding rectangle is the minimum rec-
tangle that encloses a set of points in a two-dimensional plane, as shown in
Figure 10.24d. Write a method that returns a bounding rectangle for a set of
points in a two-dimensional plane, as follows:
public static
MyRectangle2D getRectangle(
double
[][] points)
Search WWH ::
Custom Search