CS 151 Fall 2007 Lab 7

Debugging in BlueJ

 

The goal for this lab is to learn a little bit about debugging with BlueJ.  We will do so using some source code and an exercise from Sect. 6.6-6.7 of your book.  If you didn't bring your book you should still be able to complete the lab.  With the instruction below and some help from the TA and me.

 

  1. Open BlueJ either by selecting it from the Dock or navigating to it in the Applications folder and double clicking it.

  2. Mount your network directory/folder so that you can access the lab after you leave Olin 323.  Go to the Finder and from the Go menu select Connect to Server....  Type afp://fileserver1 or choose it from the list of selections and select the Personal volume when prompted.

  3. Download Lab7.zip and copy/drag it into your personal network folder.  Double click it to uncompress the lab7 project that it contains.  Start BlueJ if you haven't already and open the lab7 project.

  4. Compile both classes by clicking the rectangular compile button in the project window.  Run the main method of the Syllable counter class to see what it does.  When prompted in the terminal window, enter a short phrase/sentence that ends with a period.  Run it again and type: "hello yellow peach." (without the quotes) for your input.  Is the output what you expect?  Probably not, which means there is at least one error in the code that we need to first find and then fix.  The BlueJ Debugger will help us in this task.

 

  1. Open the Word class.  If you don't see line numbers down the left hand side in the window, from the Options menu choose "Preferences".  Click on the Editor tab if it isn't active and make sure that the "Display Line Numbers" option box is checked.  Click the OK button to return to the editor window for Word.

 

  1. Scroll down the Word class code to the countSyllables() method.  Point to the line number 42 in the very left of the window (the line is: int count = 0;).  Click on the number 42, and a red stop-sign icon should now appear where the line number was previously.  If you single-click on that icon it will disappear.  Single-click once more to add the icon back.  Alternatively, you can toggle breakpoints on/off by clicking anywhere in the line and selecting "Set/Clear Breakpoint" from the Tools menu.  A breakpoint tells BlueJ (more generally any debugger) to run the code in the specified method/constructor up to but not including the line with first (there can be multiple ones) breakpoint.  At that point the debugger is waiting for your instructions about what to do next.  Note that you can only set breakpoints in a class AFTER COMPILING the class.

 

  1. Run the SyllableCounter class' main method.  When prompted in the terminal window enter the "hello yellow peach." input again.  The BlueJ Debugger window will appear and in the editor window for the Word class, line 42 will be highlighted and you will see an arrow on top of the stop sign icon.  The arrow indicates which line will be executed next.  Note the information that the Debugger window provides you.  You can see the sequence of method/constructor calls that led to the breakpoint, the value of instance variables, static variables, and local variables.

    More information about the BlueJ Debugger can be found in the BlueJ Tutorial (reachable from BlueJ by selecting "BlueJ Tutorial" from Help menu) and in the BlueJ Reference Manual.

 

  1. Step through the method one line by clicking the "Step" button once for line.  Observe the appearance of the local variables after each line is executed.  Do this until you get to line 48 by which time count, endPos, and ch have all been created and assigned values.  Notice that the text variable holds "hell", endPos = 3, and ch = 'l'.  We would expect text = "hello", endPos = 4, ch = 'o' so it looks like a problem with the constructor. 

 

  1. Click the "Continue" button to advance to the next breakpoint.  You will again return to the same breakpoint the next time the countSyllables() method is called.  However, the second time the instance variable text has the value "yello", i.e. the second word of your input. Click "Continue" twice more to finish the execution.  Note that you can add and remove breakpoints while in the middle of a debugging session.

 

  1. Look at the constructor for the Word class and see if you can find the problem.  First remove the breakpoint from the countSyllables() method by clicking the stop sign icon.  Now add a breakpoint at the beginning of the constructor (line 14).  Run the main method again with the same "hello yellow peach." input.  Step and observe the values the local variables get until you get to line 23 where instance variable text is assigned its value.  Are startPos and endPos set to what they should be for the word "hello"?  Step one more time so that text is assigned a value.  What is the problem?

 

  1. After fixing the problem rerun with the same input.  It's no good to change your input before verifying you actually fixed the problem you were working on.  There still appears to be a problem.  Remove any all breakpoints in the Word class by clicking them individually or clicking "Terminate" in the debugger window (this clears all breakpoints).  Add a breakpoint on the first line of countSyllables().  Lets try to use a simpler input to get at the problem.  Run the main method again with only "hello." as the input.

 

  1. When the breakpoint is reached Step through the method line by line.   Note that on the fifth iteration (corresponding to the letter "o") the first if loop is entered, but the second if is not entered.  Why?  The reason is the insideVowelGroup boolean variable is still true.  It was never reset to false at the end of the previous vowel group that occurs when the first "l" is encountered.  Click on Terminate in the debugger to stop the debugging session and program running.

 

  1. To fix the problem modify the existing outer if statement in the countSyllables() method so that it has an else clause that resets the variable insideVowelGroup to false every time a consonant is read.  Why are the curly braces necessary?

 

 if ("aeiouy".indexOf(ch) >= 0)

{

...

}

else

insideVowelGroup = false;

 

Run the main method again with "hello" first and then with "hello yellow peach."

 

  1. If you have time, add a message that prints out the number of words that the user entered and the entire original string that was entered.  Can you figure out a way to print out the number of words and the entire sentence before the syllable counts for each word are displayed?  Hint: first read in all the words, store them in a single string variable.  Then, use the split() method to create an array (list) of Strings (see the syntax below).  You can learn the length of the array (the number of items in the list) by accessing the array's length attribute (not method).  Access entry number 0, 1, 2, etc. of the array (they are indexed like characters in Strings) by using the position in square brackets after the array name.  Finally, create Word objects for each entry of the array and continue the code as before.

    // uses a blank space to split the string up into words (tokens is another term people use)
    String[] varName = "This is a word split test.".split(" ");

    // print out each word on a separate line (spaces were removed during splitting)
    for (pos = 0; pos < varName.length; pos++)
                System.out.println(varName[pos]);