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.
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.
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 shootBolt
to 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.
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.
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!
IsClosestToMe
that has
three object parameters: obj1, obj2 and obj3. This function returns
whichever
object (obj2 or obj3) that is closest to obj1.
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.
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.
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.