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

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.

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 named lab07b for this lab.

  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

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. 

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

    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.

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

  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?
  3. An alternative way of generating random numbers is the Random class which you can read about in Chapter 4.11. It has methods that will enable you to generate numbers in a range of 0 - n where n is passed to the method.

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.

  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.



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


last update - 10/08/2013 - NLH