JamesMadisonUniversity

Computer Science Department


CS 139 Lab: Bottles of Pop


Objectives:

  • Students use a loop to solve a loop problem.  

Background:

There is a travel song, 100 bottles of “pop” on the wall. (See the verse below.) Your task is to implement a program that will “sing” a few verses of the “bottles of pop” song.  (You may substitute your favorite libation but in the interest of our underage population, the libation should be non-alcoholic.) 

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:

Reference: Gaddis chapter 4.2-while, 4.4-do, 4.5-for
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 your program.

2 Understanding the problem

The verses of the song looks like this:

100 bottles of pop on the wall
100 bottles of pop
If one of those bottles should happen to fall
99 bottles of pop on the wall

next verse
99 bottles of pop on the wall
… etc until

0 bottles of pop on the wall

  1. Do you see how this song works? If not, see one of the lab instructors.  Design the algorithm.  What values will you need to track?  How will you use those values?  What are the primary tasks (high level abstract) and then how will you carry each of those out. 
  2. Decide on the type of loop to use.  
  3. Test the algorithm.  Walk through it as if you were the computer.  Does it seem to work?

3 Implement the basic solution - singing the song from a number of the user's choice to 0

TEST each step before moving onto the next.
  1. Prompt the user for the beginning number. (i.e. 100) .  Use this number as a starting point for the song. Don't validate yet.
  2. Write the program (and test) for one verse without any error checking.  Use a variable for the number of bottles.
  3. Next add in the decrementing loop (the verses go down for each loop).  Test this starting at the number the user entered and decrementing to 0.  Check that you don't have an off by one error or an infinite loop.
  4. Add a check to insure that the number the user has entered is an integer and that the integer is >= 0.  Keep prompting and reading until you get a good value. HINT: This should be a method so that you don't have to worry about an inner loop. You have some helpful methods in your toolkit that you can submit.

4 Implementing a more complex solution - singing only a few verses of the song

Once part 3 is working correctly, add in another variant of this program.
  1. Just after you prompt the user for the beginning number, prompt them for the number of stanzas to sing.  Do not worry about error checking yet.
  2. Alter the loop in part 3.3 so that it stops when the proper number of verses has been "sung".  So if the number to start is 100 and the verses to sing is 5, we would expect to see 
  3. 95 bottles of pop on the wall as the last line.
  4. When that is working correctly, verify that the number of verses is a number and that the verses are less than or equal to the number of verses to sing.  Until a valid number is entered, keep prompting and reading until you get a correct number. METHOD ANYONE?

5 Refinement - OPTIONAL

Once part 4 is working properly and you have looked for off by one errors, add this refinement.
  1. Instead of printing 1 bottles of pop on the wall when we get to 1, display 1 bottle of pop on the wall when we reach that number.

Updated 10/19/11 (nlh)