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. Download the following files:
    to an appropriate directory/folder (e.g., the course downloads directory/folder). In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....
  2. If you don't already have one from earlier in the semester, create a directory/folder named skills (under the src directory/folder).
  3. Outside of VSCode, copy PhoneCard.java and PhoneDriver.java into that directory/folder.
  4. Start VSCode and open PhoneCard.java and PhoneDriver.java.

1. 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 PhoneDriver class, what kind of objects are end, now, and start?


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


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


    It is under the src directory in the directory for this lab.
    Expand
  4. Where is the .class file that was generated by the compiler for the PhoneCard class?


    It is in the corresponding directory under the bin directory.
    Expand
  5. Where is the source code for the Date class?


    The organization that created the version of Java that I am using has it somewhere.
    Expand
  6. Where is the .class file for the Date class?


    It is in the Java library (in the package java.util) that is installed somewhere on my machine.
    Expand
  7. Read 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.)
  8. 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
  9. 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 that's the topic for a different lab.)
    Expand
2. 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 teach you how.
  1. Click on the tab containing PhoneDriver.java to make sure that it has the focus.
  2. Right-click on the line number of the line in PhoneDriver.java that contains the statement card.startCall("540-568-1671", start); and pull down to Toggle Breakpoint.
  3. What happened?


    A red circle appeared on the left side of the line.
    Expand
  4. Click on debug.png in the activity bar. What happened?


    The explorer view closed and the debug view opened.
    Expand
  5. Press the F5 key. What happened?


    The line with the breakpoint was was highlighted and the program stopped executing. Information that looks like the current values of the variables was added (temporarily) to the code.
    Expand
3. 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). This part of the lab will teach you how.
  1. Expand the variables view in the debug view.
  2. Look at the local view in the variable view (expanding it if needed). What is the current value of availableMillis?


    5999999
    Expand
4. 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 teach you how.
  1. Click on the step-over.png button that is below the menu.
  2. What happened?


    The highlight moved down to the next executable statement.
    Expand
  3. Click on the step-over.png button until the next if statement is highlighted.
  4. What is the current value of availableMillis?


    5399999
    Expand
  5. Will the code enter the body of this if statement?


    Yes, because 53999 > 0 will evaluate to true.
    Expand
  6. Click on the resume.png button to run to the end of the application.
5. 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 teach you how.
  1. Press F5 to run PhoneDriver and stop the execution at the breakpoint.
  2. Click on the step-into.png button. What happened?


    The highlight moved into the startCall() in the PhoneCard class.
    Expand
  3. Look at the call stack in the call stack view in the debug view. It tells you what class and method you are in and where this method was called from.
  4. What method is currently being executed (and what class is it in)?


    The startCall() method in the PhoneCard class.
    Expand
  5. Where was this method called from?


    The main() method in the PhoneDriver class.
    Expand
  6. In the variables view, click on the greater than sign next to this to expand it.
  7. What is the current value of balance?


    10.0
    Expand
  8. Click on the step-over.png button.
  9. Click on the greater than sign next to callNumbers to expand it.
  10. What is the current value of callNumbers[0]?


    "540-568-1671"
    Expand
  11. Click on the step-over.png button.
  12. Click on the greater than sign next to callStarts to expand it.
  13. What is the current value of callStarts[0]?


    Since callStarts[0] is a Date object, it is a reference type and it should contain a reference. VSCode displays an ID instead.
    Expand
  14. Why does it have that value?


    Since callStarts[0] is a reference to an object, and objects can have many attributes,they are somewhat difficult to display. You can expand it to see its attributes.
    Expand
  15. Click on the step-over.png.
  16. What happened?


    Execution returned to PhoneDriver.
    Expand
  17. Add a breakpoint at the next line in PhoneDriver.java that constructs a Date and assigns it to a variable named start.
  18. Click on the resume.png button. This will run the application to the next breakpoint.
  19. Click on the step-into.png button.
  20. The debugger may or may not have been able to step into the Date constructor. If it was unable to do so, why was it unable to do so?


    The debugger doesn't have the source code for the Date class.
    Expand
  21. Click on the resume.png button to run to the end.
  22. Click on explorer.png in the left toolbar to hide the debug view and show the explorer view.
6. Advanced Topics: This part of the lab will help you use the debugger more efficiently. (Note: You may need to click on Help+Documentation and search for answers.)
  1. How can you display all of the breakpoints?


    Look in the breakpoints view inside of the debug view.
    Expand
  2. What is a conditional breakpoint?


    A breakpoint that suspends execution either when a Boolean is true or when a hit count is reached.
    Expand
  3. How can you set a conditional breakpoint?


    Right-click next to the line of interest and pull down to Add Conditional Breakpoint....
    Expand
  4. What is a "Watch"?


    An expression that is evaluated using the current values of variables and attributes.
    Expand
  5. What is a "Logpoint"?


    An alternative to adding debug output statements to the code.
    Expand

Copyright 2024