James Madison University, Spring 2018 Semester
Lab16: 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
Step through code using a debugger.
Trace execution flow with a debugger.
Compare iteration with recursion.
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) |
-
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. -
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?
-
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.
-
Modify your code to print "No more bottles" instead of "0 bottles" when applicable.
-
Make sure your solution works in the following cases. Test each one via the command line.
java Bottles 0
nothing should printjava Bottles 1
the last verse printsjava Bottles 5
only five verses print
Part 2: Debugging with jGRASP
Most integrated development environments (lik$
-
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 c$
-
Move your cursor to the following line of code in jGRASP:
if (args.length > 0) {
-
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 whe$ in Debug Mode.
-
Now click the "Lady Bug" button on the toolbar. Your program should stop on the if statement.
-
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)
-
Practice stepping through your code one line at a time. Each time the program ends, you will need to restart the debugger
-
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.
-
Part 3: 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.
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.