Download - DrJava Debugger by Dr. lee 10/2008

Transcript
Page 1: DrJava Debugger by Dr. lee 10/2008

DrJava Debugger

by Dr. lee10/2008

Page 2: DrJava Debugger by Dr. lee 10/2008

Tracing Execution

• One way to trace what is happening in your program is – To add System.out.println() statements

• Add these to print out the value of the current object’s name before and after the name is initialized in the constructor that takes a name– System.out.println(sum);– sum = sum + myScore;– System.out.println(sum);

Page 3: DrJava Debugger by Dr. lee 10/2008

Sample Program 1

public class Sample1 { public static void main(String[] args) { int sum = 0; int myScore = 100; System.out.println(sum); sum = sum + myScore; System.out.println(sum); } }

Page 4: DrJava Debugger by Dr. lee 10/2008

Debuggers

• You can use a debugger to find the cause of bugs (errors in your program)– A moth caused one bug– http://www.jamesshuggins.com/h/tek1/

first_computer_bug.htm

• And to trace execution to see what is happening– Which constructor is executed or what

method is executed– What values are in the fields

Page 5: DrJava Debugger by Dr. lee 10/2008

Open a File in DrJava

• Open DrJava• Open the file “TestCircle1.java” in the directory

“C:\CS202-DrJava\CS202-Liang-Textbook-Programs”

Page 6: DrJava Debugger by Dr. lee 10/2008

DrJava’s Debugger

• Click on Debugger in the menu– Then check the Debug Mode checkbox

Watches and Breakpoints Area

Stack andThreads Area

Check values here

Page 7: DrJava Debugger by Dr. lee 10/2008

Setting a Breakpoint

• When you use a debugger you often want to set places to stop execution– Each place to stop at is a breakpoint

• Once execution has stopped there– You can check the value of parameters and fields

• To set a breakpoint– Right click on a line of code, then click “Toggle

Breakpoint”– Or click the “Debugger” icon, then click “Toggle

Breakpoint on Current Line”– It will be highlighted in red

Page 8: DrJava Debugger by Dr. lee 10/2008

Showing a Breakpoint

• Lines with breakpoints are highlighted in red in DrJava

Page 9: DrJava Debugger by Dr. lee 10/2008

Testing a Breakpoint

• Type the following in the interactions pane

> java TestCircle1

• Execution should stop at the breakpoint and the color change to blue

Page 10: DrJava Debugger by Dr. lee 10/2008

Show myCircle.radius value• Key in myCircle.radius in the Interactions Pane• You will see “Undefined class ‘myCircle’ “, why? • Execution stops before the breakpoint line is executed

• So the class hasn’t been defined yet

Page 11: DrJava Debugger by Dr. lee 10/2008

Check myCircle.radius Again

• Click “Step Over” button, then key in myCircle.radius in the Interactions Pane again.

• What do you see? Why? The current line of code has been executed

Page 12: DrJava Debugger by Dr. lee 10/2008

Debugging Options

• Step Over– Execute the current line of code and then stop again before you

execute the next line of code

• Step Into– If the line of code that we are stopped at has a method call in it,

then stop at the first line in the called method

• Step Out– Execute the rest of the current method and stop at the first line after

the call to this method

• Resume– Continue execution at the current point

• Until the next breakpoint• Or the program ends

• You can quit debugging by clicking on the X

Page 13: DrJava Debugger by Dr. lee 10/2008

Ctrl-F to Show “Find” & “Replace” Windows

Georgia Institute of Technology