RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab09a
looping turtles

Today we will re-visit Greenfoot's Turtles, although now we can use them as non-static (e.g. instead of turn(myrtle,60) we'll write                 ).

We'll work on labs individually today, not with partners.

  1. Download and save-to-disk today's initial turtles project. Uncompress the scenario (Windows: right-click and choose the extract-files option.)
    Start Greenfoot, and open the scenario you just saved. (It should include “Turtle”, not “Trtl”.)
  2. Just to get us warmed up: Open up class Turtle and add a method drawSquare which takes in a size, and has this turtle draw a square of the indicated size. (No need to use a loop yet.)
  3. Write a method spinSquare, which has this turtle repeatedly: draw a square and turn (say) 15°. It keeps repeating these two actions as long as its direction is less than (360-15)°.
    Hint: If you look near the top of class Turtle, you can see that there is a field to keep track of each Turtle's direction.
    You can recall the loops we wrote in yesterday's lecture.
  4. Add an index-variable to your loop, which counts how many times the turtle has gone through the loop so far. (You can name your variable timesTurned, or you can also use i which is an idiomatic index variable.)
    You can recall the loops we wrote in yesterday's lecture.
  5. Let's make a loop which uses the index-variable: Write spinGrowingSquare which:
    draws a square-of-size-0, rotates 15°
    draws a square-of-size-1, rotates 15°
    draws a square-of-size-2, rotates 15°
    etc. as long as the turtle's angle-turned is less than (360-15)°
    You can recall the loops we wrote in yesterday's lecture.
  6. There's an arbitrary number we've been repeating in many places in the program. Change it into a named constant!
    (Recall how we made MB_PER_SEC and DEG_CELC_PER_FAHR named constants: Make them                  fields.)
  7. Stopping when the internal angle was (360-15)° or more was kind of a yucky end-condition; initially did that because angles “wrap around” back to 0°, and we don't have a good way of telling 1° from 361°, so we needed to stop before we reached 360°.

    stopping when the turtle is facing (nearly) east is also incorrect for another reason: it only draws a full circle if the turtle started by facing east. But clearly, it makes sense to make a full revolution no matter what direction the turtle starts in.

    However, we can tell from the outset how many rotations we want to make: 360°/15°. And since we're now using an index variable, we can change our loop to say “do the following 24 times”. Modify spinGrowingSquare accordingly: change the loop's ending condition so that it stops once it's made the appropriate number of turns (rather than checking its current angle).


    You can recall the loops we wrote in yesterday's lecture.
  8. Write a Turtle method which makes a regular polygon of n sides.
    (Hint: When drawing a triangle, how many degrees did you turn each time? When drawing a square, how many degrees did you turn each time? Can you guess a simple pattern (presumably involving the number 360)?)
  9. Make a variant spinPolygon, which is like spinSquare but uses n-gons rather than squares.
  10. Similarly, Make a variant spinGrowingPolygon, which is like spinGrowingSquare but uses n-gons rather than squares.
  11. Try creating some other shapes/patterns. Perhaps some of these shapes, or anything else you want to try.

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Nov.06 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme