Java Reference
In-Depth Information
The method
dequeue()
that removes and returns the element from the
queue.
■
The method
empty()
that returns true if the queue is empty.
■
■
The method
getSize()
that returns the size of the queue.
Draw an UML diagram for the class. Implement the class with the initial array
size set to 8. The array size will be doubled once the number of the elements
exceeds the size. After an element is removed from the beginning of the array,
you need to shift all elements in the array one position the left. Write a test
program that adds 20 numbers from 1 to 20 into the queue and removes these
numbers and displays them.
*10.11
(
Geometry: the
Circle2D
class
) Define the
Circle2D
class that contains:
Two
double
data fields named
x
and
y
that specify the center of the circle
with getter methods.
■
A data field
radius
with a getter method.
■
A no-arg constructor that creates a default circle with (
0
,
0
) for (
x
,
y
) and
1
for
radius
.
■
A constructor that creates a circle with the specified
x
,
y
, and
radius
.
■
A method
getArea()
that returns the area of the circle.
■
A method
getPerimeter()
that returns the perimeter of the circle.
■
A method
contains(double x, double y)
that returns
true
if the
specified point (
x
,
y
) is inside this circle (see Figure 10.21a).
■
A method
contains(Circle2D circle)
that returns
true
if the speci-
fied circle is inside this circle (see Figure 10.21b).
■
A method
overlaps(Circle2D circle)
that returns
true
if the speci-
fied circle overlaps with this circle (see Figure 10.21c).
■
p
(a)
(b)
(c)
F
IGURE
10.21
(a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle
overlaps another circle.
Draw the UML diagram for the class and then implement the class. Write a test
program that creates a
Circle2D
object
c1
(
new Circle2D(2, 2, 5.5)
),
displays its area and perimeter, and displays the result of
c1.contains(3,
3)
,
c1.contains(new Circle2D(4, 5, 10.5))
, and
c1.overlaps(new
Circle2D(3, 5, 2.3))
.
***10.12
(
Geometry: the
Triangle2D
class
) Define the
Triangle2D
class that contains:
Three points named
p1
,
p2
, and
p3
of the type
MyPoint
with getter and
setter methods.
MyPoint
is defined in Programming Exercise 10.4.
■
A no-arg constructor that creates a default triangle with the points (
0
,
0
), (
1
,
1
), and (
2
,
5
).
■
A constructor that creates a triangle with the specified points.
■
A method
getArea()
that returns the area of the triangle.
■
A method
getPerimeter()
that returns the perimeter of the triangle.
■
A method
contains(MyPoint p)
that returns
true
if the specified point
p
is inside this triangle (see Figure 10.22a).
■
Search WWH ::
Custom Search