CompSci 94 Exam 2 Fall 2019 Solutions Problem 1 PART A. (4 pts) The bunny moves forward 1.0 (and collides with the hare). Then the hare moves backward 1.0. Then the bunny moves forward 1.0 (and collides with the pig). Then the pig moves backward 1.0. Then the bunny says "hello". Problem 1 PART B - A) (3 pts) The hare moves left 3.0 (actually moves left 1.0 three times) Then the pig moves backward 1.0 Note: The hare also collides with the bunny but there is no collision event for that. Problem 1 PART B - B) (3 pts) The user presses the B key and the bunny moves forward 1.0. The user presses the B key two more times and the bunny collides with the panda. The panda will move backward 1.0. Then everytime the user presses the B key the bunny moves forward 1 and collides with the panda, who moves backward 1. Problem 2 Part A (6 pts) The animals one at a time move up 1 in the order they are shown from left to right, marchHare first, then whiteRabbit, etc. Then the animals all together move down 1.0. Then the marchHare says hello, then the tortoise says hello and then the bunny says hello. Problem 2 Part B (4 pts) The first three animals in the array are painted blue. That is, the marchHare, whiteRabbit and tortoise, one at a time are painted blue. Problem 2 Part C (4 pts) The whiteRabbit is painted Gray. Then Alice is painted red Then the panda is painted green. Problem 3 PART A (9 pts) A) variable B) total length of pandas in the array C) 10 D) The mysteryProc processes all the pandas in the pandas array. If a panda is red it is doubled in size, and otherwise the panda resizes to half its size. Problem 3 PART B (7 pts) A) wholeNumber B) mysteryFunction returns the number of pandas that are shorter than someAnimal. C) mysteryFunction now returns the total number of pandas plus the number of pandas that are shorter than someAnimal. Problem 4 (8 pts) A) cow says hello and moves up and down B) wolf says hello and moves backwards C) poodle says hello and turns once around D) nothing Problem 5 (12 pts) A) Allows you to click on any object and drag it around in the scene. B) Bunny says "CompSci 94" and then the penguin says "score". C) Flamingo turns around once and the panda says "The end" D) nothing happens, they are both in set A. E) addCollisionStartListener new SThing[] {flamingo}, new SThing[] {bunny, pentuin, panda} declare procedure collisionStarted event getSThingFromSetA, event getSthingFromSetB getsthingFromSetB setOpacity opacity 0.0 Problem 6 (12 pts) A) Tortoise says Click on Tortoises for points, then the timer is set to 20 and the score is set to 0. B) Add a wholeNumber parameter named amount. Then the first line should be changed to: this.setTimerNumber timerNumber this.timerNumber - amount C) this.timer updateTimer amount 1 D) addMouseClickOnObjectListener setOfVisuals this.yetibabies this.timer updateTimer amount -5 E) In the timeElapsed event the code should be: if timerNumber > 0 then this.timer updateTimer amount 1 Problem 7 (6 pts) A) All the bunnies move down 10 when the wolf collides with a single bunny. Only that bunny is supposed to move down. The score should increase by the correct amount. B) The line "someBunny move Down 10" should be the first line in the first if statement. That is the code should be: for each Bunny someBunny in this.bunnies if someBunny == event.getSthingFromSetB is true then someBunny move Down 10.0 duration 0.0 if someBunny getPaint == RED is true then this.Scorer increaseScore amount 3 else this.Scorer increaseScore amount 1 else Do nothing loop ends Problem 8 Part A) (6 pts) declare procedure resizeSmallOnes DecimalNumber heightCutOff, DecimalNumber minNumber, DecimalNumber maxNumber for each Bunny someBunny in this.bunnies if someBunny getHeight < heightCutoff someBunny resize nextRandomRealNumberInRange minNumber maxNumber Problem 8 Part B) (6 pts) declare procedure changeColor WholeNumber amount, Paint someColor WholeNumber index = 0 count up to amount bunnies[index] setPaint paint someColor index <-- index + 1 ALT SOLN: declare procedure changeColor WholeNumber amount, Paint someColor WholeNumber index = 0 while index < amount is true bunnies[index] setPaint paint someColor index <-- index + 1 Problem 8 Part C) (6 pts) declare WholeNumber function howManyTheseColors Paint color1, Paint color2 WholeNumber count = 0 for each Bunny oneBunny in this.bunnies if oneBunny getPaint == color1 OR if oneBunny getPaint == color2 then count <- count + 1 return count ALT SOLN: declare WholeNumber function howManyTheseColors Paint color1, Paint color2 WholeNumber count = 0 for each Bunny oneBunny in this.bunnies if oneBunny getPaint == color1 then count <- count + 1 else if oneBunny getPaint == color2 then count <- count + 1 return count ALT SOLN WholeNumber count = 0 // this variable is for counting WholeNumber index = 0 // this variable is for indexing the array while index < bunnies.length if bunnies[index] getPaint == color1 OR if bunnies[index] getPaint == color2 then count <- count + 1 index <- index + 1 return count Note: index gets updated each time in the loop, whereas count only gets updated if the color of the bunny matches.