DrJava Debugger

Download Report

Transcript DrJava Debugger

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);

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); }

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_co mputer_bug .htm

• And to trace execution to see what is happening – Which constructor method is executed or what is executed – What values are in the fields

Open a File in DrJava

• Open DrJava • Open the file “ TestCircle1.java

” in the directory “ C:\CS202-DrJava\CS202-Liang-Textbook Programs ”

DrJava’s Debugger

• Click on Debugger in the menu – Then check the Debug Mode checkbox Watches and Breakpoints Area Stack and Threads Area Check values here

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

Showing a Breakpoint

• Lines with breakpoints are highlighted in red in DrJava

Testing a Breakpoint

• Type the following in the interactions pane > java TestCircle1 • Execution should stop at the breakpoint a nd the color change to blue

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

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

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 then stop at the first line in the called method call in it, • 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

Ctrl F to Show “Find” & “Replace” Windows Georgia Institute of Technology