home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lect04a
tracing through a program
with a debugger, and by hand
At the start of class, we'll demo the on-line tool now available
through the tutor/PI link.
Group discussion:
- Make a new project for today's lab, and create a new class
PizzaServer2.
Paste in the contents of
our ol' Pizzeria class,
and compile it.
Examine the code for pizzaArea and crustArea
for a moment, to re-familiarize yourself with them.
- Debugger demo:
Together, we'll both (a) have some volunteers play the parts
of pizzaArea(double) and crustArea(double);
I'll play testPizzeria().
and step through each thing the computer does.
At the same time, everybody will follow along using
a debugger — a tool which lets you see what
the computer is doing step-by-step (among other things).
Click in the left-hand margin of the body of
crustArea (line 25 or so).
A red stop-sign should appear
— a breakpoint.
This means that when BlueJ is running a program and
encounters that line, it will stop.
(Unfortunately, you can't set a breakpoint next to
a local variable's declaration.
Instead, set the breakpoint next to the initialization,
on the next line.)
- In the Code Pad,
type in Pizzeria.crustArea(20).
Instead of running the entire code and returning the
answer near-instantly, you'll see the debugger window pop up,
because BlueJ reached a breakpoint (stop-sign).
You'll also see an arrow pointing to the line of your program
which the computer is about to run next.
- Look at the window that has come up.
It includes a panel for the function's local variables
(its “environment” — the sheet of paper
for each person, in our lecture-demo).
We see that the parameters have been initialized already,
as part of making the function call.
- Click step into. This runs one line of code.
Moreover, the “into” means that if that line
is calling another function we'll step into that
function (as opposed to stepping over it —
completing that entire sub-function call all at once).
- Click step into several more times.
Pay attention to the local variables window at each step,
as well as the call stack window.
The call stack window keeps track
of which functions are currently pending, awaiting
the result of a sub-function to complete.
Although these above instructions are specific to BlueJ,
other IDEs (eclipse, JCreator, etc.) have debuggers which work similarly.
In general:
when function A is running and calls
function B, A is temporarily
set aside (“put on the stack”),
and B runs.
If B were to call another function C,
then both A and B would be
pending on the stack while C runs.
Once C finishes, then B would
resume, and when B finishes and hands its
answer back to A, then A can
resume at the line where it left off.
We will spend the remaining time reviewing the bits of syntax we've learned
over the last five weeks.
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs