Espresso: A Concentrated Introduction to Java
Summary: In today's laboratory, you will explore a powerful software development tool: the debugger.
Contents
a. Start Eclipse, and create a new package within the Code
project. I'd suggest the name username.debug.
b. Copy the following files into the directory associated with your new package:
c. Refresh the package, so that Eclipse will recognize the files.
d. Modify the source files, so they will run in your package. Be sure to
leave TimeWaster.java open in the editor window.
a. In the upper-right corner of the Eclipse window you should see an icon labelled Java and another icon that looks like some sort of window with a plus sign in the corner. Hovering over these icons with the mouse brings up labels Java perspective and Open perspecive, respectively. You may also see an icon here labelled Debug. If not, click on the Open perspective icon and select Debug perspective.
In Eclise, a Perspective is simply a new configuration of windows and tabs set up for a particular purpose. The windows you now see are useful for debugging code.
When you are ready (but not right now), you can return to the Java
perspective in the same way.
(You may need to click on the >> symbol to make the Java
icon visible.)
b. Click the mouse into TimeWaster.java in the editor
window, so that it "has focus". Now select Debug As and then Java
application from the Run menu.
You should see that TimeWaster.java is printing output to the
Console window. Taking a look at the source code, you will see that this
program will run for a long time. We have some time to play.
c. Find the window labelled Debug in the upper left of the Eclipse window. Now find the toolbar of icons associated with the Debug window. Press the Suspend button (two vertical yellow bars), and notice its effect on the program. Now press the Resume button (a yellow bar and a green arrow). Finally, press the Terminate button (red square).
If these icons are "grayed-out" so that you cannot select them, please let me know! We should be able to correct this by double-checking some of the Eclipse settings.
a. Notice that there is a blue vertical bar at the far left of the editor
window. Double-click in this region at the same height as the first
call to System.out.println. This sets a breakpoint at
this line of code.
Now run the program in debug mode again. You should see that execution of the program is automatically suspended at the breakpoint. In fact, execution is suspended just before executing the associated line of code.
Insert another breakpoint at the second call to
System.out.println, and then click Resume to resume
execution of the program.
Click Resume a few more times. Do you understand what is
happening?
b. Find the Breakpoints window in the upper right of the Eclipse window. Each of the breakpoints you have set should appear in this window. Note that the source code line number is given with each breakpoint. Further, notice that if you click on a line in the source code, its line number is displayed at the very bottom of the Eclipse window.
Disable the breakpoint at the second call to
System.out.println by right-clicking on its display in the
Breakpoints window and the selecting Disable. Resume execution of
your program, and notice the effect.
Enable the breakpoint again in the analogous way.
Remove the first breakpoint, either by selecting and removing it from the Breakpoints window or by double-clicking on it in the source code window.
If you stopped TimeWaster at the end of the last exercise,
start it again.
Move the mouse over one of the variable names in the source code window. You should see a pop-up display, showing the value currently stored in that variable.
Find the Variables window in the upper right of the Eclipse screen. It may be behind the Breakpoints window. If so, bring it to the front. You should see a list of the currently defined variables, along with the value stored in each of them.
Click Resume a few more times and watch the values change.
Now click on one of the variables in the Variables window. The value of that variable should appear in an editable region at the bottom of the Variables window. Change the value, and then save the change with Ctrl+S. Notice that the value displayed for the variable (and the value stored in the program's memory) has been updated.
If you stopped your program at the end of the last exercise, start it again.
Find the Step Over icon on the toolbar associated with the Debug
window. Click it a few times. You should see the debugger step through the
source code one line at a time. Note that it steps "over" any method calls,
such as System.out.println. This means that it treats the
method call as a single line of code, landing on it and then moving on,
just like any other line of code.
Now find the Step Into icon. Click it a few times. This, too, will
cause the debugger to step through the code line by line, but now it will
step "into" any methods that are called. This means that following each
method call, it will step line by line through the source code of the
associated method. Note that you can step into and view
the source code of classes provided by Java, such as
java.lang.String.
When you tire of this exercise, find the Step Return icon.
Each time you press Step Return, execution will continue through
the current method and stop just after the method returns. Depending
on how many methods you have stepped into, you may need to press this button
several times before you are returned to the TimeWaster main
method.
(If you are someone who prefers keystrokes over the mouse, you will be pleased to know that the functionality we explored in this exercise can also be accessed as follows: Step Into (F5), Step Over (F6), Step Return (F7).)
If you have not already done so, terminate execution of the
TimeWaster.
Create a new class ViewArray in the package
username.debug. (Although you can do this within
the Debug perspective, it is
probably easiest to return to the Java perspective. This allows you to
highlight the package in which the class should be created in the
Package Explorer window.)
Write code in ViewArray that declares an array of 10
int's and then uses a for-loop to load the numbers 100
through 109 into the array elements.
Set a breakpoint on the line of code containing the for-loop control information. (You can do this from within the Java perspective.)
Now run the program in debug mode. (Although it may take a few moments, Eclipse should switch to the Debug perspective, and suspend execution at the breakpoint.)
If the Variables window is not visible, bring it to the front. You should see your array's name displayed with a triangle icon to its left. Click on the triangle icon to cause it to point downward. You should now also see the values stored in each array element.
Step through your for-loop line by line, so that you can see the array elements get loaded.
Edit the main class ViewArray such that it also declares a
Fraction object and then prints out its value. Set a new
breakpoint on the line of code in which the Fraction is printed.
Now run your program again in debug mode. When it is suspended after
generating the Fraction, you should also be able to see the
values stored in the Fraction's fields within the Variables
window.
Open the source file for the class DebugMe that you
downloaded in Exercise 0 of this lab. This program contains multiple
errors. Track them down and fix them with the help of the Eclipse debugger.