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.
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.
m
that contains errors.
m
that causes the error. If the statement is an invocation to another
method m1
, then you might need to restart the debugger and find the statement inside method m1
that causes
the error. Notice that this step might require stopping and restarting the debugger several times.
TestLab6.java
.
TestLab6
. Tests 6 and 7 will fail. Set a breakpoint at line 113
(testPassed = true;
) of TestLab6
; this is the
line where test 6 starts. Run the debugger; the program should stop execution when it reaches line 113.
F6
or click on the "Step over" button; every time that you press F6
look at the
value of variable testPassed
. The value of this variable is initialized to true
; stop when the value of the
variable changes to false
.
if
statement causes the value of variable testPassed
to be set to false
? Write the line number of
this if
statement in AnswersLab6.txt.
if
statement that caused the value of testPassed
to be set to
false
.
newHeadPostion
is invoked at the line where the debugger paused the execution of the
program, which suggests that the problem with the program is the implementation of method newHeadPosition
. The specification
of this method given in Assignment 1 states that the method must compute the position where the head of the snake should move
without actually moving the snake. Since in line 114 (theSnake = new Snake(5, 8);
)
of TestLab6
the inital position of the snake is
(5,8), then after invoking method newHeadPosition
for the first time in line 119 (if (!theSnake.newHeadPosition("up").equals(up))
),
the snake must still be in postion (5,8). theSnake
to expand it. Then click on the ">" symbol beside variable snakeBody
(if you rember, this is the array that contains the positions of the game board occupied by the body of the snake). Then click
on the ">' symbol beside entry [0] of this array. In which row and column is the snake currently
positioned? Is this the right position for the head of the snake? Write your answers in
AnswersLab6.
snakeBody
was incorrectly changed, and lines 113-118 do not
access this array, then the problem must be with method newHeadPostion
invoked in line 119.
Terminate the execution of the program. Clear the breakpoint and add a new breakpoint at line 119.
F5
or click on "Step into" to get inside method newHeadPosition
.
In the variables window, click on the ">" symbol beside this
, then select snakeBody[0]
as explained above. Note that the position of the head is still (5,8). F6
until you reach line 42 of class Snake.java
. Check that the snake's head is still in
position(5,8). Press F6
once more; since direction
is "up", the statement
head.setRow(row-1);
will be executed. Press F6
one more time. Now the
position of the snake's head is (4,8), which is incorrect. Why did the statement
head.setRow(row-1);
changed the data stored in snakeBody[0]
? Write your answer
in AnswersLab6.
newHeadPostion
so it does
return the correct answer, but it does not modify the data stored in snakeBody[0]
.
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. Download the Exception Examples 1, 6, 7, and 8 from the Sample Code section of the course's webpage.
ExceptionExample1
. Why does the program crash? Write your answer in AnswersLab6.
try-catch
statement. Print an appropriate message when the
exception is caught.
ExceptionExample6
and ExceptionExample7
. They both produce the same exception,
but notice that the try-catch
statement is in a different method. This is an example of
propagating an exception.
ExceptionExample6
and ExceptionExample7
?
Write your answer in AnswersLab6.
ExceptionExample7
does the execution of method change
continue after the
statement that generates the exception? Write your answer in AnswersLab6.
ExceptionExample8
. Notice that both main
and change
have a
try-catch
statement. In which method is the exception caught? Write your answer in AnswersLab6.