Lab 4

Purpose

Get some experience using the Eclipse debugger.

What to do

  1. Start Eclipse.
  2. Close any editor panes you might have left open.
  3. Create a new Java Project with the name TwentyOnePickupBuggy.
  4. Download TwentyOnePickup.java from Canvas.
  5. Open (from within Eclipse) the TwentyOnePickup.java you just downloaded. (Caution do not copy and paste from a webpage which can result in some unexpected non-printing characters in the file copy created below.)
  6. Select and copy full content.
  7. Create a new class in your project (it should be in TwentyOnePickupBuggy/src).
  8. Replace it's content with what you copied in step 4.
  9. Close the editor pane opened in step 5.

You are now ready to begin debugging this buggy version of TwentyOnePickup.java.

  • Compile and run the program once.
  • Try and remove 3 stones. Take a look at the output. You should see that there is a problem.
  • Set a breakpoint on the line (21) in main() where computerMove() is called. You can use shift-click to toggle the breakpoint.
  • Run/Debug As/Java Application. It may offer to switch to the debugger view, accept.
  • Try to again remove 3 stones.
  • This time the program should pause at the breakpoint.
  • Notice that the local variables are shown in the Variables pane. What is the value of numberOfStones (a post-lab question)?
  • Click "Step Into" three times, noticing where the cursor moves to each time.
  • You can use this to "single step" your program and see what it is doing. This can be a powerful learning tool as well as a debugging tool.
  • Now what is the value of move?
  • Click "Step Over".
  • Now what is the value of numberOfStones?
  • Now click "Step Return" (you could also click "Step Over" until the cursor stops back in main()).
  • What is the value of numberOfStones now? What happened? Do you see the problem? Hint: Primitive type parameters in Java are passed using call-by-value. Think back to the stack of plates example discussed in class.
  • After fixing the problems in main (the same mistake was made on two different lines), compile the program and run it again.
  • Run the program to completion, entering 3 as the number of stones to remove each time. Confirm that it appears to be working correctly. How many stones does the computer remove on it's last turn?
  • The program contains another bug (that I know about). Run the program again. This time try and remove 1 stone on your first move.
  • How many stones does the computer remove?
  • Click "Run/Terminate" (or the red square) to end the execution and again Debug As/Java Application (or just click the bug icon).
  • Execution should again pause at the call to computerMove().
  • Single step using "Step Into" until a value has been assigned to move and the value shows in the Variables pane.
  • The winning strategy in this game is to try and always leave the pile with a multiple of 4 stones. Can you see the problem here? Since the winning strategy doesn't work (when you took away 1 stone that left a multiple of 4 and the computer can only take at most 3), the computer needs to do something else. Anything you do to make a legal move would be fine. Legal moves are 1-3 stones.

What to turn in

Modify your fixed TwentyOnePickup.java so that printInstructions() first prints your name. Then set a breakpoint somewhere in playerMove() and run the program in debug mode until it hits the break point. At that point, take a screen shot and submit that under lab4 in Canvas. That screen shot should show the command window showing your name in the output, and the other debug perspective windows showing the program paused at your breakpoint in playerMove().