CS1027b Computer Science Fundamentals II

Lab 6

Learning Outcomes

Upon completion of this lab, you should be able to do the following:

General Lab Instructions

IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote and AnswersLab6.txt through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.

Exercise 1: Debugging a Large Program

Create a new project for this lab. Add all the java files from filesLab6.zip to your project. Place all the image files and file board1.txt in the root directory of your project (this is the same thing you did for assignment 1). Run the program PlayGame give as input board1.txt. Move the snake around using the arrow keys in your keyboard; you will see that as you move the snake many snake heads are created because the program has errors in it. You have to debug the program and fix the errors.

Many of you will feel overwhelmed by the task of debugging a program like this that consists of hundreds of lines of code, many of which that were not written by you. How on earth can we figure out why this program does not work as expected so we can fix it?

When debugging a program we do not look at the entire code and try to figure out what is wrong. Instead we look at each class and method of the program and try to find out which contain errors. To do this we use a test harness in each class or a separate test program that invokes each method one at a time and checks whether the method produces the expected output. We should design the testing program so that it tries to test methods independently from each other; this will simplify the task of finding the erroneous code.

When designing a test program for a method we should think what input will be passed to the method and what output the method must produce. Then, after running the testing program we check whether the output is the one that we expect; if it is not, then we examine the code of that method closely looking for the bug.

The debugger is a very useful tool in the process of finding bugs and correcting them. You have used the debugger in past labs, but this time you will use it to find bugs in a much larger program than the ones you considered in other labs. Keep in mind that a debugger is a tool that you will use throughout your career, as it greatly simplifies the task of finding and fixing errors in programs.

To debug a program you will follow these steps: We wrote a testing program for you called TestLab6.java. Run program TestLab6 again. Test 6 will still fail. Line 61 of class Snake.java contains this statement: if (snakeBody[i] == pos); is this the correct way to compare two non-primitive variables (snakeBody[i] and pos are of type Position)? How should they be compared? Write your answer in AnswersLab6.

Fix statement if (snakeBody[i] == pos); and then run the program PlayGame with input board1.txt. You will see that the snake does not move when you press the arrow keys and the program will repeatedly print the word "null" in the Console window. This message "null" is printed because the program is trying to access a null object. Use what you learned above to fix this error. Which method tried to access a null object, causing the error in the program? Write your answer in AnswersLab6. Run the program PlayGame and make sure that the snake moves around with the arrow keys, as expected.
Please try to figure out what the error is and how to fix it on your own. If you are really stuck, the read these hints.

Exercise 2: Experimenting with Exceptions

Download the Exception Examples 1, 6, 7, and 8 from the Sample Code section of the course's webpage.