RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

Turtles
functions calling functions

    Setting up

  1. Save-to-disk this file, http://www.radford.edu/itec120/2019fall-ibarland/Labs/turtles/turtles-src.tar.gz
  2. Un-zip this file (in Windows: right-click on the icon, and choose Open with… » 7zip » extract here. This creates a .tar file; repeat the same steps to extract again, to get a folder turtles-src.
  3. Start Greenfoot (not BlueJ!) today.
    (Just go to the start menu and type in greenfoot.) If 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 ZagTrtl (among others).
  5. Playing with ZagTrtls

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

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

  7. A ZagTrtl can basically do two interesting things for now: Let's try moving one of our two ZagTrtls:
    Right-click on the tan class ZagTrtl, and select void move( ZagTrtl thiss, double dist ). Now, fill in the arguments in the box that pops up: for the ZagTrtl, you can just click on the ZagTrtl you want to move1 After you get the ZagTrtl to move, have it turn.
  8. Let's draw a triangle together, in class (just calling functions via right-clicking), arranging so that wi 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 5-sided pentagram or a 19-sided figure instead, can you guess how much each of the individual (5 or 19) turns should be?
  9. Adding new ZagTrtl functions

  10. Let's add a new function to ZagTrtl, which takes in a ZagTrtl and a size (in that order), and has it draw an equilateral triangle of that size, leaving it pointed in its original direction.

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

  11. Write a new (static) function which takes in a turtle and has it draw a (crude) radiation-warning symbol. Hint: This is an example of having your function call other functions you've previously written!
  12. Optional: Make a function which (given a ZagTrtl) makes several triangles of different sizes right on top of each other.
  13. Write your own (static) function which takes in a ZagTrtl 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?)
  14. Now, upgrade your zag function so that it takes in a ZagTrtl and a distance, and has the ZagTrtl draw that zag shape, where the total distance covered from the start to the finish is the provided distance.
  15.  

     

  16. 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 ZagTrtls can perform. How can you get a ZagTrtl to move location, without drawing a line?
  17. Here's a bigger task: Make a function smiley that takes in ZagTrtl 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.

  18. 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 ZagTrtl structure, and unit-tests could test that all fields of the returned ZagTrtl 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 ZagTrtls you've clicked on, more variables are created: zagTrtl2, etc..      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.