CompSci 94 Test 2 Practice April 2018 Solutions 1 A) There are three events. 1 Scene Activated Listener 2 Time Elasped Listeners 1B) MyFirstMethod - handles the sceneActivatedListener event First TimeElapsedListener - handled by an if statement if the hare and tortoise are less than 1.0 apart and this event currently isn't being handled, it also calls the procedure something Second TimeElapsed Listener - handed by an if statement if the hare and tortoise are about 1.0 or further apart - then the penguin turns to face the hare, and moves over to it and then says hello 1C) Since the penquin and hare are far apart, the penguin turns to face the hare and repeatedly moves closer to it, stopping close to it and says hello. 1D) A) Move the tortoise to the table. Nothing else happens. 1D) B) Move the penguin to the table The penguin turns to face the hare, and tries to move over to it. But note that the penguin may be turning still as we move the penguin to the table, so that when it starts moving forward, it may not be facing the hare anymore and may pass by the hare and keep going. 1D) C) Move the hare to the table The penguin turns to face the hare and tries to move over to it. But note that the penguin may be turning still as we move the hare to the table, so that when it starts moving forward, it may not be facing the hare anymore and may pass by the hare and keep going. 2) The panda says a random number from 0 to 5. Then the characters in the array move up that amount one at a time. Then the object in the index position that is the random number, that object turns around once. 3) A) Rewrite mouseClicked to: if getModelAtMouseLocation == soccerball updateScore increment 1 else if getModelAtMouseLocation == volleyball updateScore increment 2 else // must be pumpkinHead updateScore increment 5 3B) The game will run the same except now when you click on volleyball you will get 3 pts added to the score, 2 from one event and 1 pt from the new event. 4A) declare WholeNumber function numberVisible WholeNumber count = 0 for each Flyer onePenguin in penguins if onePenguin getOpacity > 0.5 is true then count = count + 1 return count 4B) declare procedure makeInvisible WholeNumber randomIndex = nextRandomIngegerFromAUpToButExcludingB 0, penguins.length // need to make sure the penguin at the randomIndex location is visible. If // not, we need to generate another randomIndex while penguins[randomIndex] getOpacity < 0.5 is true // That penguin is already invisible, generate another randomIndex randomIndex = nextRandomIntegerFromAUpToButExcludingB 0, penguins.length loop // at this point, randomIndex should be the location of a penguin that is // visible, say. make that penguin invisible now!! penguins[randomIndex] setOpacity 0.0 4C) declare procedure MakeAllButNumInvisible WholeNumber number while numbervisible > number makeInvisible 5A) declare WholeNumber function numberOfTypeBird Flyer birdType WholeNumber birdCount = 0 For each Flyer oneBird in birds if oneBird toString startsWith birdType toString is true then birdCount = birdCount + 1 Return birdCount 5B) declare WholeNumber function indexLocationOfBird, Flyer oneBird WholeNumber index = 0 for each Flyer aBird in birds if aBird toString startsWith oneBird toString is true then return index index = index + 1 loop // Should never get here, you should return for some position in the array // This last return is just in case, even though return 0 5C) declare procedure changeBirdInArray Flyer birdOne, Flyer birdTwo for each Flyer someBird in birds if someBird toString contentEquals birdOne toString is true then birds[indexLocationOfBird birdOne] = birdTwo