CS 139 Algorithm Development
Lab09B: 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.  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 for this lab.

  2. Download a copy of whichever EightBall.java you prefer to use to your folder from Canvas.



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 makeBricks - http://codingbat.com/prob/p183562. 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. In the original lab, you had a method that checked the question for length. This method would then abort the program if it was too long after printing the error message. This time, we want to change that method to return a valid question. That means that this method should not abort if wrong, but instead loop and return a valid question.
  2. 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.
  3. 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.
  4. Note: your error message should be specific...if the problem is that the String is too long, state that.
  5. 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...in other words, the question is not too short.  Follow these guidelines to check for a too short question.
  1. Your question should not be too short.  If the String is less than 3 characters long, you should tell the user that a short 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. Note also that you should combine this requirement with the first requirement. In other words, for each entry that the user makes, you should check to make sure that it is not too long nor too short. This is tricky so again THINK before you code.

3 Continue to play:

Once item 2 is working correctly, make the following changes.
  1. You are already prompting for and receiving a yes/no response to the question "Do you want to ask a question(yes/no)? ". However, if they say something other than "yes" we are using a System.exit to leave the program. While this is legal, it is not the best coding practice. So consider how you might change this if statement to simply end main if the answer is something other than "yes" and then implement that change. 
  2. Then, you should change this whole structure into a loop which will execute only if they have answered "yes" to the question. Hmmmm, do you need an else any more?

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. (See Chapter 9 for the "endsWith" method. Be careful...the empty String has no characters in it; in other words, make sure that there is at least one character to test).
  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/22/2014 - NLH