Using the Debugger 1 – Intro to Java Programming

It’s a fact of life that no matter how hard we try our programs have bugs. In the olden days, computers had real bugs. For example, the moth that you see here got trapped in a relay of the very first computer in the United States causing it to malfunction. But of course, nowadays, the bugs are usually the fault of the programmer by not having thought through everything. And that’s normal. You can find simple bugs by putting print statements into your code, printing out everything in sight. Seeing what goes wrong, and then removing the print statements again, but that’s no fun. There’s a nifty software tool called a debugger, that you can use to control your program. Stop it and look around, and then find out what it actually does as opposed to what you think it’s been doing. Using a debugger is pretty simple, there are three key concepts that you need to master. You can set breakpoints in your program, and then that the debugger will stop when the program reaches a breakpoint. You can then step through your program one instruction at a time. And you can even step inside method calls. And finally, you can inspect the contents of variables. So if you have a variable, you can ask the debugger what’s inside at this point. So, these are the three key concepts that you want to keep in mind. Let me show you how to do these in BlueJ. This example program, tries to count the syllables in a word. Let me show you. enter a few words. And as you can see, the program doesn’t seem to be doing so well. Hello has two syllables, not one. Yellow also has two. Oh, peach has one. Before debugging this program, let’s have a quick look at algorithm is used to count the syllables. Here is our algorithm. We look for groups of vowels. There’s one here, another group of length one, and another one, and we count the groups. This word has three syllables. However, here we’ll count this group, but we won’t count an “e” at the end, because nice only has one syllable. That second rule trips us up with some very short words such as the. We’re not counting the E would give us zero syllables and we’ll say if that happens we’ll change the count to one. To summarize, we count vowel groups but not a final e and if that count gives us zero, we change it one. Now on, just seeing the debugger in action. Open the syllable counter program in BlueJ, and I really suggest that you do that with me. Click in this column here next to the first line of the main method and you’ll see a tiny stop sign, that’s a break point. Now, run the program in the usual way, and now the debugger wakes up. We’ve hit this break point and what you see over here is the console that controls the debugger. These buttons here, let you step through the program. Let’s try it out. We’ll click on step and you can see that we’re now in the next line. We click on step again, oh, now it has produced the output here. And we’re back where the little arrow is. Now when we step nothing seems to be happening, the arrow is gone. That’s because right now, we’re reading input. Let’s look at the terminal window and here it is waiting for the input. I’ll just apply it. Now I hit Enter, and you can see the debugger window again. And back here our program has advanced by one step. Let’s bring this to the front. And we’re past here. Now to make sure that you are trying this at home, let me ask you to do exactly what I did. Set the break point here, enter hello yellow peach, keep signal stepping. And I’d like you to tell me, how many times are you hitting this line, the call to the constructor, before you get out of the loop.

One thought on “Using the Debugger 1 – Intro to Java Programming

Leave a Reply

Your email address will not be published. Required fields are marked *