Students who complete this lab will demonstrate that they can:
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
We suggest that you center the figure’s head at coordinates
(
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 that method.
Save a copy of your previous program as
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
Save this version of your program so that you can turn it in later. |
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
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:
This method receives a coordinate, either x or y, and returns a
new, legal value based on its lower and upper bound arguments. For
example, if
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.computeCoordinate(currentX, currentDiameter/2, WIDTH - currentDiameter/2) With this utility method in place, you can implement two stick figures this as follows:
Save this program so that you can turn it in later. |
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 criteria: