CompSci 94 Exam 3 Fall 2024 Solutions Problem 1: 1.A. 10 times 1.B.1 amount <- amount - 5 OR: amount <- amount - 6 OR: amount <- amount - 7 The number to subtract could also be any number from 5 to 7. 1.B.2 If you use amount <- amount - 5, then panda says: amount is 0 If you use amount <- amount - 6, then panda says: amount is -3 If you use amount <- amount - 7, then panda says: amount is -6 -------------------------------------------------------------------- Problem 2: 2 A) First, each animal in the array says rain, one at a time in this order: pig, panda, tortoise, yetiBaby, bunny. Next, all the objects in the array animals (pig, panda, tortoise, yetiBaby and bunny) say Duke at the same time Last, the panda is painted yellow. 2 B) The pig turns green. Then the panda turns red. Then the tortoise turns blue. Then the yetiBaby turns yellow. Then the bunny turns black. 2 C) There are five animals in the array at index positions 0 to 4. The while loop condition: while indexTwo < 6 means that when indexTwo is 5, the next line if this.animals[indextwo] not equal to this.pig will freeze because this.animals does not have a position 5. To fix the error, change the while condition to: while indexTwo < 5 OR change the while condition to: while indexTwo < animals.length since animals.length would be 5 in this case. -------------------------------------------------------------------- Problem 3: A) Biped B) someAnimal C) SJointedModel D) the pig says hello E) The bunny says The End F) The mystery function returns the animal in the array animals that is furthest from the parameter someAnimal -------------------------------------------------------------------- Problem 4: 4.A) The pig says "Donuts are yummy" 4.B) The bunny moves up 0.25 4.C) Hare turns right 0.5 (halfway around) 4.D) nothing happens. Hare is not in the set of visuals array. -------------------------------------------------------------------- Problem 5: A) bunny says one, hare says four, and pig says six B) hare says two, hare says four, panda says five C) nothing happens. They are in the same set. D) bunny says one, hare says four, panda says five -------------------------------------------------------------------- Problem 6: The bunny moves right 1.0 and collides with yetiBaby, and at the same time the hare move left 1.0 and collides with the pig Next, the yetiBaby moves backwards 1.0 and at the same time the hare turns all the way around and also at the same time the pig moves forward 1.0 Next, the hare moves right 1.0 and collides with yetiBaby Next, the yetiBaby moves backwards 1.0 -------------------------------------------------------------------- Problem 7: A) The pig moves right 1 Then the hare moves left 1 Then the pig moves backward 1 B) If the user pressed the R key, the bunny turns all the way around once. If the user presses any other key than R, the yetiBaby moves backward once. *) If the user presses any other key then R again, the yetiBaby moves backward once again, and this time collides with the pig. That results in the hare moving left once and the pig moving backward once. The star move can happen over and over again if the user keeps pressing any key other than the R key. -------------------------------------------------------------------- Problem 8: A) The poodle says: Click on me to start When you click on the poodle, the timer appears with value 20 and starts counting down, subtracting 1 every second. B) declare procedure updateTimer with parameter: WholeNumber amount this setTimerNumber this.timerNumber - amount this setValue "" + this.timerNumber C) if this.poodle getOpacity > 0.95 this.Timer initializezTimer startValue 20 this.poodle setOpacity 0.9 D) if getModelAtMouseLocation == bunny this.Timer updateTimer amount: -5 if getModelAtMouseLocation == yetiBaby this.Timer updateTimer amount: 3 E) while this.timerNumber > 0 and this.scoreNumber < 10 F) not graded -------------------------------------------------------------------- Problem 9: A) declare DecimalNumber function randomOpacity WholeNumber randominteger <- nextRandomWholeNumberFromAUpToAndIncludingB 1, 4 if randomInteger == 1 is true then return 0.0 else if randomInteger == 2 is true then return 0.25 else if randomInteger == 3 is true then return 0.5 else return 0.75 B) declare WholeNumber function howManyInRange with parameters: DecimalNumber minRange, DecimalNumber maxRange WholeNumber count <-- 0 for each Penguin onePenguin in this.penguins if BOTH onePenguin getOpacity >= minRange AND onePenguin getOpacity <= maxRange count <= count + 1 else loop return count C) declare procedure myFirstMethod for each Penguin somePenguin in this.penguins somePenguin setOpacity this randomOpacity this.penguin say "Number of penguins with opacity from 0.25 to 0.75 is " + this howManyInRange minRange 0.25, maxRange 0.75 this.penguin2 say "Number of penguins with opacity from 0.25 to 0.5 is " + this howManyInRange minRange 0.25, maxRange 0.5