CS 139 Algorithm Development
Lab08B: Bottles of Pop

Objectives

Students use a for loop to solve a counted 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

You will write BottlesOfPop.java from scratch.
Submission
  • Complete the CodeBat exercises indicated below at http://codingbat.com. Be sure to include your instructor e-mail in your profile.

  • Submit BottlesOfPop.java to Blackboard or demonstrate for the lab instructor.

Acknowledgment

Adapted from an original lab by Arch Harris & Nancy Harris



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

Part 2 - Warmup

  1. In the CodingBat Java / String-2 group, do repeatEnd - http://codingbat.com/prob/p152339. You should do this part individually, but may receive help from a partner.

Part 3 - Understand the problem

The verses of a song look 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 the pattern of how this song works? If not, see one of the lab instructors. 

  2. Design the algorithm. 
    1. What values will you need to track? 
    2. How will you use those values? 
    3. What are the primary tasks (high level abstract) and then how will you carry each of those out.

  3. Decide which type of loop to use in the program (while, do/while, for). 

  4. Test the algorithm.  Walk through it as if you were the computer.  Does it seem to work?

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

TEST each step before moving on to the next.

  1. Prompt the user for the beginning number (e.g., 100).  Use this number as a starting point for the song.

  2. Write the program (and test) for one verse without any error checking.  Use a variable for the number of bottles and give it an arbitrary initial value.

  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.

  4.  Check to make sure that you don't have an off-by-one error or an infinite loop.

  5. Add a check to ensure 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.


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

Once part 4 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 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  95 bottles of pop on the wall  as the last line.

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

(Optional) Part 6 - Refinement

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

  2. Make your song count down to zero! Instead of printing "0 bottles" of pop on the wall when we get to 0, display "no more bottles" of pop on the wall...

(Optional) Part 7 - Cool Down Practice

Return to www.codingbat.com. In Java String-2, complete doubleChar http://codingbat.com/prob/p165312.

updated 10/16/2012 - NLH
updated 10/18/2012 - CSM