This assignment asks you to write an applet that replicates the classic Magic 8 Ball toy from the 1960’s.
Your program (like the original 8 Ball) will not really pay any attention to the user’s question—it will simply take the
question and discard it. Then it always gives one of several "canned" answers. The original Magic 8 Ball had the
answers printed on the sides of two eight sided dice.
outlook not so good
don't count on it
my sources say no
without a doubt
reply hazy, try again
it is certain
my reply is no
as I see it yes
most likely
you may rely on it
cannot predict now
better not tell you now
very doubtful
outlook good
yes definitely
concentrate and ask again
You may use these answers or make up your own.
Arrays in Java work a little differently than C++. For this assignment you are required to use an array of strings to store the answers. You can declare and intialize an array of strings with code like:
String[] saAnswers = {"yes", "no", "maybe" };
System.out.print(saAnswers[1]);
Your program will need to get user input. For this assignment you will use a TextField
.
The code for including a TextField
is identical to that for a Button. To "remove" the
TextField
(or a Button) from the screen just use the command remove
.
Your assignment will need at least two classes: the Applet class and a class to represent the eight ball. Make sure
that the eight ball class contains the array of answers as well as a method to generate the random answer. (Note: don't
call your eight ball class 8ball
. Java won't accept a digit for the first letter of a class name.)
You can change the label of a button with the setLabel()
member function. (You can also retrieve the
current label with getLabel()
, for more information check the JDK help file). You may also want to center
the answers on the screen. One way to do this involves FontMetrics
. You can retrieve the length of a
String
in pixels with g.getFontMetrics().stringWidth(sString)
where g
is the graphics object and sString
is the String
.
You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.
Submit the URL of your applet along with your Java code (the .java
file(s)) in an email message
to mrsimon@lycos.com.