JamesMadisonUniversity

Computer Science Department


CS 139 Lab: Loopy Eight 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 here.  There are a couple of minor differences in this version, but it can be used for this program.

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.
Acknowledgment Lab by Nancy Harris

1 General Instructions:

Set up your programming environment for this lab. You should create a folder into which you will store both programs.

Download your EightBall.java program from the prior lab.  

Do each step and test it before moving on to the next.

NOTE: The first three error will all be in one loop.  Once you set up the loop in part 1, add in the additional checks for part 2 and part 3.

2 Error checking - part 1:

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

3 Error checking - part 2:

Once part 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.

4 Continue to play:

Once part 3 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 the program.

Updated 10/18/2011 (nlh)