JamesMadisonUniversity

Computer Science Department


CS 139 Lab: Practice with Looping - Preconditional and Postconditional Loops


Objectives:

  • Introduce students to using loop structures in programs.
  • Provide students practice in validation problems

Background:

Many computing problems use the capability of the computer to repeat calculations very quickly. This lab will let you explore loop structures to solve various problems.

New Terms:

  • loop
  • loop control variable
  • off by one error
  • infinite loop
  • while
  • do while
  • for

Materials:

Download Guess.java.
Acknowledgment Lab by Nancy Harris

Part 1 General Instructions:

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

Download the program from the materials section.

Part 2 Using a post condition loop in a guessing game.

For this part, you will create a loop that will prompt the user to enter a character between 'a' and 'j' and tell them if they have guessed the correct value that is generated by a random number generator.  That generator and producing the character to guess are provided.  The program should count the number of guesses the user makes before guessing the character.  If the user guesses in 5 tries or less, announce that they win.  If the user takes more than five tries, announce that the computer wins.  In your final output message, display the character the computer picked, the number of guesses that the user made and who won.  As a failsafe, if the user makes more than10 guesses, end the loop and announce winner as before, in other words, let the user keep guessing even if they can't win.
  1. Create a set of test cases (Examples).  Since we are using a random number generator, you will need to decide what circumstances you want to test for.  Include cases like guessing on the first and last tries.  You should also build a test case where the user makes too many guesses.
  2. Decide how you want to structure your loop.  You need to make at least one pass, which would mean one prompt to ask the user for a guess and a read of their value.  
  3. Decide what your ending condition will be.  How will you know you have reached the end of your looping?
  4. Code your loop.
  5. Test your code thoroughly.
  6. Save your work.

Part 3 Using a pre condition loop in a guessing game.

For this part, you will ask the user if they want to play the guessing game.  If they say "yes", you execute the code from Part 2.  If they say "no", you end the game.  At the end of one round, you ask the user again if they want to play again.  If so, you continue to play the game; if not, you end the game.  If the user enters 'y', 'Y', or "yes", consider it an affirmation that they want to play.  If they enter anything else consider it as if it were a "no" answer.  Each round should generate a new number, prompt the user for the correct guess and display the results.
  1. Create a set of test cases (Examples) that you will test for correctness.
  2. Create a new copy of your code as GuessV2.java.
  3. Look carefully at your code.  Where should the initialization occur for your new loop?  Where will the test be? 
  4. Make sure that the code needed for the game is contained within the body of the loop.
  5. Test your program thoroughly.

Part 4 Thought Questions

  1. Why did we suggest that you use a post condition loop for the first part?
  2. Why did we suggest that you use a pre condition loop for the second part?
  3. How would your code be different if you used a pre condition loop for the first part?
  4. How would your code be different if you used a post condition loop for the second part?

Part 5 - Optional - Higher or lower

  1. Add code to your game to tell the user whether their guess is higher or lower than the actual value.  

Created 10/13/09 (nlh)