CS 139 Algorithm Development
Lab07B:
Magic 8 Ball
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 (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 (in conjunction with a switch)
equals() and equalsIgnoreCase() methods (Gaddis 3.9)
random number generator
(or more appropriately a pseudo random number generator or a random
number generator simulator)
Math.random
System.exit(n) - A
mechanism for ending a program. |
Materials
|
Begin with the program: EightBall.java.
|
Submission
|
- Complete the
CodeBat exercises indicated below at
http://codingbat.com. Be sure to include your
instructor e-mail in your profile.
- EightBall.java to Blackboard
|
Acknowledgment
|
Adapted from an original lab by Nancy Harris
|
Part 1 - Set Up
- Create a new folder
named lab07b for this lab, on your Desktop or removable drive.
- Download a copy of
EightBall.java to your folder from Blackboard.
Part 2 - Warmup
CodingBat is a web site
that has practice programming problems. These are set up as
individual methods. See the About page for more
information.
- Go to www.codingbat.com. If
you have not already done so, create an account (upper right
hand part of the window). Log in and then go to Prefs. Fill in
your name and then "Share" your account with your instructor
by putting her/his e-mail address in the Share box.
- In Java Warm-Ups-1,
do problems MonkeyTrouble and startOz (hint: there is a String method, charAt(int pos) that returns the char at that position. The first character in a string is at position 0. Careful, it will crash if the String is not long enough.
Part 3 - The main event
2 Steps:
- The declarations have
already been done for you.
- There are instructions
inside of the code. You will need to fill in the code.
3 Output:
- Output the heading,
"Magic
8 Ball\n\n"
"Do
you want to ask a question(yes/no)? "
Output a new line after reading the input.
- If the user does not enter
the word, "yes", in any case output
"Goodbye\n"
and end the program using the System.exit(1) statement.
- If the user does enter the
word, "yes" in any case, continue.
- Prompt for the question with
"What is your
question?\n"
and output a new
line after reading the input.
- Read in the question. If the
question is longer than 60 positions long, output the message
"Your
question is too long. Be more concise.\nGoodbye\n"
and exit the program with the System.exit(2) statement.
- Output a new line after
reading the value.
- After calculating the
correct answer, output two lines using the printf formats (Remember
that you need to provide the String to substitute):
"Question:
%s\n"
"
Answer: %s\n"
4 Some useful information
- Math.random() is a method of the Math class that returns a number from 0 (inclusive) to 1 (exclusive).
- Using this number, you can produce a number in any range. For example, if I want a number in the 10-15 range (or 6 different values altogether) I would multiply the random number by 6. That gives me a floating point number in the 0-5.99999999999.. range. Change it to an integer and you get 6 discrete values from 0-5. So what do I do to map those numbers to my desired range of 10 - 15?
5 Magic 8 Ball Answers:
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 (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.
- Signs point to yes.
- Yes.
- Reply hazy, try again.
- Without a doubt.
- My sources say no.
- As I see it, yes.
- You may rely on it.
- Concentrate and ask again.
- Outlook not so good.
- It is decidedly so.
- Better not tell you now.
- Very doubtful.
- Yes - definitely.
- It is certain.
- Cannot predict now.
- Most likely.
- Ask again later.
- My reply is no.
- Outlook good.
- Don't count on it.
5 Submitting your work
Demonstrate your working program for the lab instructors. You
should see at least one test that produces the 20th answer and one test
Optional Part 4 - Cool Down Practice
Return to
www.codingbat.com. In Java Logic-1, caughtSpeeding.
last update - 10/08/2012 - NLH