In this lab, you will learn how to use the debugger along with a bit about reference variables and parameter passing.
Wouldn't it be helpful to see the Java code being executed one line at a time? This is something we can do with the debugger.
A debugger can step through your program code while you examine the values of your program variables. You can also set a breakpoint on a line of code of your program. Your program executes normally until it reaches the breakpoint at which point the program is halted so that you can examine its variables.
Let's start by setting a breakpoint in the
Do this by placing your cursor
anywhere within the line that says:
rating = PromptIO.ReadInteger("Enter a teacher rating... ", 0) ;
Next right click while the cursor remains on the
line of Java code. You should see a new
Select Toggle Breakpoint from the menu.
A pink rectangle should now surround the selected line.
You can start debugging from the Project pull-down menu by selecting Debug Project. or you can just select the Run in Debugger button, .
Your program starts, screen flash, and soon you should see
that the line with the breakpoint now has a green background
and is the target of a little green arrow. Your breakpoint
has been reached.
Be sure that the left panel is displaying the Debugger Window. If not, select the Debugging tab.
At the top of the Debugger Window is a line of nine
Start by turning off all of these nine selections, so that
your icons display as below. These icons control your view of
your program and its variables. We're going to experiement
with these controls. In the following few paragraphs
turn on and off each icon to see its effect. By the way,
it is possible to have several icons selected simultaneously.
The first icon shows the status of your debugging sessions. It should show that your DebugExample session has been stopped on a breakpoint. It is unlikely that you will ever intentionally have more than one active debugging session while a student in this course.
The second icon
shows the status of your program's threads.
It is possible to have more than one Java interpreter simultaneously
executing the methods of your program. In fact, multi-threaded
applications are required for interesting multi-media applications
and you have undoubtedly encountered many on the web.
Right now, your application may appear to have only one thread,
but push down on those expanders, and you'll see more.
The third icon shows your call stack. This can be used to determine the series of method calls that were made to reach the breakpoint.
The fourth icon displays the local variables of a active method invocation. It is possible, though rarely advisable, to change the value of a variable in this display. Because an object may be passed between method, it may appear in many different method invocations.
The debugger may be working on several sessions.
Each session may be executed by several threads, each with its own
Each call stack may have several method invocations, each with its
own set of local variables.
The fifth icon, called All in One, puts this entire
hierarchy into a single display.
Press the expanders in the All in One window to
descend to the depths of your Java program.
The sixth icon controls watches. A watch is a variable the debugger is actively tracking. This used with a programmer thinks a particular variable is causing problems and requires special attention. The value of that variable is displayed in the watch list.
The seventh icon allows you to lists the fields, constructors, and methods of your active classes. Many classes are within packages and you must descent through the class hierarchy to uncover them. Press the Classes icon. Notice that the class Teacher is not displayed. That's because your program has yet to instantiate an object of class Teacher.
The eighth icon provides a quick list of breakpoints.
The ninth icon is for properties, a mechanism for internationalizing the text displayed by a Java application. You won't use them here.
When you reach a breakpoint, you can start stepping through the code. For each line of code, you can either:
Presumably your debugger is still waiting at the breakpoint. Go into the Local Variables view where you should see nothing put a single variable args.
Contine to step over lines of code until the first call to the
zeroRating() method is highlighted. By now,
you should have a teacher reference variable
in the local variables display. Click the expander to
teacher to view its instance variables.
Note that some of the instance variables are also references to objects that can be viewed. Explore all reference variables and make sure that you understand what you see.
Now use the Step Into button, or the
F7 key, to step into the
zeroRating() method. Display both the call stack and
local variables windows and switch between the two sets of local
variables by choosing between the two frames of the call stack. You
choose a frame by double-clicking on that frame in the call stack.
Advance to the "}" marking the end of the zeroRating() method.
Show your lab instructor the variables in both
main(). Explain why the value of
main() is not changed by the call to
Advance until the second call to
zeroRating() is highlighted,
and then step in to
zeroRating(). Again, observe the
call stack and local variables display. Click
the expander to view the object referenced by the
Advance to the "
}" marking the end of this
Show your lab instructor the variables in both
main(). Explain why the
object created in
changed by the call to