home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lab04a
Turtles
functions calling functions
Setting up
-
Save-to-disk this file, http://www.radford.edu/itec120/2010fall-ibarland/Labs/lab04-turtles.tar.gz
-
Un-zip this file (in Windows: right-click on the icon, and choose “Extract files”).
-
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).
-
Greenfoot > Scenario > Open... the file you un-zipped earlier.
You should get a scenario which include a class Trtl
(among others).
-
Click compile (if the tan boxes are covered in diagonal lines).
Playing with Trtls
-
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.
-
A Trtl can basically do two interesting things:
-
public void move( Trtl t, double dist )
-
public void turn( Trtl t, double angle )
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.
-
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?
Adding new Trtl functions
-
Let's add a new function to Trtl,
which takes in a Trtl and has it draw a triangle.
-
If you double-click on class Trtl, you'll
get an editor and you can edit the code to add new functions.
-
What is the signature we want, for this function?
(You have to think up names for the function itself, as well as
for the parameters.)
You can test your function by (say) by right-clicking on a turtle,
and selecting your function.
Try having your turtle do this:
-
making a triangle,
-
turning 10°,
-
making a second triangle,
-
turning another 10°,
and
-
making a third triangle.
-
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:
-
How many pieces of info are we passing in? What types?
-
Update the signature to reflect these.
-
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!
-
Optional: Make a function which (given a Trtl)
makes several triangles of different sizes right on top of each other.
- 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?)
-
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.
-
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?
-
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.
-
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:
-
Greenfoot declares a variable “trtl”
(note the lower-case “t”);
-
Greenfoot initializes that variable to refer to
the Trtl you just clicked on.
If you have several Trtls you've clicked on,
more variables are created: trtl_2, etc..
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs