Java Reference
In-Depth Information
Write a program
DieSimulator
that uses the
Random
class to simulate
the cast of a die, printing a random number between 1 and 6 every time that
the program is run.
Exercise P2.8. Write a program
LotteryPrinter
that picks a
combination in a lottery. In this lottery, players can choose 6 numbers
(possibly repeated) between 1 and 49. (In a real lottery, repetitions aren't
allowed, but we haven't yet discussed the programming constructs that
would be required to deal with that problem.) Your program should print
out a sentence such as ȒPlay this combinationȌit'll make you rich!ȓ,
followed by a lottery combination.
76
77
T Exercise P2.9. Write a program
ReplaceTester
that encodes a string
by replacing all letters
ÐiÑ
with
Ð!Ñ
and all letters
ÐsÑ
with
Ð$Ñ
. Use
the
replace
method. Demonstrate that you can correctly encode the
string
ÐMississippiÑ
. Print both the actual and expected result.
Exercise P2.10. Write a program
HollePrinter
that switches the
letters
ÐeÑ
and
ÐoÑ
in a string. Use the
replace
method repeatedly.
Demonstrate that the string
ÐHello, World!Ñ
turns into
ÐHolle,
Werld!Ñ
G Exercise P2.11. Write a graphics program that draws your name in red,
contained inside a blue rectangle. Provide a class
NameViewer
and a
class
NameComponent
.
G Exercise P2.12. Write a graphics program that draws 12 strings, one each
for the 12 standard colors, besides
Color.WHITE
, each in its own color.
Provide a class
Color-NameViewer
and a class
ColorNameComponent
.
G Exercise P2.13. Write a program that draws two solid squares: one in
pink and one in purple. Use a standard color for one of them and a custom
color for the other. Provide a class
TwoSquareViewer
and a class
TwoSquareComponent
.
G Exercise P2.14. Write a program that fills the window with a large
ellipse, with a black outline and filled with your favorite color. The