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
  1. Create a new folder named lab07b for this lab, on your Desktop or removable drive.

  2. 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.

  1. 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.

  2. 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:

  1. The declarations have already been done for you.
  2. There are instructions inside of the code. You will need to fill in the code.

3 Output:

  1. Output the heading, "Magic 8 Ball\n\n"
  2. "Do you want to ask a question(yes/no)? " Output a new line after reading the input.
  3. 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.
  4. If the user does enter the word, "yes" in any case, continue.
  5. Prompt for the question with "What is your question?\n" and output a new line after reading the input.
  6. 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.
  7. Output a new line after reading the value.
  8. 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

  1. Math.random() is a method of the Math class that returns a number from 0 (inclusive) to 1 (exclusive).
  2. 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.

  1. Signs point to yes.
  2. Yes.
  3. Reply hazy, try again.
  4. Without a doubt.
  5. My sources say no.
  6. As I see it, yes.
  7. You may rely on it.
  8. Concentrate and ask again.
  9. Outlook not so good.
  10. It is decidedly so.
  11. Better not tell you now.
  12. Very doubtful.
  13. Yes - definitely.
  14. It is certain.
  15. Cannot predict now.
  16. Most likely.
  17. Ask again later.
  18. My reply is no.
  19. Outlook good.
  20. 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