Course Links

Exercises

Resources

External

This lab is designed to help you practice using stacks. It has two parts. The first part is to complete the tutorial on stacks. The second part is described below.

Debugging and the Call Stack

Recall that the java debugger jdb lets you look at a program and its variables as it is running. You can freeze a program at any point in the middle of its execution, step it forward line by line, and watch the state of the program change. The complete list of commands available can be seen by typing help at the jdb prompt. You can also see them on this handout.

Take a look at SampleJDB.java. We will use this as a testbed. It contains a few features that can be illuminated by pausing the program in the debugger and examining the call stack.

Following the instructions below should highlight the points of interest just mentioned. Start a script session, and go through the following sets of exercises designed to familiarize you with some of the capabilities of jdb. (Try not to use help during the transcript session, or the results will appear in the file. Instead, you can use the link above.) You can always ask me or the TA for help if you have trouble figuring out how to accomplish any of these steps.

  1. Compile SampleJDB.java using the -g flag.
  2. Start the program using jdb.
  3. Set a breakpoint (stop) when the program enters the lottery method. (The idea here is that this is a method that will be called very rarely -- but you can tell the program to stop when it happens.)
  4. Set a breakpoint at line 13. (This should be the line that reads "if (n <= 1) {")
  5. Put a watch on SampleJDB.number, so that you will be notified whenever its value changes (but not for ordinary read access).
  6. Run the program. (Where does it stop?)
  7. print n
  8. Continue the program once.
  9. print n
  10. Clear the breakpoint at SampleJDB:13 and move it to line 14. Continue the program.
  11. Show the call stack, with where. You should see several levels of calls to factorial.
  12. print n
  13. Go up one stack frame, and print n.
  14. Go up one stack frame, and print n again.
  15. Continue the program. (Where does it stop next?)
  16. Look at the value of SampleJDB.number.
  17. Advance the program by one step.
  18. Look at the value of SampleJDB.number again. It should now be different.
  19. Advance the program by two more step.
  20. Continue the program. (Where does it stop next?)
  21. Move one level up the call stack, and print out all the locals. (What is i? Why does it have this value?)
  22. Allow the program to continue and exit.

To Submit