.Lists { font-family: Arial, Helvetica, sans-serif; }
CS 139 Algorithm Development
Lab09A: 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 (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.)

Random class: A class containing methods that help us to generate pseudo Random numbers.

System.exit(n) A method for ending a program. A call to System.exit(0) terminates the currently running program and returns control of the computer to the operating system. While you are being guided to use that in this program, it violates one entry and one exit from a method.

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.
  • Submit EightBall.java to Blackboard

Acknowledgment

Adapted from an original lab by Nancy Harris
GRADING See your instructor for the percentage grading for this assignment.



Part 1 - Set Up
  1. Create a new folder for this lab.

  2. Download a copy of EightBall.java to your folder from Canvas or from the Materials section.



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

For this part of the lab, you will complete the coding in EightBall.java. The declarations have already been done for you. There are instructions inside of the code. You will need to fill in the code to complete the program.
  1. Create a Scanner object.

  2. Output the heading, "Magic 8 Ball\n\n".

  3. Ask the user: "Do you want to ask a question(yes/no)? " and input the answer.  Output a new line after reading the input.

  4. If the user does not enter the word, "yes" (regardless of upper or lower case), output "Goodbye\n" and end the program using the System.exit(1) statement.

  5. Prompt for the question with "What is your question?\n" and output a new line after reading the input.

    Read 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. Complete the method "checkQuestion" to perform this test.

    Output a new line after reading the value.

  6. Use the random number generator to select an answer from the list of answers listed below. Use the method "pickRandom" to select a random number.

  7. After calculating the correct answer, output two lines using the following printf formats (Remember that you need to provide the String to substitute):
    • "Question: %s\n"
    • " Answer: %s\n"

Some useful information

  1. Random is a class that generates pseudo random numbers. It can be instantiated with a "seed", basically a starting value to control the "random" numbers or without anything which will base the starter on the system date/time.
  2. The nextInt method returns a value across the entire range of integer values. To control the range, you can specify a value to nextInt which is the number of different values that you want. For example to generate numbers in the 10-20 range, you are looking for 11 possibilities, so you would call nextInt with (random.nextInt(11)). That will give you a number from 0 - 10 (one less than the parameter, but this is actually 11 values). So how can you map that return value into the 10-20 range?

Magic 8 Ball Answers:

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.

NOTE: You may also make up your own answers. You should have at least 10 different answers for this problem. (Keep them clean and respectful of anyone viewing your program).

  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.

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 first. If you don't get it finished during lab, upload the finished work to Canvas.



Part 4 - Cool Down Practice
  1. Return to www.codingbat.com. In Java Logic-1, complete caughtSpeeding.


last update - 10/20/2014 - NLH