JMU JMU - Department of Computer Science
Help Tools
Lab: Skills - Using a Debugger


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Depending on your development environment, create either a directory or a project for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above.)

1. Starting jGRASP: Most integrated development environments (IDEs) include a debugger. For this lab, you will use jGRASP. To get started:
  1. Run jGRASP.
  2. "Clean up" jGRASP by clicking on File and pulling down to Close All+Windows.
  3. Open Geometry.java by clicking on File, pulling down to Open, and selecting the appropriate file and directory.
  4. Open AreaCalculator.java in the same fashion.
2. Review: This part of the lab will review a few topics related to programming in Java.
  1. Where is the source code for the AreaCalculator class?


    In the current working directory.
    Expand
  2. Where is the source code for the Geometry class?


    In the current working directory.
    Expand
  3. Where is the source code for the JMUConsole class?


    I don't know, I don't have it. I only have the byte code (i.e., the .class file).
    Expand
  4. Do you need the source code or the byte code to execute a program?


    The byte code. The Java interpreter executes the byte code contained in the .class files. That's why we can use the JMUConsole class even though we don't have the source code.
    Expand
  5. Where can you find information about the methods in the JMUConsole class?


    From the documentation (which is available from the course "Help" page).
    Expand
3. Setting-Up jGRASP for Debugging: Debuggers require special versions of .class files that are created by "compiling in debug mode". This part of the lab will help you learn how to do this.
  1. To make jGRASP compile in debug mode, click on Build and "check" Debug Mode (if it is not already "checked").
  2. Click on the "button" for Geometry.java to make sure that it has the focus.
  3. Click on Build and pull down to Compile.
  4. Click on the "button" for AreaCalculator.java to make sure that it has the focus.
  5. Click on Build and pull down to Compile.
4. Setting a Breakpoint: One of the nice things about running an application in a debugger is that you can stop the execution at one or more pre-defined locations (called breakpoints). This part of the lab will help you learn how to work with breakpoints.
  1. Click on the "button" for AreaCalculator.java to make sure that it has the focus.
  2. Click in line 25 of AreaCalculator.java (the if statement) to move the cursor to that line. (Note: Don't worry if you don't yet know what in if statement does, it doesn't matter at this point and we'll discuss it soon.)
  3. Right-click in line 25 of AreaCalculator.java and pull down to Toggle Breakpoint.Alert (Note: You can also set a breakpoint by clicking on the left-most edge of a line.)
  4. What happened?


    A red circle appeared on the left side of line 25.
    Expand
  5. Click on Build and pull down to Debug and enter 4 when prompted.
  6. What happened?


    The program prompted me to enter the shape. Then the execution stopped at the breakpoint (i.e., line 25), a blue arrow appeared on the left side of line 25, and the line was highlighted.
    Expand
5. Checking State Information: Another nice thing about running an application in a debugger is that, once you stop the execution at a breakpoint, you can check state information (e.g., the value of attributes and variables). For example:
  1. Click on the Variables tab on the left side of the jGRASP window.
  2. Click on the "expand icon" next to Locals to expand it (if it isn't already expanded).
  3. What is the current value of shape?


    4
    Expand
  4. Enter an expression involving shape in the "Eval" tab (e.g., shape/2).
  5. Note that the value of this expression will change whenever shape changes. How might this kind of capability help when debugging?


    It's particularly useful when you want to determine what would happen if you were to change a line of code (e.g., use a different calculation than the one you are currently using).
    Expand
  6. Click on the "Variables" tab.
6. Stepping Over Lines: When running an application in a debugger, once you stop the execution at a breakpoint, you can continue the execution one "step" at a time. This part of the lab will help you learn how to do this.
  1. Click on the step-over.gif button.
  2. What happened?


    The blue arrow and highlight moved down to line 33.
    Expand
  3. Why did it move to this line?


    Again, don't worry if you don't yet understand if statements. What happens is that shape has the value 4, so shape == 0 evaluates to false which means that control enters the else block (not the if block).
    Expand
  4. Click on the resume.gif button to run to the end of the application and respond to both prompts with the value 10.
7. Stepping Into Lines: So far, all of the "stepping" you have done has been in one method in one class. This is called "stepping over". You can also "step into" a line of code to see what happens there. This part of the lab will help you learn how to do this.
  1. Click on Build and pull down to Debug. Enter 0 when prompted.
  2. Click on the step-over.gif button twice. You should be prompted for the diameter. Enter 10 and click on step-over.gif.
  3. What happened?


    The blue arrow and highlight moved down to the next executable statement which is the call to circleArea() method in the Geometry class.
    Expand
  4. Now, instead of clicking on the step-over.gif button, click on the step-into.gif button instead.
  5. What happened?


    The blue arrow and highlight moved to the first statement in the circleArea() method in the Geometry class.
    Expand
  6. Look at the "Call Stack". It tells you what class and method you are in and where this method was called from.
  7. What method is currently being executed (and what class is it in)?


    The circleArea() method in the Geometry class.
    Expand
  8. What line is currently being executed?


    20
    Expand
  9. Where was this method called from?


    The main() method in the AreaCalculator class (line 29).
    Expand
  10. Click on the "Variables" tab.
  11. What are the current values of the local variables?


    diamater has the value 10.0 (which was passed to the method). None of the other variables have been assigned values, so they don't show up.
    Expand
  12. Click on the step-over.gif button (watching how the local variables change) until control returns from the circleArea() method in Geometry class to the main() method in the AreaCalculator class.
  13. What is the current value of area in the main() method in the AreaCalculator class?


    It hasn't been initialized yet since the assignment operation hasn't been completed.
    Expand
  14. Click on the step-over.gif button. What is the current value of area?


    78.539... (which is the value that was returned from the circleArea() method.
    Expand
  15. Click on the resume.gif button.
  16. Run the program again, but this time, try and step into the statement that calls JMUConsole.print().
  17. What happened?


    Apparently, nothing.
    Expand
  18. Why?


    Because the debugger doesn't have the source code for the JMUConsole class. So, it can't show the source code for the statements that are executing.
    Expand
  19. Click on the resume.gif button and complete the run.
8. Stepping to the Cursor: In this part of the lab you will learn how to step to a particular line without setting another breakpoint.
  1. Run the program in debug mode, again entering 0 for the shape.
  2. Click on line 40.
  3. Click on the run-to-cursor.gif button. This will run the application to the cursor.
  4. What happened?


    The program resumed execution but stopped at line 40.
    Expand
  5. Click on the resume.gif button to run to the end.
  6. How else could you have accomplished the same thing?


    By setting another breakpoint at line 40.
    Expand
9. Gaining Experience with the Debugger: To understand the value of a debugger, you need to use it to trace the execution of a larger program than the one used in the previons sections. This part of the lab will give you some experience with this.
  1. Does a debugger actually debug code?


    No.
    Expand
  2. What does a debugger do?


    It instruments code and makes it easier to trace through it. It helps facilitate the debugging process.
    Expand
  3. You may have been unable to remove all of the errors from the code you wrote for a programming assignment. If so, use the debugger to step through your code to try and identify the errors. (Obviously, you should execute the code on a test that it failed, not a test that it passed.)

    If all of your code you have written thus far has not contained any errors, use the debugger to step through the most complicated program you've written this semester.

Copyright 2019