JamesMadisonUniversity

Computer Science Department


CS 139 Lab:Magic Eight Ball


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.

1 General Instructions:

  1. Set up your programming environment.
  2. Download the "starter program" from the materials.
  3. Update the EightBall.java with your identifying information.

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", output "Goodbye\n" and end the program using the System.exit(1) statement.
  4. If the user does enter the word, "yes" 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. Next prompt for the Random number seed, "Type a large positive integer: ".A seed is a value that is used to "start" the random generator.
  8. Output a new line after reading the value.
  9. After calculating the correct answer, output two lines using the printf formats (Remember that you need to provide the String to substitute):

4 Some useful information

  1. The Random constructor takes a parameter, the seed value. If we use the declaration 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.
  2. The Random class has a method, nextInt(int range), which takes in a integer which describes the number of values to be generated. In this case your range will be from 0 - 19 (20 values).

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. 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 that produces the 1st answer.

Updated 10/13/10 (nlh)