CS 140 - Homework 10 - Due Wed March 26

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.

  1. What are the coordinates of the middle of the world? Hint: You can call the methods getWidth() and getHeight() on a World object. Finish the variable declarations below to calculaate the center coordinate.
         int middleX = 
    					
         int middleY = 
      
  2. You can move a turtle to an arbitrary coordinate by calling the method moveTo(x,y) on a turtle object. Write a Java statement below that moves a turtle t to the center of the world.
  3.     ______________________________________
    		
  4. In order to determine if a turtle is too close to an edge we need to know its current location. We can get the current location of a turtle by calling the 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.
  5. Modify your if-statement above so that if a turtle is too close to the top edge or the left edge it goes back to the middle.
  6. Modify your if-statement above to also check if the turtle is within ten pixels of the right edge. This is a little trickier. Hint: use the getWidth() method on the world object.
  7. Add in a check for the bottom edge.
  8. Use the penUp() and penDown() methods so we don't see the straight lines any longer where the turtle returned to the middle.