Name: ____________________________________________
In this homework you're going to modify your random walk lab that you just completed. We're going to use our knowledge of if-statements to keep a turtle away from the edge of the world. When a turtle gets too close to the edge we're going to move it back to the middle of the world. Turn in this sheet with answers in the blanks provided along with a copy of your program stapled to this sheet.
getWidth()
and getHeight()
on a World
object. Finish
the variable declarations below to calculaate the center coordinate.
int middleX = int middleY =
moveTo(x,y)
on a turtle object. Write a Java statement below that moves
a turtle t
to the center of the world.
______________________________________
Turtle
methods
getXPos()
and getYPos()
. Write an if-statement that determines
if the turtle is within ten pixels of the top edge. If it is have the turtle
move to the center.
Add this if-statement to your program and verify that it works. If it does you should see
a straight line where the turtle shot back to the middle. You might
need to run it a few times.
getWidth()
method on the world object.
penUp()
and penDown()
methods so we don't see the straight lines any longer
where the turtle returned to the middle.