CompSci 94 Fall 2022 Exam 3 Solutions 1. While loop Iteration 1) 7 2) 3 3) Line 01 is creating and initializing the variable someValue Line 06 is updating or changing the value of the variable someValue 2. Array index problems A) 5 pts - 2, 2, 1 pts B) 5 pts) C) 4 pts Part A) One at a time, the objects in the creatures array turn around once. They turn around in the order they appear in the creatures array: bear, camel, cow, elk, horse, and then wolf. Then at the same time, all the objects in the creatures array say "hello" Then cow says "restaurant" Part B) The camel says "museum" The cow says "movie" The elk says "restaurant" The horse says "zoo" Part C) Explanation: The problem with the code is the indexOne updates only when a word starts with "m". For any other word indexOne never updates so it gets stuck in an infinite loop. To FIX: the line indexOne <- indexOne + 1 should be moved to immediately after the if statement. OR Add indexOne <- indexOne + 1 right after the cow says the word in the words array as part of the True part of the if. Problem 3) A) Quadruped B) The elk says "cheers" C) The camel says "hello" D) The bear says "goodbye" E) The mystery procedure returns the first animal in the array creatures that is less than 1.5 units from the animal passed in. If no such creatures in the array are within 1.5 units from the animal, it returns the first animal in the array. Problem 4 A) Nothing B) Mad Hatter turn around once C) The Mad Hatter moves up and down once D) The whiteRabbit says I'm late Problem 5 A) The bunny turns right once, the bunny says "lunch" and then the pig moves backwards once B) The panda says "hello", the bunny says "lunch", and the hare moves up and down. C) The panda says "hello", the bunny says "lunch" and the pig moves backwards D) nothing happens Problem 6 When the run button is pressed: At the same time, the hare moves forward one unit and the tortoise moves to its left one unit. Then the bunny moves back one unit (since the tortoise collided with it) Then the pig moves to its right one unit (since the bunny collided with it) Problem 7 2 pts and 4 pts Part A) When the run button is pressed... The bunny moves right one unit and then the tortoise moves to its right one unit (due to the collision with the bunny) Part B) If the H key is pressed, the hare turns around once If the R key is pressed, the pig moves to its right. If the pig collides with the hare, the hare moves backward one unit. If any key other than H or R is pressed, the tortoise moves left one unit. The tortoise could collide with the bunny and if that happens, the tortoise moves to the right one. Problem 8 A) 3 B) 3 C) 4 D) 4 Part A) The score is initialized to 0 and displayed as 0. Part B) Add a WholeNumber parameter named amount. Change the 1 in the first line to add amount to the score as: this setScoreNum scoreNum this.scoreNum + amount Part C) this addMouseClickOnObjectListener setOfVisuals new Visual[] this.bunny, this.penguin, this.poodle declare procedure mouseClicked do in order if event getModelAtMouseLocation == this.bunny this.score UpdateScore amount: 2 else if event getModelAtMouseLocation == this.penguin this.score UpdateScore amount: 5 else this.score UpdateScore amount: 6 Part D) You would need some kind of gamestate that would have multiple strings gamestate = "bunny" When you click on the bunny you would change the game state to "penguin" When you click on the penguin you would change the game state to "poodle" When you click on the penguin you would change the game state to "gameover" In the event for clicking on objects you would modify it to: If the thing you clicked on is bunny and gameState is "bunny" then you would update the score and change the gamestate to "penguin" so you could only get points for penguin next. If the thing you clicked on is penguin and gameState is "penguin" then you would update the score and change the gamestate to "poodle" so you could only get points for poodle next. If the thing you clicked on is poodle and gameState is "poodle" then you would update the score and change the gamestate to "gameover". This gamestate forces the user to click on the bunny once, then penguin once and then poodle once. OR Another way is to check if score is 0 and opacity of bunny > 0.95 then update score by 2 and change the opacity of the bunny to 0.9 if score is 2 and opacity of penguin > 0.95 then update score by 5 and change the opacity of the penguin to 0.9 if score is 7 and opacity of poodle > 0.95 then update score by 6 and change the opacity of the poodle to 0.9 Problem 9 Part A) Paint function newPaintColor do in order WholeNumber randomNumber <- nextRandomIntegerFrom AUpToAndIncludingB 1, 4 if randomNumber == 1 is true then return RED else if randomNumber == 2 is true then return BLUE else if randomNumber == 3 is true then return YELLOW else return GREEN Part B) declare WholeNumber function countPaintColors with parameters Paint paint1, Paint paint2 do in order WholeNumber count <- 0 for each Penguin onePenguin in this.penguins if either onePenguin getPaint == paint1 OR onePenguin getPaint == paint2 count <- count + 1 else loop return count Part C) for each Penguin somePenguin in this.penguins somePenguin setPaint somePenguin newPaintColor loop this.penguin say "Number of penguins red or green is " + this countPaintColors paint1 RED, paint2 GREEN