COMP 105
Introduction to Computer Science
Kalamazoo College Computer Science Program Fall 2008

Mini-Lab 3: World Level Methods


Introduction

The purpose of this mini-lab is to get some practice creating world-level methods, and working with parameters.

Exercises

  1. Revenge of the Shark

    In this scenario, the shark has gathered three or four of his meanest friends, and they are all chasing the fish. Unfortunately for the sharks, the fish is a tough character. Whenever the fish is caught by a shark, the shark gets flung several meters away and must resume the chase from the new location. (You don't need to use sharks and fish here; feel free to animate any group of animals chasing any other animal.)

    We could animate this new scene by copying and modifying our chase code several times, but it makes more sense to write a single parameterized "chase" method. This method will be responsible for animating a single shark as it chases the fish. The method should take two parameters: one to contain the object being chased (the chasee), and one to contain the object doing the chasing (the chaser). We can cause many sharks to chase the fish by including many calls to this new method, each of which assigns a different shark to the "chaser" parameter.

    The "chase" method described above only moves the sharks. We will need an additional method to move the fish. This "swimAround" method should take a single parameter containing the object that is running away. The "swimAround" animation can use the same random motion technique you implemented in the previous mini-lab.

    Once your animation is complete "world.my first method" should only contain calls to your new methods; it should not include any commands that directly move objects in the world.

  2. Follow the Leader

    One advantage of using methods is that it often possible to solve new problems by reusing existing code. In this case, we want to animate a slight variation of the previous scene. Instead of all of the sharks chasing the fish, in this scene the first shark is chasing the fish, the second shark is chasing first shark, the third shark is chasing the second shark, and so on. You should be able to animate this scene without changing either of the methods you wrote above. The only code you need to change should be in "world.my first method".