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

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

lab04a
Turtles
functions calling functions

    Setting up

  1. Save-to-disk this file, http://www.radford.edu/itec120/2010fall-ibarland/Labs/lab04-turtles.tar.gz
  2. Un-zip this file (in Windows: right-click on the icon, and choose “Extract files”).
  3. Start Greenfoot (not BlueJ!) today.
    (Just go to the start menu and type in greenfoot.) When it asks “Allow network access?”, just say “No” (since we don't have administrator privileges).
  4. Greenfoot > Scenario > Open... the file you un-zipped earlier. You should get a scenario which include a class Trtl (among others).
  5. Click compile (if the tan boxes are covered in diagonal lines).
  6. Playing with Trtls

  7. Right-click on the tan Trtl class, and call new_Trtl(), which creates a Trtl and puts it into the world. (It also returns the Trtl object, which is why the Method Result window pops up; you can dismiss it.)

    Call new_Trtl() a second time, so that it's clear we can make as many different Trtls as we like.

  8. A Trtl can basically do two interesting things: Let's try moving one of our two Trtls:
    Right-click on the tan class Trtl, and select void move( Trtl t, double dist ). Now, fill in the arguments in the box that pops up: for the Trtl, you can just click on the Trtl you want to move1 After you get the Trtl to move, have it turn.
  9. Let's draw a triangle together, in class (just calling functions via right-clicking), arranging so that leave the turtle pointing in its original direction.
    bonus thought:: How many degrees did we end up turning, altogether? 360° -- the turtle rotated exactly once while drawing the figure! If you wanted to make a pentagram or a 19-sided figure instead, can you guess how much each of the individual (5 or 19) turns should be?
  10. Adding new Trtl functions

  11. Let's add a new function to Trtl, which takes in a Trtl and has it draw a triangle.

    You can test your function by (say) by right-clicking on a turtle, and selecting your function. Try having your turtle do this:

  12. Upgrade your triangle function so that it takes in the triangle's size (as well as which turtle to move).
    We still use bits of the design recipe:
  13. Write a new function which has the turtle draw a (crude) radiation-warning symbol. Hint: This is an example of having your function call other functions you've previously written!
  14. Optional: Make a function which (given a Trtl) makes several triangles of different sizes right on top of each other.
  15. Write your own function which takes in a Trtl and has it draw the following 'zag' shape, where the distance from the start to the finish is 90. (How big is the gap, if the zaggy part outlines an equilateral triangle?)
  16. Now, upgrade your zag function so that it takes in a Trtl and a distance, and has the Trtl draw that zag shape, where the total distance covered from the start to the finish is the provided distance.
  17.  

     

  18. In the code-editor, use the pull-down in the upper-right to switch to 'documentation' view. Look at some of the other functions that Trtls can perform. How can you get a Trtl to move location, without drawing a line?
  19. Here's a bigger task: Make a function smiley that takes in Trtl and a size, and makes a smiley face like: (Note that the smile doesn't have to exactly line up with the eyes; it can be a bit off, to add to the creepy factor.)

    How, exactly, will you draw the bowtie-ish thing, using two triangles? You must obey the first law of programming: No Repeated Code. If you want to (say) draw some triangles, just call the function you already wrote previously -- Easy!

    hint:Have the turtle start in the middle of the bowtie-thing.

    Note: your next problem is easier, if you decide that smiley leaves the turtle facing the same direction it started.

  20. Finally: Use your previous function to make a function threeSmileys which draws something like:
to teacher:

Common bug: Sometimes in threeSmileys, just one of them is mysteriously missing its eyes!?! This is because penUp and penDown have been called from several different functions and not restored.

(It's a casualty of using void methods and side-effects: our turtle has state that we're not specifying; perhaps every method should return a Trtl structure, and unit-tests could test that all fields of the returned Trtl were the expected result.)

We will continue working on this in Thursday's lab, and you will check it off (with your partner).


1 When you double click on a new turtle in this context, several things happen:

If you have several Trtls you've clicked on, more variables are created: trtl_2, etc..      

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


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