Students who complete this lab will demonstrate that they can:
- Design and implement procedural abstractions using methods
- Implement parameters and return values
Methods allow programs to add new procedural abstractions to the
programming language. In this lab exercise, for example, you write a
method to draw a composite stick figure.
Create a program called
that renders the (non-animated) stick figure shown to the right.
There are a number of approaches to doing this; for the sake of
this exercise, use the following algorithm, which includes a user-defined
- Set the output panel size;
- Turn on smoothing;
- Turn off animation looping.
- Call the method that renders the stick figure.
- Render figure:
This routine returns nothing (i.e.,
- Create a method called
renderFigure that receives no arguments.
- Set local float variables
to the center of the output window;
- Set local variable
diameter (the diameter of the
figure’s head) to 24;
- Draw the figure’s trunk;
- Draw the figure’s arms;
- Draw the figure’s two legs;
- Draw the figure’s head.
void ) to its
We suggest that you center the figure’s head at coordinates
) and draw all the rest of figure relative to these coordinates.
For example, the trunk is a single vertical line from (
y + diameter * 0.5
) to (
y + diameter * 1.5
). This scaling will help you in the next exercise.
Save this program so you can turn it in later.
One nice feature of the code in the previous exercise is that it
groups all the figure-drawing code in one appropriately named method.
This improves the readability of the program and encourages reuse of
Save a copy of your previous program as
. Modify this new copy so that you can draw two stick figures at
different locations (as shown on the right). You can do this as
renderFigure() method so that it
y coordinates from
its calling program rather than declaring them locally.
draw() so that it invokes
renderFigure() twice, passing different x-y
coordinates as arguments to each call (e.g.
Save this program; you’ll use it as the basis for both
exercise 3 and exercise 4.
Methods are valuable for building useful procedural
abstractions. We don't need to restrict ourselves to parameterizing
just the x-y coordinates of the stick figure.
Save a copy of your parameterized program from exercise 2 as
. Modify your program to animate a growing (or shrinking) stick
figure. The example on the right shows two stationary figures,
each growing at different rates. Implement this as follows:
- Add global variables to represent the diameters of your
two stick figures;
- Add a third parameter
renderFigure() that specifies the diameter of the
stick figure and modify the method to draw a scalable stick
figure based on this new parameter.
draw() to pass the new diameter
values as arguments
renderFigure() and to increment the diameter
values on each frame.
Save this version of your program so that you can turn it in
Methods can also return values. This is useful when your program
must compute a certain value frequently.
Save a (second) copy of your parameterized program from exercise 2
(not the scalable program from exercise 3) as
. In this exercise, you will modify this program so that it
animates the two stick figures using Brownian motion (as shown on
Start by implementing a utility method
that helps you constrain the location of your figures, keeping
them on the visible canvas at all times. You had to perform a
similar task for the Brownian motion exercise in lab 4a. Use the following algorithm:
from the calling program: a coordinate value
a minimum legal value
lowerBound and a maximum
- Compute and return
a new value that is randomly modified from
but constrained by
As in lab 4a, use the constrain
method for this computation.
This method receives a coordinate, either x or y, and returns a
new, legal value based on its lower and upper bound arguments. For
refer to the value of a stick figure’s current x coordinate
and diameter respectively, then invoking:
will return a new value that ensures that
the stick figure is not too far to the left or to the right. Test
this method by printing out its value for a few key test cases,
e.g.: values that are in the range, too small for the range, too
large for the range and on the boundaries.
WIDTH - currentDiameter/2)
With this utility method in place, you can implement two
stick figures this as follows:
- Add global variables
y2 to store the current coordinates of each of
your two stick figures;
draw() so that it changes the values
of the new global variables by some small amount (e.g., 5
pixels), using your new
computeCoordinate() to help
you change and constrain the coordinate values;
Save this program so that you can turn it in later.
Exercise 4b.5: Extra Credit
Modify your program so that the color of the stick figure is based
on how far it is from some location (or figure). For example, the
image to the right shows two animated stick figures that get
redder the closer they get to the sun. To do this, you’ll
need to use a distance method such as Processing’s
Submit all the code and supporting files for the exercises in
this lab. We will grade this exercise according to the following
- 80% - Programs - Complete all four of the programming
exercises (20% each).
- 5% - Header Documentation - Document the code’s basic
purpose, authors and assignment number.
- 5% - Code Documentation - Separate the logical blocks of
your program with useful comments and white space.
- 10% - Method Documentation - Document your new methods
- Extra Credit: The extra credit is worth up