CS 127 Lab: Debugging

The DrJava debugger

Your goal here is to get to know DrJava, an integrated development environment (IDE) for developing in Java that includes a debugger. Debuggers can't help you until your program compiles, but once you have eliminated all compiler errors, a debugger can be one of your most valuable tools.

Today, you'll use DrJava to (1) watch a variable, (2) single-step through a program, and (3) set breakpoints. All three of these operations should become clear shortly.

  1. Save the program SumPractice.java in your account.

  2. Start up DrJava by typing drjava at a Linux prompt.
  3. From the File menu, choose Open, then navigate to find SumPractice.java. Open it.

  4. Compile your program by clicking on the Compile All button in the toolbar.

  5. In the window at the bottom, click on the Interactions tab. Type java SumPractice to run the program.

  6. Turn on the debugger by going to the Debugger menu, and clicking on "Debug Mode."

  7. Line 22 should be the one that reads int n = 10 (the line number can be found at the very bottom right of the window). Right-click on this line somewhere, then set a breakpoint by going to the Debugger menu and selecting "Toggle Breakpoint." The line should be highlighted to indicate that a breakpoint is set.

  8. Run your program again by typing java SumPractice in the Interactions Window at the bottom. The program will run until your breakpoint and stop there.

  9. In the "Watches" tab that popped up with the debugger, type "n" in the Name column. You should see <not found> in the value column, since n has not been defined yet.

    Likewise, add "sum" to the Name column.

  10. Click the "Step Into" button (to the right of the debugging windows). What value is currently in n?

  11. Keep clicking "Step Into" until your program finishes. Continue to observe the values of n and sum.

  12. Run the program again, but this time use the "Step Over" button instead of "Step Into." What is the difference between these two buttons? Why is this useful for debugging?

  13. Run the program again, and this time use "step" again to step into the sumOfOdds function. After you have stepped through a couple of iterations of the while loop, click the "Step Out" button. What does this button do? Why is this useful for debugging?

Debugging Code

cd to the directory /Accounts/courses/cs127/dmusican/debug. There, you will find four files: Program1.java, Program2.java, Program3.java, and Program4.java. Copy them your own directory. The files are from a recent CS117 homework assignment; all personal identifying information has been removed from the code. (Special thanks to the students who graciously donated their code!)

These files all have something in common: they compile and run, but they do not always generate the expected result. Your task is to find the bug(s), classify them, and correct them. You do not need to turn in any of your work. Work together, and have fun!

Here are some guidelines:

If time permits, you may want to try these debugging methods on your own code while in the lab.


Authors: Amy Csizmar Dalal, Dave Musicant, Mark Peralta, Jeff Ondich