Java Reference
In-Depth Information
10.
To play an audio, create an audio clip from the
URL
object for the audio source. You
can use the
AudioClip
's
play()
method to play it once, the
loop()
method to play
it repeatedly, and the
stop()
method to stop it.
T
EST
Q
UESTIONS
P
ROGRAMMING
E
XERCISES
Pedagogical Note
For every applet in the exercise, add a main method to enable it to run as a stand-
alone application.
Sections 18.2-18.6
18.1
(
Loan calculator
) Revise Listing 16.7, LoanCalculator.java, to be an applet for
computing loan payment.
18.2
(
Convert applications to applets
) Rewrite
ClockAnimation
in Listing 16.12
as an applet and enable it to run standalone.
*18.3
(
Guess capitals and states
) Revise Programming Exercise 9.17 to write an
applet that repeatedly prompts the user to enter a capital for a state or vice
versa, as shown in Figure 18.16a. Upon clicking the
Answer
button, the pro-
gram gets the user input from the text field, reports whether the answer is cor-
rect in a message dialog box (Figure 18.16b-c), shows the correct count and
total count, and then displays the next question. The user can specify whether
to let the program generate a question randomly or sequentially, and whether
to generate questions for a capital or a state.
(a)
(b)
(c)
F
IGURE
18.16
The applet tests the user knowledge on states and capitals.
*18.4
(
Pass strings to applets
) Rewrite Listing 18.5, DisplayMessage.java, to display a
message with a standard color, font, and size. The
message
,
x
,
y
,
color
,
fontname
, and
fontsize
are parameters in the
<applet>
tag, as shown below:
<applet
code
=
"Exercise18_04.class"
width
=
200
height
=
50
alt
=
"You must have a Java-enabled browser to view the applet"
>
<
param name
=
MESSAGE
value
=
"Welcome to Java"
/>
<param name
=
X
value
=
40
/>
<param name
=
Y
value
=
50
/>
<param name
=
COLOR
value
=
"red"
/>