![]() |
Computer Science Department |
Objectives: |
This program provides practice in the use of the Java switch statement. |
Background: |
The Magic 8 Ball is a toy produced by Tyco (maybe Mattel now) Toys and consists of a ball filled with a blue fluid. Suspended in the fluid is a icosahedron (a 20 sided polyhedron with each side consisting of an equilateral triangle.) Each face has an answer to a y/n question. See http://8ball.ofb.net/procedure.html for details of what a Magic 8 Ball looks like. |
New Terms: |
switch statement break statement equals() and
equalsIgnoreCase() methods (Gaddis 144-148) random number generator (or more appropriately a pseudo random number generator or a random number generator simulator) Random class (Gaddis 233-235) System.exit(n) - A mechanism for ending a program. |
Materials: |
Begin with the program: EightBall.java.
|
Acknowledgments | Adapted from a lab by Nancy Harris. |
"Magic
8 Ball\n\n"
"Do
you want to ask a question(yes/no)? "
Output a new line after reading the input."Goodbye\n"
and end the program using the System.exit(1)
statement."What is your
question?\n"
and output a new
line after reading the input."Your
question is too long. Be more concise.\nGoodbye\n"
and exit the program with the System.exit(2)
statement."Type a large
positive integer: "
.A seed is
a value that is used to "start" the random generator."Question:
%s\n"
"
Answer: %s\n"
Random
aGen
, you instantiate it with
aGen = new
Random(largeInt);
, where
largeInt is a large integer value. In
this case that value is read in.Your random number generator should output a number from 1 to 20 and use the corresponding answer. A switch structure may be the best way to implement this. Make sure that you test a few entries before implementing all 20.
Updated 10/13/10 (nlh)