RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab10b
debugging

Today we will use the debugger tool built into BlueJ.

  1. Make a new project with the following two classes which are a (buggy) solution to last week's 'Pig Latin' lab: VowelExpert.java, TestVowelExpert.java,
  2. It contains two bugs. We'll track it down as follows.
  3. Run our test-class's main method. From inspecting the terminal-window output, What method seems to contain the (first) bug?
  4. Now, open VowelExpert and click on the line-number, for a line inside the faulty method, firstA. BlueJ will put a little stop-sign (a “breakpoint”) by that line.
  5. Then you can run your program/tests as normal, but when BlueJ reaches a breakpoint, it pauses executing and puts up a debugger-window. This window contains three important panels: In addition to those important panels, there are three important buttons available:

After you fix both bugs, here's a method to paste into class VowelExpert. Step through it, to see how it works:

    /** A mystery method.  Step through this with a debugger.
     * (It doesn't actually compute anything meaningful, though.)
     */ 
    public String mystery() {
        final int N = 8;
        final String spell = "Supercalifragilisticexpialidocious";
        
        String nonsenseSoFar = "";
        for (int i=0; i+N <= spell.length(); i=i+2) {
            System.out.print( i + ": " );
            String smidgen = spell.substring(i,i+N);
            String edjumacated = toPigLatin( smidgen );
            nonsenseSoFar += edjumacated.substring(2,3);
            System.out.println( edjumacated );
            }
        return nonsenseSoFar;
        }

Upshot: Good programming tip: Writing small methods and making test cases is the best way to isolate bugs easily (and avoid them altogether). If you need to resort to the debugger you can, but it's no replacement for writing several small helper methods, testing it, and then calling it to help with a larger task!

Note that many other IDEs have debuggers, which probably include more features than BlueJ's. For example, Eclipse's debugger lets you set "watch points": “keep running until my variable x changes”. From the command-line, you can run the debugger jdb.

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.11 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme