|
CIT 594 Forté Tutorial III: Using the Debugger |
In this tutorial you will be asked to download a Forté program. The program creates a random (but sorted) array of ten integers, and does a binary search for a given integer in the array. The binary search does not work correctly. The purpose of the tutorial is to demonstrate the use of Forté's built-in debugger.
http://www.cis.upenn.edu/~matuszek/cit594/Programs/binsearch.jar
(by right-clicking on it and choosing Save Target As...).jar xvf binsearch.jar)
or any zip utility. This will give you a folder named BinarySearch. Mount Filesystem...
to mount the BinarySearch folder. (See previous tutorials if
you don't know how to do this.) Open the binary search project. binarySearch method--it should begin on line 134. if statement
in binarySearch (this should be line 135) by right-clicking anywhere
on the line and choosing Toggle Breakpoint. The line should turn
bright red.
Toggle Breakpoint command should more properly be called
Add/Remove Breakpoint; it adds a breakpoint if there is none, or
removes on if there is.int
binarySearch(...)), it looks like you can do it, but the breakpoint
won't actually work, because there isn't any executable code there--it's
just a description of the method. Debugging tabbed
pane. This opens a different set of windows: Source Editor (which
is available in other tabbed panes as well), Output Window, and
Debugger Window. Also notice that the set of debugging icons (immediately
above the Debugging tab) has expanded. (This step can be
omitted.) Debug -> Start from the main menu,
or click on the down-arrow/green-bug icon, or hit F5. Do not choose
Build -> Execute or click the green triangle; this
will run the applet as usual, without starting the debugger. New Array button (more
than once, if necessary) and type in a number that the binary search should
find, but can't (a number that only occurs in position 0, 3, 6, or 9). When
you click the Search button, the program will pause at your breakpoint
and the debugging windows will appear (if they weren't already visible).BinarySearch.binarySearch: 135, which is where you
are: class, method, and line number. Click on the key next to it,
and you will finally see something really useful: your variables including
array, left, right, and target,
and their current values. (Why doesn't it show middle?
Look at the Source Editor window for a clue.) array shows you the
values in the array.this gives you more
information than you ever wanted to know about this BinarySearch
object. left. Don't click
on the variable, just let the mouse hover over it. After a second, a tiny
window pops up to show you the value of the variable. Test whether this trick
also works for target, array, and middle.
This technique is too slow for variables you want to examine often, but it's
handy for examining variables that you seldom look at.left and choose Add Watch.... The window that
appears should have left in its text field (if not, you can type
it in). Click OK. Do the same for right and middle
(and array, if you like). Now go back to the Debugger Window
and click the Watches tab. Notice what has been added. Debug -> Step Over from the main
menu, or press the F8 key. Notice that line 135 in the
Source Editor window has changed from blue back to red. The next line
(136) is now the blue line. The blue line is the one you are about to
execute; red lines indicate breakpoints. Blue covers red.middle is still undefined. Debug -> Step Over from the main
menu once again, or press the F8 key again. Now line 137
is blue.middle and given it the value 4.Debug),
an icon you can click, and a keyboard shortcut. Step Over [F8] -- execute this
complete statement. If the statement contains any method calls, "step
over" the methods--that is, the methods are executed, but the debugger
doesn't "look down" into their execution. (However, if the method
contains a breakpoint, the debugger will pause there and show it
to you.)Step Into [F7] -- start executing
this statement. If the statement contains any method calls, the debugger
"steps into" the method and pauses at the first executable statement
in the called method. If this statement does not contain any method
calls, Step Into is exactly the same as Step Over.
Step Out [Ctrl-F7] -- this gets
you out of a method that you have stepped into. Use this if you have stepped
into a method by accident, or if you have simply seen all you want to see
of that method.Run to Cursor [F4] -- puts a temporary
breakpoint on the line containing your cursor, and executes up to that point.
Use this to skip over a bunch of statements that you aren't really interested
in.Continue [Ctrl-F5] -- this executes
all statements up until the next breakpoint that it encounters. If no more
breakpoints are encountered, the program will continue to run until it is
finished (or you quit it). Use this command when you have seen all you want
to see at the current breakpoint.Finish [Shift-F5] -- use this
to quit debugging. Since you might have multiple debugging sessions going
at the same time, you will be asked to select which ones to quit. Chances
are there's only one, and it's checked, so just hit OK.binarySearch (there's
only one). If you have already spotted it, pretend you haven't, and play around
with the debugger some more.Delete (or just
hit the Delete key).File -> Save All).
If the command is grayed out, that's because Forté thinks the current
version is already saved; but it's wrong. Although debugger settings are
saved with the program, Forté doesn't realize that the debugger
settings have changed. Make some minor change to the program (I usually
add and delete a space), and try again.