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. Setup your development environment.
  2. 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....
  3. If you don't already have one from earlier in the semester, create a project named eclipseskills (that uses the course style guide and formatter and does not use a module).
  4. Click and drag PhoneCard.java and PhoneDriver.java into the src directory of the project (using the "Copy" option). Note: If Eclipse does not allow you to do this, it may be because you are not using the "Java Perspective".
  5. Open PhoneCard.java and PhoneDriver.java.
  6. Go into each .java file, select all of the source code (prefereably using a keyboard shortcut), and re-format all of the source code.

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?


    Since I told Eclipse to create separate directories when I created the project, it is in the src directory under the directory for this project. (I can see this directory in the Eclipse project explorer.)
    Expand
  4. Where is the .class file that was generated by the compiler for the PhoneCard class?


    Since I told Eclipse to create separate directories when I created the project, it is in the bin directory under the directory for this project. (I can't see this directory in the Eclipse project explorer, but I can in the operating system's file explorer.)
    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 the professor was too lazy to generate it.)
    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 blue circle appeared on the left side of the line.
    Expand
  4. Click on debug.png (or Run+Debug). This will run PhoneDriver and stop the execution at the breakpoint. Note: If prompted, allow Eclipse to enter the "Debug Perspective".
  5. What happened?


    Eclipse changed to he "debug perspective". A blue arrow appeared on the left side of the line and the line was highlighted.
    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. Click on the "Variables" tab on the left side of the debug window.
  2. 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.
  2. What happened?


    The blue arrow and 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? (Hint: Look in the "Variables" tab. You may beed to scroll.)


    5399999
    Expand
  5. 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. Click on debug.png. As you know, this will run PhoneDriver and stop the execution at the breakpoint.
  2. Click on the step-into.png button.
  3. What happened?


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


    The startCall() method in the PhoneCard class.
    Expand
  6. Notice that it also tells you what line is currently being executed.
  7. Where was this method called from?


    The main() method in the PhoneDriver class.
    Expand
  8. Notice that it also tells you what line the currently executing method was called from.
  9. In the "Variable" area, click on the "triangle icon" next to this to expand it.
  10. What is the current value of balance?


    10.0
    Expand
  11. Click on the step-over.png button.
  12. Click on the "triangle icon" next to callNumbers to expand it.
  13. What is the current value of callNumbers[0]?


    "540-568-1671"
    Expand
  14. Click on the step-over.png button.
  15. Click on the "triangle icon" next to callStarts to expand it.
  16. 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. Eclipse displays an ID instead.
    Expand
  17. 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
  18. Click on the step-over.png.
  19. What happened?


    Execution returned to PhoneDriver.
    Expand
  20. Add a breakpoint at the line in PhoneDriver.java that constructs a Date and assigns it to a variable named start.
  21. Click on the resume.png button. This will run the application to the next breakpoint.
  22. Click on the step-into.png button.
  23. 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
  24. Click on the resume.png button to run to the end.
  25. Click on Window+Perspective+Close Perspective to close the "Debug Perspective".
6. Advanced Topics: This part of the lab will help you use the debugger more efficiently. The answers to all of the following questions are available on the course "Help" page on "Eclipse Tips" (but can also be found elsewhere).
  1. How can you display all of the breakpoints?


    Using the Breakpoints View.
    Expand
  2. What is a conditional breakpoint?


    A breakpoint that suspends execution either when a Boolean is true or when a Boolean changes value.
    Expand
  3. How can you see variable references while debugging?


    By managing the Variables View.
    Expand

Copyright 2023