CompSci 4 Test 2 Fall 2010 Solutions 1. A) list events When the world starts When penguin distance to chair <= 1.5 When penguin distance to beachChair <=2.5 When the mouse clicks on an object B) List event handlers world.myFirstMethod penguin.begin penguin.sit penguin say Goodbye penguin.something move the object clicked on C) The penguin circles around the beachChair, says "That was fun" and then moves 1 meter towards the chair. At this point he says "I'll sit", he turns to face the chair and jumps up on it, then faces front. He then continuously flaps his wings. D) The penguin says Goodbye, stops flapping his wings and then everything stated in C now happens again. 2. cat penguin tortoise joey turtle lemur 3. A) boolean B) someColor is a parameter C) total is a local variable D) value is a parameter E) mystery returns true if more than "value" animals have the color "someColor". Otherwise it returns false. F) yes 4. A) 3 times B) 12 times C) 18 times 5. A) Lines 5 and 7 have recursion B) The way out is if input < factor C) 3 5 2 1 6. A) score.increase if item.color == red score.current set value to score.current + 3 else score.current set value to score.current + 1 score set text to score.current as a string B) when (alienOnWheels distance to baseball < 0.25), do score.increase item=baseball when (alienOnWheels distance to beachBall < 0.25), do score.increase item=beachBall 7. 123 world.numberInRange 123 min 123 max 123 total = 0 Loop 123 index from 0 up to but not including size of world.values incr by 1 if both item index from world.values >= min and item index from world.values <= max total set value to total + 1 else do nothing Return total 8. A. world.randomlyColor color color For all world.chickens, one obj item_from_Chickens at a time if choose true 50% of the time item_from_chickens set color to color else item_from_chickens set color to white 8. A. ALT world.randomlyColor color color 123 number = random number min = 1 max = 3 integer only For all world.chickens, one obj item_from_Chickens at a time if number == 1 item_from_chickens set color to color else item_from_chickens set color to white B. 123 world.ratioThisColor 123 count = 0 For all world.chickens one obj item_from_chickens at a time if item_from_chickens.color == color count set value to count + 1 else do nothing Return (count/size of world.chickens) C. world.fixTheColoring color newColor world.randomlyColor color=newColor While world.ratioThisColor color=newColor <= 0.5 world.randomlyColor color=newColor print The percentage of blue chickens is joined with ((world.ratioThisColor color=newColor) as a string) 9A. 123 world.lastOneShorter obj person 123 indexOfShorter = AV size - 1 Loop 123 index from 0 up to (bn including) AV's size incrementing by 1 if the value at AV[index]'s height < person's height indexOfShorter set value to index else do nothing return indexOfShorter 9B. world.Greetings obj visitor Loop 123 index from 0 up to (bn including) AV's size incr by 1 if index != world.lastOneShorter person = visitor the value at AV[index] say Hello else the value at AV[index] say Boo Hoo, I am shorter than you