The DrJava Debugger


Overview

The more you program, the more time you will spend debugging. Even with carefully planned programs, there will be errors that will be difficult to find. Especially difficult to find are the bugs that do not cause compilation errors, but instead affect the integrity of the program. A debugger is a tool within the IDE (a program like DrJava) that can help you walk through a program and keep track of certain values so that you can hopefully figure out where you went wrong.

Goals

How to get started...

  1. Download Practice.java or copy the text into DrJava.
  2. Compile Practice.java
  3. Turn on the debugger. Go to Debugger -> DebugMode (alternatively, hit Ctrl+D)
  4. Become familiar with the debug mode. You will see two new sections appear the DrJava interactions. The left side allows you to watch the values of your variables. We won't worry about the right side for now.
  5. On the left side, there is a tab that says Watches. Underneath this tab, there is a space for you to type in the name of a variable.
  6. Learn what a breakpoint is. Breakpoints allow you to "pause" your program so that you can see the values of your variables. The values of the instance variables of an object at any point in time is called the state of that object.
  7. Practice using breakpoints. Under the Watch tab, type a, b, and i each in a separate space under the Name column. Press return to get a new space. Now click at the beginning of the loop() method on line 5. Then choose Debugger -> Toggle Breakpoint on Current Line (alternatively, hit Ctrl+B). There should be a red bar across line 5.
  8. Using DrJava's Debugger Now complete the following interactions:
     > Practice p = new Practice()
     > p.loop()
  9. Understanding the buttons
  10. Hit the resume button. Notice that you didn't get to track any of the values because the program finished too quickly.
  11. Go back and type the interactions again from before. Now set DrJava to debugger mode and put a breakpoint on line 5. You can put some variables like a, b, or i in the left hand column for DrJava to watch. Now instead of hitting Resume, try Step Over. If you keep hitting Step Over, you will see how the program runs line by line. Watch your variables in the left pane.
  12. Clear your breakpoint by choosing Debugger -> Clear all Breakpoints. Now create another breakpoint within the loop on line 8. Since line 8 is executed more than one time, if you use resume to watch the variables, the program will stop each time at line 8. Try that now by typing in the following and then using resume to step through your program:
     > Practice p = new Practice()
     > p.loop()

Exercises

Download ScrabbleScorer.java and open it in DrJava. ScrabbleScorer.java compiles, but does not always run properly. The inputs provided below should illuminate most of the problems with the program. You should observe the following:

Input Expected output Actual output
"HiThere" 13 28
"HelloWorld" 17 47
"score+=1;" 7 50
"kahnnnn!" 14 24

The "Expected output" comes from manually adding the scores for alphabetical characters, as listed in the comments in ScrabbleScorer.java. Note that punctuation characters should score 0 points. The "Actual output" is the value output by the program.

The code in the file ScrabbleScorer.java contains at least three logical errors (also called "bugs"). Try to find them all with the debugger, make sure to test other inputs besides just those provided, they may not lead you to all the bugs. Think about where it makes sense to put your breakpoints and which variables you need to track.

 

Download Blackjack.java and open it in DrJava. Blackjack.java compiles, but does not run properly. The following interactions illustrate how it should run. Use the debugger to find the errors. (Note: because it involves the use of a psuedo-random number generator, the exact numbers in the outputs will not match, but the general functionality should)

> Blackjack game = new Blackjack()
> game.getRecord()
You have won: 0 games and lost: 0 games
> game.dealHand()
You have: 5 1 for a total of: 6
Dealer shows: 10, what would you like to do?
> game.hit()
You have: 5 1 7 for a total of: 13
Dealer shows: 10, what would you like to do?
> game.hit()
You busted.
You have: 5 1 7 10 for a total of: 23
Dealer shows: 10, 10, for a total of: 20
You lost.
> game.dealHand()
You have: 5 3 for a total of: 8
Dealer shows: 1, what would you like to do?
> game.hit()
You have: 5 3 2 for a total of: 10
Dealer shows: 1, what would you like to do?
> game.hit()
You have: 5 3 2 3 for a total of: 13
Dealer shows: 1, what would you like to do?
> game.stay()
You have: 5 3 2 3 for a total of: 13
Dealer shows: 2, 1, 8, 3, 8, for a total of: 22
You won.
> game.dealHand()
You have: 6 7 for a total of: 13
Dealer shows: 10, what would you like to do?
> game.hit()
You busted.
You have: 6 7 10 for a total of: 23
Dealer shows: 4, 10, for a total of: 14
You lost.
> game.getRecord()
You have won: 1 games and lost: 2 games
> game.getCardsUsed()
20
> game.reset()
> game.getRecord()
You have won: 0 games and lost: 0 games
> game.getCardsUsed()
0
> game.dealHand()
You have: 3 6 for a total of: 9
Dealer shows: 3, what would you like to do?
> game.hit()
You have: 3 6 1 for a total of: 10
Dealer shows: 3, what would you like to do?
> game.hit()
You have: 3 6 1 7 for a total of: 17
Dealer shows: 3, what would you like to do?
> game.stay()
You have: 3 6 1 7 for a total of: 17
Dealer shows: 6, 3, 10, for a total of: 19
You lost.
> game.getRecord()
You have won: 0 games and lost: 1 games
> game.getCardsUsed()
7
>

AGAIN NOTE: Simply following the interactions above will not necessary lead you to all the errors. There are some cases not illustrated above that are faulty in the program.