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.
Open PhoneCard.java by clicking on File,
pulling down to Open, and selecting the appropriate
file and directory.
Open Driver.java in the same fashion.
2. Review:
This part of the lab will review a few topics related to object-oriented
programming in Java.
In the main() method in the Driver class,
what kind of objects are end, now, and
start?
They are Date objects.
In the main() method in the Driver class,
what kind of object is card?
A PhoneCard object.
Where is the code for the PhoneCard class?
In the current working directory.
Where is the code for the Date class?
It is in the Java library (in the package java.util).
Search on-line for the documentation for the
Date class.
Make sure you find the documentation for the Date class that
is in java.util. (There are several Data classes
in the Java library.)
When you construct a Date object using the default constructor
(i.e., the constructor that has no parameters), what properties
will it have?
It will contain the current date.
Where can you find information about the methods in the
PhoneCard class?
From the source code for the PhoneCard class.
(It would be nice if we had the documentation in an HTML file, but
the professor was too lazy to generate it.)
3. Setting-Up jGrasp for Debugging:
Debuggers require special versions of .class files
that are created by "compiling in debug mode". To make jGrasp
compile in debug mode:
Click on the title bar of the window containing
Driver.java to make sure that it has the focus.
Click on Build and "check" Debug Mode
(if it is not already "checked").
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). For example:
Click on the title bar of the window containing
Driver.java to make sure that it has the focus.
Click in line 33 of Driver.java (the first
if statement) to move the cursor to that line.
Right-click in line 33 of Driver.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 33.
Click on Build and pull down to Debug.
This will run Driver and stop the execution at the
breakpoint (i.e., line 33).
What happened?
A blue arrow appeared on the left side of line 33 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 "tree icon" next to Locals to expand it.
What is the current value of availableMillis?
5999999
Enter an expression involving availableMillis in the
"Eval" tab (e.g., availableMillis/2).
Note that the value of this expression will change whenever
availableMillis 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).
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. For example:
Click on the button until it is
highlighted (i.e., darker).
This will cause the execution to
step one byte-code at a time.
Click on the button.
What happened?
Nothing seems to have happened. In fact, one byte-code was executed. So,
the state information could have changed.
Repeatedly click on the button
until something obvious happens.
What happened?
The blue arrow and highlight moved down to the next executable statement.
Click on the button. This will resume
normal execution. (In this case, it will run to the end of the application
since there are no more breakpoints).
Click on Build and pull down to Debug.
This will run Driver again and stop the execution at the
breakpoint (i.e., line 33).
Click on the button until it is
not highlighted. This will cause the execution to
step one statement at a time.
Click on the button.
What happened?
The blue arrow and highlight moved down to the next executable statement.
highlighted.
Click on the button until the next
if statement is highlighted.
What is the current value of availableMillis?
5399999
Click on the button to run to the
end of the application.
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.
For example:
Click on Build and pull down to Debug.
This will run Driver and stop the execution at the
breakpoint (i.e., line 33).
Click on the button.
Click on the button again.
What happened?
The blue arrow and highlight moved down to the next executable statement.
highlighted.
Click on the button.
What happened?
The blue arrow and highlight moved into the startCall() in the
PhoneCard 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 startCall() method in the PhoneCard class.
What line is currently being executed?
102
Where was this method called from?
The main() method in the Driver class (line 35).
Click on the "tree icon" next to this to expand it.
What is the current value of balance?
10.0
Click on the button.
Click on the "tree icon" next to callNumbers
to expand it.
What is the current value of callNumbers[0]?
"540-568-1671"
Click on the "tree icon" next to callStarts
to expand it.
What is the current value of callStarts[0]?
Since callStarts[0]
is a Date object, it is a reference type and
it should contain a reference. jGrasp displays an ID instead.
Why does it have that value?
Since callStarts[0] is an object
it can have many attributes, making it somewhat difficult to display.
You can either expand it or right-click on it and pull down to
View by Name to see its attributes.
Click on the button twice.
What happened?
Execution returned to Driver.
Click on line 46 in Driver.java (i.e., the line
that constructs a Date).
Click on the button. This will
run the application to the cursor.
Click on the button.
Why didn't the debugger step into the Date constructor?
The debugger doesn't have the source code for the Date class.
Click on the button to run to the end.
8. Using the Debugger with Code You Wrote:
In this part of the lab you will use teh debugger in jGrasp to execute
some of your code.
Copy the working source code from one of the programming assignments
to the working directory.
Set a breakpoint on the first line of main().
Execute your code one line at a time, monitoring the values of the
important variables as you do so.
Based on this experience, why are these features collectively
called a "debugger"?
Because they make it easy to trace the execution of a program and, hence,
find any bugs.