To use the debugger, choose
Project -> Debug Project
At the bottom of the window you should notice some new icons. These will allow us to interact the debugger. We also can use the Run menu as well.
The set of buttons to the left of the output area (bottom most section of the window) controls what will be displayed there. One of the icons there looks like a small computer screen. If we click on this icon we get the console for the Java program. Any output of the program will be here. If we need to type input we will do it here as well. One of the buttons is a small red hexagon. This allows us to view any breakpoints that we have set. A third button is a pair of glasses and lets us view the values of variables that are being watched.
We also have a row of buttons that are the basic controls for the debugger.
Below this are the two controls for the application. The green triangle will allow us to run the application. The red box allows us to stop executing the program.
Use the controls to place a breakpoint on the class TaxTotals. As the code runs you will see a pictograph that moves through the source indicating the line of code that is about to be executed.
Start the execution. You should be at the first line of code in TaxTotals. Begin by using step over to move to the first statement, which is an output statement. Rather than stepping into the call to readInt(), use step over to move to the next statement. The debugger will wait for you to enter the number of employees. Click on the screen button so that the output area is the console and type your input value in there.
Using step over, step through the remainder of the program, one line at a time. If necessary, you can always stop execution and the begin again.
Remember that when you reach the input statement, you need to enter data in the screen window. Enter the data (e.g., 3) and continue stepping. In particular, watch how the program behaves when execution reaches the for loop.
A nice feature of this debugger is that we can look at the values of variables while the code is running. Click on the Add Watch button. Choose a variable in your program and enter its name into the text box. You can view that variable by clicking on the watch button left of the output area. When a statement executes that changes the value of a given variable, it will change in the display. This allows you to examine the values of your program's variables and see how the execution of a statement affects them.
Note also that when you reach the end of the for loop, step over takes you back to its beginning. Keep stepping through the statements until you understand how a loop causes a sequence of statements to be repeated.
As was mentioned earlier, you can always restart the debugger at the beginning of the program by choosing clicking the stop icon followed by the start icon.
While there are situations where we want to step over a method (such as a module method like readInt() in class Integer), there are other situations where we want to step into the method. For example, to examine the behavior of our computeTotalCost() method, we might want to step into it, rather than over it.
To illustrate, run your program using step over and enter some value less than the luxury tax minimum for cost and a value for rate. Then continue to step over until execution reaches the line where computeTotalCost() is called. If you now step into computeTotalCost(), the line being executed should "jump" from the call in TaxTotals.java to the first line of method computeTotalCost() in Tax.java. Look at the watch display. The objects from the main function cannot be accessed within computeTotalCost() and no value will be displayed for those variables.
You can then step through this method using the step over or step into methods as we have seen before.
Note how the program behaves when execution reaches the if statement: the code that computes "normal" cost is performed, while the code that computes luxury tax is skipped. Continue stepping until you reach the end of the method. When you step again, you should return to the statement where computeTotalCost() was called.
Continue stepping through the program, and when it pauses for input again, this time enter a value greater than the luxury tax minimum for cost (and a value for rate). When the program stops at computeTotalCost() again, step into computeTotalCost() and note that this time control skips the 'normal' part of the if but moves through its luxury tax part.
As we said earlier, if at any time you want to leave a method (this is especially useful if you mistakenly step into a system method), choose the step out operation. This will complete the execution of the method currently executing and stop back in its caller.