JMU
Lab: Skills - Using a Debugger


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

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, the easiest way to do this is by right-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 DrJava
  2. "Clean up" DrJava File and pulling down to Close All.
  3. Open PhoneCard.java by clicking on File, pulling down to Open, and selecting the appropriate file and directory.
  4. Open Driver.java in the same fashion.
2. Review: This part of the lab will review a few topics related to object-oriented programming in Java.
  1. In the main() method in the Driver class, what kinds of objects are end, now, and start?


    They are Date objects.
    Expand
  2. In the main() method in the Driver class, what kinds of object is card?


    A PhoneCard object.
    Expand
  3. Where is the code for the PhoneCard class?


    In the current working directory.
    Expand
  4. Where is the code for the Date class?


    It is in the Java library (in the package java.util).
    Expand
  5. Search on-line for the documentation for the Date java.util.Date class. Make sure you find the documentation for the Date class that is in java.util. (There are several Date classes in the Java library.)
  6. When you construct a Date object using the default constructor (i.e., the constructor that has no parameters), what properties will it have?


    It will contain the current date.
    Expand
  7. Where can you find information about the methods in the PhoneCard class?


    From the source code for the PhoneCard class. (It would be nice if we had the documentation in an HTML file, but the professor was too lazy to generate it.)
    Expand
  8. Compile both files.
3. Setting-Up DrJava for Debugging: The debugger in DrJava needs to be configured before it can be used.
  1. Drjava needs to know the location of a file named tools.jar that was installed when the Java Development Kit was installed. Click on Edit+Preferences, select Resource Locations, and enter the appropriate path (including the file name) in the "Tools.jar Location" field.
  2. Exit and re-run DrJava.
4. Using the Debugger: To use the debugger DrJava must be in "Debug Mode".
  1. Click on Debugger and pull down to Debug Mode.
  2. What happened?


    New panes appeared between the "Code Pane" and the "Interactions Pane".
    Expand
  3. Move the divider between the "Watches" tab and the "Stack" tab so that you can read the words "Name", "Value" and "Type".
5. 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). For example:
  1. Click on Driver.java to make sure that file has the focus.
  2. Click in line 33 of Driver.java (the first if statement) to move the cursor to that line.
  3. Right-click in line 33 of Driver.java and pull down to Toggle Breakpoint.Alert
  4. What happened?


    The line is highlighted in red.
    Expand
  5. Click on Run.
  6. What happened?


    Execution began, stopped at line 33, and line 33 is now highlighted in cyan.
    Expand
6. 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. In the "Watches" tab, click in the field under "Name", type availableMillis, and press Enter.
  2. What is the current value of availableMillis?


    5999999
    Expand
7. 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. For example:
  1. Click on the Step Over button.
  2. What happened?


    The cyan highlight moved to line 35 and line 33 is again highlighted in red.
    Expand
  3. What do red and cyan highlighting seem to indicate?


    Red highlighting seems to indicate the location of a breakpoint and cyan highlighting seems to indicate the statement that is about to be executed.
    Expand
  4. Click on the Step Over button until the next if statement is highlighted.
  5. What is the current value of availableMillis?


    5399999
    Expand
  6. Click on the Resume button to run to the end of the application.
8. 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. For example:
  1. Click on Run. This will run Driver and stop the execution at the breakpoint (i.e., line 33).
  2. Click on the Step Over button.
  3. What happened?


    Line 33 was executed and line 35 is highlighted in cyan, indicating that it is next to be executed. highlighted.
    Expand
  4. Click on the Step Into button again.
  5. What happened?


    The cyan highlight moved into the startCall() method in the PhoneCard 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 startCall() method in the PhoneCard class.
    Expand
  8. What line is currently being executed?


    90
    Expand
  9. Where was this method called from?


    The main() method in the Driver class (line 35).
    Expand
  10. In the "Watches" tab, what is the current value of availableMillis?


    "not found"
    Expand
  11. Why?


    It is out of scope (i.e., it is a local variable in the main() method in the Driver class, and the code being executed is the startCall() method in the PhoneDriver class.
    Expand
  12. What is the current value of the attribute named balance?


    10.0
    Expand
  13. Click on the Step Over button.
  14. What is the current value of callNumbers[0]?


    "540-568-1671"
    Expand
  15. What is the type of callNumbers[0]?


    String
    Expand
  16. What is the type of callNumbers?


    String[]
    Expand
  17. How many elements are there in callNumbers and what values do the contain?


    2. Element 0 is "540-568-1671" and element 1 is null. String[]
    Expand
  18. What is the current value of callStarts[0]?


    null
    Expand
  19. Click on the Step Into button.
  20. What is the type of callStarts[0]?


    java.util.Date
    Expand
  21. What value does DrJava display for callStarts[0]?


    It appears to be the current date and time.
    Expand
  22. Why is this somewhat surprising?


    callStarts[0] is an object that has many attributes. DrJava is displaying a convenient String representation of those attributes.
    Expand
  23. Click on the Step Over button.
  24. What happened?


    Execution returned to Driver.
    Expand
  25. Click on line 46 in Driver.java (i.e., the line that constructs a Date) and set a breakpoint.
  26. Click on the Resume to run the application to this breakpoint.
  27. Click on the Step Into button.
  28. Why didn't the debugger step into the Date constructor?


    The debugger doesn't have the source code for the Date class.
    Expand
  29. Click on the Resume button to run to the end.
9. Other Features of the Debugger: Different debuggers have different capabilities. While DrJava's debugger is not the most powerful, there are some other things it does.
  1. What information is on the "Breakpoints" tab?


    All current breakpoints in all files.
    Expand
  2. How can you remove all breakpoints?


    By clicking on the Remove All button on the "Breakpoints" tab or by clicking on Debugger+Clear All Breakpoints.
    Expand
  3. What does the Step Out button do?


    It executes the rest of the current method and returns control to the caller.
    Expand
  4. What does the Automatic Trace button do?


    It steps into each line, one per second.
    Expand
  5. Why might this be useful?


    If you've set several watches, you can trace the execution of the program without have to click on Step Into repeatedly.
    Expand
  6. How else can you toggle breakpoints?


    By pressing Ctrl+B or by clicking on Debugger+Toggle Breakpoint on Current Line.
    Expand

Copyright 2017