CompSci 4 - Classwork 10 - Sep. 29, 2011
10 pts

Today's classwork goes along with Chapter 6, Section 2. You will be writing conditionals and thinking about logic.

The following work needs to be checked off when you have completed it.

PART 1 - The tutorial in lecture

You should complete the tutorial in the lecture notes that writes and uses the three functions world.objectThatIsShorter, world.objectThatIsShorterOf3, and the chicken function chicken.changeColor. If you followed along in lecture, you have already completed this part, but you still need to show us this world when you get checked off.

PART 2 - Zeus World

Download the Zeus world from the web page. Your computer may save it as a zip file, be sure to rename it as a .a2w file.

Run the program and look at the world.myFirstMethod code, shootAllBoltsExample code, and shootBoltto see what they do. The method shootBolt with a given philosopher and color will have Zeus send a lightning bolt at that philosopher and turn the philosoher that color. If you try to shoot at philosopher who's color is black, Zeus won't do it. If you try to shoot at something that is not a philosopher, Zeus won't do it.

In what follows, only modify or create the functions or methods specified. You should NOT modify other existing functions and methods.

  1. We will define the color rules on zapping a target. It takes three lightning bolts to zap and char a target. The first lightning bolt turns it red, the second lightning bolt turns it green and the third lightning bolt turns it black.

    Write the world method called shootBoltAtTarget which has one object parameter called target. This method attempts to shoot one lightning bolt at target and turns it the appropriate color. This method should call shootBolt which has already been written for you. shootBolt should not be modified.

  2. Test your method in the following way. First in world.myFirstMethod, remove the call to shootAllBoltsExample and instead add a call to shootAllBolts. Then add code to shootAllBolts to call only shootBoltAtTarget several times for different philosophers. Make sure you zap one philosopher at least 4 times!

  3. Write a world function called IsClosestToMe that has three object parameters: obj1, obj2 and obj3. This function returns whichever object (obj2 or obj3) that is closest to obj1.

  4. Test this function in shootAllBolts by invoking shootBoltAtTarget with Target set to the result of calling the function isClosestToMe with three philosophers. Test this function with several different calls.

  5. Write a world function called IsClosestToMe3 that has four object parameters: obj1, obj2, obj3, and obj4. This function returns whichever object (obj2, obj3 or obj4) that is closest to obj1.

  6. Test this function in shootAllBolts by invoking shootBoltAtTarget with Target set to the result of calling the function IsClosestToMe3 with four philosophers. Test this function with several different calls.

If you finish early, feel free to embelish your animations.