Objectives |
This program provides practice in the use of the Java switch statement and introduces a random number generator. |
Background |
The Magic 8 Ball is a toy produced by Tyco (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: A Java
statement that branches to one of several possible
outcomes depending upon the value of a single variable break statement (in conjunction with a switch): A Java statement that terminates one branch of a switch equals() and equalsIgnoreCase() (Gaddis
3.9): methods that determine the equality of the
contents of String variables random
number generator (or more appropriately,
pseudo-random number generator or random number
generator simulator): A Java utility method that
generates a series of pseudo-random numbers. (Computers, being deterministic and therefore predictable machines, are incapable of truly random behavior, since randomness depends on unpredictability. Computers simulate randomness by generating long sequences of numbers that appear to be random in the short term. True randomness requires an unpredictable process.) Math.random(): A method for
generating pseudo-random numbers that are >= 0 and
< 1. |
Materials |
Begin
with the program: EightBall.java. |
Submission |
|
Acknowledgment |
Adapted
from an original lab by Nancy Harris |
GRADING | See your instructor for the percentage grading for this assignment. |
CodingBat is a web site that has practice programming problems. These are set up as individual methods. See the About page for more information.
"Magic 8 Ball\n\n".
Ask
the user: "Do you want to ask a
question(yes/no)? "
and input the
answer. "Goodbye\n"
and end the
program using the System.exit(1)
statement."What is your
question?\n"
."Your
question is too long. Be more concise.\nGoodbye\n"
and exit the program with the System.exit(2)
statement. Complete the method "checkQuestion" to perform
this test."Question:
%s\n"
"
Answer: %s\n"
Your random number generator should output a number from 1 to 20. The main method should then choose an appropriate answer. A switch structure may be the best way to implement this (but it can also be implemented by a series of if/else statements). Make sure that you test a few entries before implementing all 20.
last update -
10/08/2013 - NLH