CS 149: Introduction to Programming
James Madison University, Fall 2018 Semester

Lab15: Debugging and tracing

Background

Have you ever heard that unforgettable children's song 100 bottles of pop on the wall? Your task is to write a program that will "sing" a few verses of this song. Here's an example recording on Youtube, but beware it may get stuck in your head the rest of the day!

Objectives

Key Terms

control variable
the variable used to control the continuation of a loop
off by one
error in which a loop executes one too many/few times
infinite loop
common error in which a loop never finishes executing

Part 1: Bottles of Pop

Download Bottles.java as a starting point for the lab. Write your name and today's date in the Javadoc comment. Here is what the output should look like when you finish the lab:

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

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

(repeat until 1)
  1. Most of the algorithm has already been implemented in Bottles.java. Figure out how to complete the sing method. Do NOT edit any of the other methods.

  2. Test your solution on a small number of verses. You may want to run this lab from the command line (i.e., java Bottles 5) for convenience.

    • Is it working right? If not, go back and fix it!
    • Do you have a blank line after each verse?
  3. Modify your code to print "1 bottle" instead of "1 bottles" when applicable.

    • Don't forget the ending of the "2 bottles of pop" verse.
  4. Modify your code to print "No more bottles" instead of "0 bottles" when applicable.

  5. Make sure your solution works in the following cases. Test each one via the command line.

    • java Bottles 0   nothing should print
    • java Bottles 1   the last verse prints
    • java Bottles 5   only five verses print

Part 2: Debugging with jGRASP

Most integrated development environments (like jGRASP, Eclipse, and NetBeans) come with a debugger that allows you to "step through" your code while it is running.

  1. Click the Build menu and make sure the box is checked for Debug Mode. It is the default so it should be, but if not click the box. Compile your program.

  2. Move your cursor to the following line of code in jGRASP:

    if (args.length > 0) {
  3. Use your mouse to hover over the gray line to the left of the line numbers of the statements until a red dot appears. When the red dot appears click to set a Breakpoint on that line. This will make the program stop when it comes to that line in Debug Mode.

  4. Now click the "Lady Bug" button on the toolbar. Your program should stop on the if statement.

  5. At this point, you can take any of the following steps from the menu of blue arrows on the left above threads:

    • Step Over (leftmost straight down blue arrow)-- run the current line of code, but don't step into other methods
    • Step In (middle blue arrow that points right) -- run the current line, stepping into any method calls
    • Step Out (right blue arrow that points left) -- run all code until it returns from the current method
    • Resume (blue > button to the right) -- run all code until it reaches another breakpoint (if any)
  6. Practice stepping through your code one line at a time. Each time the program ends, you will need to restart the debugger

  7. jGRASP has an "Auto Step" feature that animates the entire execution of a program. Try setting it by clicking on the double down arrow. Note the slider bar that appears where you can set the time between steps. Now rerun your program with the initial breakpoint. You should be able to click the step button and watch your program run automatically, stepping through the program.

  8. After you have gotten the song to work using the iterative version of the code. Change the iterative(start) call to recursive(start) and step through your code again.

  9. Part 3: Generating Pseudo-random numbers

  10. Now that you have debugged your Bottles.java program, look at how the start index is being determined. While you might think that this code would have the song start singing at a different place each time, did you notice it always started with 14? That is because these values are only pseudo-random. To get an actual random number you will need to apply a different seed when you create the new Random object. This makes debugging difficult though so we use pseudo-random numbers only in this class. Make sure and notice the parentheses around the multiplication BEFORE the cast to an integer. Removing these parentheses will result in a start of zero and the song will not be sung. Try it!

  11. Next, change the code in your Bottles.java program so that it can generate a random number between 0 and 50 instead of 0 and 20 in the example.

  12. Submit your completed Bottles.java file via Canvas.

    Part 4: CodingBat Problems

    For the remainder of today's lab, see how many of the Recursion-1 problems you can solve. Remember to log into CodingBat first so you will receive credit. At a minimum, solve the following: sumDigits and countX. Be sure to read the paragraph at the top of the Recursion-1 page.

    Submit your completed Bottles.java file via Canvas by the end of the day. About half of your lab grade will be based on your CodingBat results.