IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
Eclipse comes with a debugger which allows you to execute your code one statement at a time, see the values of variables, and stop the execution of the program at any point. The debugger is a very powerful and useful tool to help you find errors in the code. In this exercise you will use the debugger to trace through the program DebuggingExercise.java.
DebuggingExercise.java
and run it. You
will see that it compiles but it crashes when you run it and the java virtual machine
prints an error message. Read the error message and try to understand it. It will give
you some information that you can use to fix the code.for (int j=1; j<=6; j++)
. You do this by
right-clicking in this line at the very left, and then clicking Toggle
Breakpoint. You can remove a breakpoint at any time in the exact
same way.for (int j=1; j<=6; j++)
is highlighted
in green. The execution of the program has stopped at this point. In the
Variables window (upper right corner) you will see the variables of the program:
args
, testArray
, and i
and their values.
Since testArray
is a variable referencing a 2-dimensional array,
or an array in which every entry is an array of integers,
when you click on the ">" symbol to its left, the debugger shows 5 new variables
([0], [1], [2], [3], and [4]), each one of them references an array of length 6;
these are the rows of the 2-dimensional array.
If you click on the ">" symbol to the left of [0], six new variables will be shown;
these are the columns of the first row of the two dimensional matrix.i
and j
,
and the values stored in the 2-dimensional array
are changing in the Variables window. Why did testArray[0][0]
not change?
testArray[0][5]
changes value
from 0 to 5 and the
statement for (int j=1; j<=6; j++)
is highlighted in green. Push key F5 one more
time. What is the value that j
has? What value does i
have?
Is it correct that the program tries to store the value (i+1)*j
in
testArray[i][j]
? Write your answers in AnswersLab3.txt
testArray
are properly
initialized. The first row of testArray
should store the values 1, 2, 3, 4, 5 6;
the second row should store the values 2, 4, 6, 8, 10, 12, the third row 3, 6, 9, 12, 15, 18,
the fourth row 4, 8, 12, 16,20,24, and the last row 5, 10, 15, 20, 25, 30.print
or
println
statements to the code that you are testing.
DebuggingExercise2.java
and run it. It will also crash.
i = process(2);
and keep pressing F5 until
you reach the statement return result;
(do not execute this statement yet).
Using the debugger find out what the values of the variables i
, step
,
result
and load
are. Write these values in
AnswersLab3.txt
.load
is a static
variable, to see its value in the Variables window
you need to click on the little inverted triangle in the upper right corner of the
Variables window; this will bring two options: "Layout" and "Java". Click on Java and then
select "Show static variables" (see figure at the end of this document).
DebuggingExercise2.java
and stop execution at statement
i = process(2)
. This time press the key F6 instead of the key F5. You will see that
the debugger now does not go inside method process
, but it goes directly to statement
total = (i * 100) / load
. Key F5 tells the debugger to "step into" a method, while key F6
makes the debugger "step over" a method. What is the value of load
? Why does the
program crash if you keep pressing F6? (Do not press F5 or the debugger will try to step into the code of the Java's virtual machine). Write your answers in AnswersLab3.txt
.
i = process(2)
to i = process(1)
and run the program again; this time it must print "The value of total is 5".load
is 0 before computing total = (i * 100) / load
.
DebuggingExercise4.java
and run it. It will not compile.
i
has been declared in the statement for (int i = 1; i < 2; ++i)
AnswersLab3.txt
.i
?
var1
and obj1
are immediately after method2(obj1)
is executed. Write your answers in AnswersLab3.txt
. Now try to determine the values of the variables immediately after method1(var1)
is executed. Write your answers in
AnswersLab3.txt
.
AnswersLab3.txt
. To see the string stored in obj1
, in the Variables window
click on the ">" symbol to the left of obj1
; you will see now the value of name
.
if (obj1 == obj2)
. Press F11 to run the program in debugging
mode and stop at this statement.
false
even though both obj1
and obj2
contain the same information, namely "joe"? Write your answer in AnswersLab3.txt
.
obj1
in the Variables window of the debugger. Since
obj1
is a non-primitive variable its value is the address of an object of the class
MyObject
. Note that the debugger does not show the actual address of the object
referenced by obj1
; instead it displays the address in a user-friendly format:
MyObject (id = 19)
—you might get a different value for id
. This means
that the address stored in obj1
is the address of an object of class MyObject
and the debugger assigns it a unique internal identifier (in the above example the identifier is 19).
If you were to print the value of obj1
with System.out.println(obj1)
you would
get a similar output, not the actual address of the object but a string of the form
MyObject@5ccd43c2
; the java virtual machine will assign a different identifier to the
object (5ccd43c2
) than the debugger.