Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
Getting Ready:
Before going any further, you should:
Depending on your development environment, create either a
directory or a project for this lab.
to an appropriate directory/folder. (In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above.)
1. Starting jGRASP:
Most integrated development environments (IDEs) include a debugger.
For this lab, you will use jGRASP. To get started:
Run jGRASP.
"Clean up" jGRASP by clicking on File and pulling
down to Close All+Windows.
Open Geometry.java by clicking on File,
pulling down to Open, and selecting the appropriate
file and directory.
Open AreaCalculator.java in the same fashion.
2. Review:
This part of the lab will review a few topics related to
programming in Java.
Where is the source code for the AreaCalculator class?
In the current working directory.
Where is the source code for the Geometry class?
In the current working directory.
Where is the source code for the JMUConsole class?
I don't know, I don't have it. I only have the byte code (i.e., the
.class file).
Do you need the source code or the byte code to execute a program?
The byte code. The Java interpreter executes the byte code contained
in the .class files. That's why we can use the
JMUConsole class even though we don't have the source code.
Where can you find information about the methods in the
JMUConsole class?
From the documentation (which is available from the course "Help"
page).
3. Setting-Up jGRASP for Debugging:
Debuggers require special versions of .class files
that are created by "compiling in debug mode". This part of the
lab will help you learn how to do this.
To make jGRASP compile in debug mode, click
on Build and "check" Debug Mode (if
it is not already "checked").
Click on the "button" for
Geometry.java to make sure that it has the focus.
Click on Build and pull down to Compile.
Click on the "button" for
AreaCalculator.java to make sure that it has the focus.
Click on Build and pull down to Compile.
4. Setting a Breakpoint:
One of the nice things about running an application in a debugger
is that you can stop the execution at one or more pre-defined
locations (called breakpoints). This part of the lab will
help you learn how to work with breakpoints.
Click on the "button" for
AreaCalculator.java to make sure that it has the focus.
Click in line 25 of AreaCalculator.java (the
if statement) to move the cursor to that line.
(Note: Don't worry if you don't yet know what in if statement
does, it doesn't matter at this point and we'll discuss it soon.)
Right-click in line 25 of AreaCalculator.java and pull down
to Toggle Breakpoint. (Note: You can also
set a breakpoint by clicking on the left-most edge of a line.)
What happened?
A red circle appeared on the left side of line 25.
Click on Build and pull down to Debug
and enter 4 when prompted.
What happened?
The program prompted me to enter the shape. Then the execution stopped
at the breakpoint (i.e., line 25), a blue arrow appeared on the left
side of line 25, and the line was highlighted.
5. Checking State Information:
Another nice thing about running an application in a debugger
is that, once you stop the execution at a breakpoint, you can
check state information (e.g., the value of attributes and variables).
For example:
Click on the Variables tab on the left side of the
jGRASP window.
Click on the "expand icon" next to Locals to expand it
(if it isn't already expanded).
What is the current value of shape?
4
Enter an expression involving shape in the
"Eval" tab (e.g., shape/2).
Note that the value of this expression will change whenever
shape changes. How might this kind of capability
help when debugging?
It's particularly useful when you want to determine what would happen
if you were to change a line of code (e.g., use a different calculation than
the one you are currently using).
Click on the "Variables" tab.
6. Stepping Over Lines:
When running an application in a debugger, once you stop the
execution at a breakpoint, you can continue the execution one "step"
at a time. This part of the lab will help you learn how to do this.
Click on the button.
What happened?
The blue arrow and highlight moved down to line 33.
Why did it move to this line?
Again, don't worry if you don't yet understand if
statements. What happens is that
shape has the value 4, so shape == 0 evaluates
to false which means that control enters the else block
(not the if block).
Click on the button to run to the
end of the application and respond to both prompts with the value 10.
7. Stepping Into Lines:
So far, all of the "stepping" you have done has been in one method
in one class. This is called "stepping over". You can also "step into"
a line of code to see what happens there.
This part of the lab will help you learn how to do this.
Click on Build and pull down to Debug.
Enter 0 when prompted.
Click on the button twice.
You should be prompted for the diameter. Enter 10 and
click on .
What happened?
The blue arrow and highlight moved down to the next executable statement
which is the call to circleArea() method in the
Geometry class.
Now, instead of clicking on the button,
click on the button instead.
What happened?
The blue arrow and highlight moved to the first statement in the
circleArea() method in the Geometry class.
Look at the "Call Stack". It tells you what class and method you are
in and where this method was called from.
What method is currently being executed (and what class is it in)?
The circleArea() method in the Geometry class.
What line is currently being executed?
20
Where was this method called from?
The main() method in the AreaCalculator class (line 29).
Click on the "Variables" tab.
What are the current values of the local variables?
diamater has the value 10.0 (which was passed to the
method). None of the other variables have been assigned values, so they
don't show up.
Click on the button (watching how the local
variables change) until control returns
from the circleArea() method in Geometry class
to the main() method in the AreaCalculator class.
What is the current value of area in the main()
method in the AreaCalculator class?
It hasn't been initialized yet since the assignment operation hasn't
been completed.
Click on the button.
What is the current value of area?
78.539... (which is the value that was returned from the
circleArea() method.
Click on the button.
Run the program again, but this time, try and step into
the statement that calls JMUConsole.print().
What happened?
Apparently, nothing.
Why?
Because the debugger doesn't have the source code for
the JMUConsole class. So, it can't show the source code
for the statements that are executing.
Click on the button and complete the run.
8. Stepping to the Cursor:
In this part of the lab you will learn how to step
to a particular line without setting another breakpoint.
Run the program in debug mode, again entering 0 for the shape.
Click on line 40.
Click on the button. This will
run the application to the cursor.
What happened?
The program resumed execution but stopped at line 40.
Click on the button to run to the end.
How else could you have accomplished the same thing?
By setting another breakpoint at line 40.
9. Gaining Experience with the Debugger:
To understand the value of a debugger, you need to use it to trace
the execution of a larger program than the one used in the previons sections.
This part of the lab will give you some experience with this.
Does a debugger actually debug code?
No.
What does a debugger do?
It instruments code and makes it easier to trace through it. It helps
facilitate the debugging process.
You may have been unable to remove all of the errors from the code
you wrote for a programming assignment. If so, use the debugger to step
through your code to try and identify the errors. (Obviously, you should
execute the code on a test that it failed, not a test that it passed.)
If all of your code you have written thus far has not contained any errors,
use the debugger to step through the most complicated program you've
written this semester.