CS 139 Algorithm Development
Lab08A: LoopyMagic 8 Ball

Objectives

Introduce students to using loop structures in programs for error correction and execution control.

Background

We have already worked with if statements to detect erroneous data.  But in some cases users will repeatedly enter the wrong value.  In addition, some problems lend themselves to repeatedly executing the program.  For example, in PA2, it would be useful to execute the program for not just one star or planet, but for a series of stars or planets of the user's choosing.  This lab will explore these uses of the Java loop.

New Terms

loop
A program structure that supports iteration.
loop control variable
A variable used to control the continuation of a loop.  
sentinel value
A special value that signals to the program that the loop should not continue.
off by one error
A kind of loop error in which the loop executes one too many or one too few times.
infinite loop
A kind of loop error in which the loop never finishes executing.

Materials

You will use your EightBall.java program from a prior lab for today's lab.  If you did not finish this lab, download a model solution from EightBall.java.

You will need to use some of the String methods for this lab.  This is a link to the Java documentation for the methods for String.  Java documentation is known as the Java APIs.  A link to all of the APIs is found on our resources page.  You will find on this page a list of all the methods for the String class.  And by clicking a method in the method summary chart, you will go to the more detailed description of that method.
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 Arch Harris & Nancy Harris



Part 1 - Set Up
  1. Create a new folder named Lab08A for this lab, on your Desktop or removable drive.

  2. Download a copy of whichever EightBall.java you prefer to use 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, Logic-2 do lonesum - http://codingbat.com/prob/p148972. You should do this part individually, but may receive help from a partner.

Part 3 - The Main Event

1. Error checking - beginning:

In your original program, you asked the user for a question and then "shook" the 8 ball to get an answer.  You did a check to see if the question was too long.  In this variant, you will not end the program, but instead ask the user to enter a shorter question as follows.
  1. If the String is longer than 60, print out an error message that indicates that the String is too long.  Reprompt the user and read in the question again.  Continue checking for the length until the user enters a question less than or equal to 60 characters.
  2. THINK very carefully about how you will set up the loop and how you will handle the repetition.  Will you use a pre or post condition loop?  Decide this before you code.
  3. Note: your error message should be specific...if the problem is that the String is too long, state that.
  4. Once it has passed this error, output the question and the answer as in the original lab.

2 Error checking - refinement:

Once item 1 is working correctly, add this variant that will also make sure that we have something there for the question.  Recall that the nextLine() method can read in an empty String.  Follow these guidelines to check for that error.
  1. Your question should not be the empty String.  (Hint: The String class has a length method that will tell you how many characters are in the string).  If the String is null (empty), you should tell the user that the empty string is not allowed and then prompt for and read in the question again.  You should do this until the user enters a valid String.
  2. After you have read in the proper question, echo the question with a suitable label, "shake" the 8 ball, and return the answer as before.

3 Continue to play:

Once item 2 is working correctly, add a new loop surrounding the original code which will execute the loop until the user says that they no longer wish to continue.  
  1. You are already prompting for and receiving a yes/no response to the question "Do you want to ask a question(yes/no)? ".  
  2. Once they have asked a question, you should reprompt for "Do you want to ask a question(yes/no)? ". If they answer "yes" (in any case) you will continue. If not, you should exit

4 When is a question not a question?:

Once item 3 is working correctly, add in a final refinement.

  1. A question ends with the character '?'. Check the question String and make sure that it ends with a question mark. Be careful...the empty String has no characters in it.
  2. If there is no question mark, tell the user that they need to ask a question and reprompt as in items 1 and 2.



(Optional) Part 4 - Cool Down Practice
  1. Return to www.codingbat.com. In Java Logic-2, complete roundSum http://codingbat.com/prob/p186753.


last update - 10/15/2012 - NLH