This lab is adapted from a short debugging I found on the web at http://www.student.math.uwaterloo.ca/~cs-marks/resources/java/jdb/.

Lab6 debugging with jdb

Preparation

  1. Read through these instructions completely before coming to lab.
  2. Complete the WebCT "Pre-lab6: debugging" review questions.
  3. Follow the instructions below and then complete the postlab quiz.

Some initial setup

For this lab you will need to make a copy (in some appropriate subdirectory of yours) of the two files LetterHome.java and letter1.dat that are in /afs/cats/courses/cmps012a-cm/lab6.

This is a slightly buggy solution to a programming project from a prior year. You can find the specification for that problem at click here. This program reads its input from a file specified on the command line (a useful thing to know how to do) instead of from standard input (the keyboard). This is necessary because the Java debugging tool, jdb, cannot (easily) be used with programs that read from standard input. This is because the commands to the debugger are being read from standard input and the program can't tell which are debugger commands and which are inputs to your program.

Compiling for the debugger

If you plan to use a debugger (we will use "jdb") with your program, you need to tell the compiler to include some additional debugging information into the compiled version. You do this by placing the flag "-g" on the compile line like this:
javac -g MyFile.java

Running the debugger

When debugging programs we generally want to know the values of the various variables that are computed as the program executes. Sometimes we also want to know which branch of an if-else is taken or monitor each iteration of a loop.

The java System.out.println statement is and effective way to debug a program. Placing "debugging printouts" throughout the program at critical places to monitor the execution can be helpful. These printouts can easily be commented out when the debugging is completed, and later uncommented if further bugs show up later.

But what to do when this becomes too tedious? Another option is to use a debugger, such as jdb. In this lab you will use jdb to examine the values of some variables in a program and to find an error in a version of the LetterHome program from programming project 1.

You should start by running the program on the sample input (java LetterHome letter1.dat) and see that in fact it appears to have (at least) two errors. The expected output is:

Dear Mom and Dad:

Thanks for the stuff you sent.
My classes are going great.
The weather here has been great.
I plan to come home for a visit in a few weeks.
You should get something different. The program has two intentional "errors".
Valid HTML 4.0! Best Viewed With Any Browser