Java Reference
In-Depth Information
*20.3
(
Guessing the capitals
) Rewrite Programming Exercise 8.37 to store the pairs
of states and capitals so that the questions are displayed randomly.
*20.4
(
Sort points in a plane
) Write a program that meets the following requirements:
Define a class named
Point
with two data fields
x
and
y
to represent a
point's
x
- and
y
-coordinates. Implement the
Comparable
interface for com-
paring the points on
x
-coordinates. If two points have the same
x
-coordinates,
compare their
y
-coordinates.
■
Define a class named
CompareY
that implements
Comparator<Point>
.
Implement the
compare
method to compare two points on their
y
-coordinates.
If two points have the same
y
-coordinates, compare their
x
-coordinates.
■
Randomly create
100
points and apply the
Arrays.sort
method to display
the points in increasing order of their
x
-coordinates and in increasing order
of their
y
-coordinates, respectively.
***20.5
(
Combine colliding bouncing balls
) The example in Section 20.7 displays mul-
tiple bouncing balls. Extend the example to detect collisions. Once two balls
collide, remove the later ball that was added to the pane and add its radius to
the other ball, as shown in Figure 20.17b. Use the
Suspend
button to suspend
the animation and the
Resume
button to resume the animation. Add a mouse
pressed handler that removes a ball when the mouse is pressed on the ball.
20.6
(
Use iterators on linked lists
) Write a test program that stores 5 million integers
in a linked list and test the time to traverse the list using an
iterator
vs. using
the
get(index)
method.
***20.7
(
Game: hangman
) Programming Exercise 7.35 presents a console version of the
popular hangman game. Write a GUI program that lets a user play the game. The
user guesses a word by entering one letter at a time, as shown in Figure 20.18.
If the user misses seven times, a hanging man swings. Once a word is finished,
the user can press the
Enter
key to continue to guess another word.
**20.8
(
Game: lottery
) Revise Programming Exercise 3.15 to add an additional $2,000
award if two digits from the user input are in the lottery number. (
Hint
: Sort
the three digits in the lottery number and three digits in the user input into two
lists, and use the
Collection
's
containsAll
method to check whether the
two digits in the user input are in the lottery number.)
Sections 20.8-20.10
***20.9
■
(
Remove the largest ball first
) Modify Listing 20.6, MultipleBallApp.java to
assign a random radius between 2 and 20 when a ball is created. When the
-
button is clicked, one of largest balls is removed.
20.10
(
Perform set operations on priority queues
) Create two priority queues,
{
"George"
,
"Jim"
,
"John"
,
"Blake"
,
"Kevin"
,
"Michael"
} and
{
"George"
,
"Katie"
,
"Kevin"
,
"Michelle"
,
"Ryan"
}, and find their
union, difference, and intersection.
*20.11
(
Match grouping symbols
) A Java program contains various pairs of grouping
symbols, such as:
Parentheses:
(
and
)
■
Braces:
{
and
}
■
Brackets:
[
and
]
Note that the grouping symbols cannot overlap. For example,
(a{b)}
is illegal.
Write a program to check whether a Java source-code file has correct pairs of
grouping symbols. Pass the source-code file name as a command-line argument.
■
Search WWH ::
Custom Search