CompSci 94 Fall 2018 Test 2 Solution Problem 1 (10 pts) 1A) (3 pts) sceneActivatedListener pointOfViewChangeListener keyPressListener 1B) (3 pts) The hare moves backward 4 units. Then the marchHare turns to face the madHatter and moves towards the madHatter 0.5 units. 1C) (4 pts) If the user presses the M key the madHatter turns around once. If the user presses the H key, the hare jumps up and down, and the marchHare turns to face the madHatter and moves 0.5 units towards the madHatter. These two keyPress events will occur every time the keys are pressed. Problem 2 (12 pts) 2A) (6 pts) First, in the order they are shown in the array, blackCat, bunny, pig, etc. these animals one at a time turn right half way around so their back is facing the camera. Second, all the animals in the array at the same time turn back around, turning left this time, to face the camera. 2B) (3 pts) The babyYeti (in position 4) moves forward 1, then the pig (in position 2) moves forward 1 and then the blackCat (in position 0) moves forward 1. 2C) (3 pts) The blackCat (in position 0) turns all the way around, then the hare (in position 1) turns all the way around, and then the pig (in position 2) turns all the way around. Problem 3 (14 pts) PART A) A) (2 pts) 4 times B) (3 pts) For each chicken in the chickenArray it randomly decides whether or not to glue the chicken to the cow (set the chicken's vehicle to the cow). C) (2 pts) That IF is not needed since the indexChicken goes from 0 to indexChicken minus one. It doesn't need to be checked to see if it is less than indexChicken. Thus the code runs the same if the IF is dissolved. PART B) A) (2 pts) WholeNumber B) (2 pts) oneChicken is a variable C) (3 pts) Mystery returns the number of chickens that have been glued to the cow (or rather have their vehicle property set to the cow). Problem 4 (9 pts) A) (2 pts) The bunny moves up and down B) (2 pts) Nothing happens. C) (2 pts) The blackCat turns right all the way around. D) (3 pts) The tortoise moves to its right 2.0 units. Then the bunny moves up and down. Problem 5 (12 pts) A) (2 pts) No. UpdateScore is guarded by the game state. It is initially the string "setup". UpdateScore cannot be called until the game state is set to "playLevel1". It is not set until after the tortoise says 'Click on penguins to score'. B) (2 pts) In the addMouseClickOnObjectListener, add detail and specify this event is only for the penguin array. Then the event will not work for peacocks. C) (2 pts) procedure UpdateScore wholenumber someNumber this.setScoreNumber scoreNumber + someNumber this.setValue "" + this.scoreNumber D) (6 pts) Assume the mouseClicked event is only for penguins in the penguinArray and the parameter has been added to updateScore if this.gameState contentsEquals("playLevel1") is true then if event getModelAtMouseLocation getOpacity >= 0.95 event getModelAtMouseLocation setOpacity 0.7 updateScore 3 pts else if event getModelAtMouseLocation getOpacity >= 0.65 event getModelAtMouseLocation setOpacity 0.4 updateScore 2 pts else event getModelAtMouseLocation setOpacity 0.0 updateScore 1 pt Problem 6 (8 pts) A) (2 pts) The pig says hello and the chicken moves up and down. B) (2 pts) Nothing happens C) (2 pts) The pig says hello and the phoenix moves forward 1 D) (2 pts) The hare moves forward 1 and the chicken moves up and down. Problem 7 (4 pts) The problem is the loop is only executed once. The variable widestSoFar is set to the first element in the array, which is the pig. The first element in the array will not be wider than itself, so this returns the first time in the loop, which is too early. It is suppose to compare widestSoFar to every element in the loop, saving the widest in the variable widestSoFar. To fix the code, the "return widestSoFar" in the else should be removed. Problem 8 (12 pts) Part A (6 pts) WholeNumber function NumberWithVisibilityInRange with parameters: DecimalNumber minVisibility, DecimalNumber maxVisibility DoInOrder WholeNumber countVisibleInRange <= 0 for each SJointedModel oneOfThem in this.penguinArray if both minVisibility <= oneOfThem opacity AND oneOfThem getOpacity <= maxVisibility is true then countVisibleInRange <-- countVisibleInRange + 1 else do nothing return countVisibleInRange Part B (6 pts) Declare Procedure ComparePenguinsWithDifferentVisibility DoInOrder Whole Number numCompletelyVisible <-- this.NumberVisibilityInRange 0.95 1.1 Whole Number numPartiallyVisible <-- this.NumberVisibilityInRange 0.05 0.95 Whole Number numInvisible <-- this.NumberVisibilityInRange 0.0 0.05 if Both numCompletelyVisible > numPartiallyVisible and numCompletelyVisible > numInvisible is true then this.peacock say "There are more visible penguins" else if both numPartiallyVisible > numinvisible and numPartiallyVisible > numinvisible is true then this.peacock say "There are more partially visible penguins" else this.peacock say "There are more invisible penguins" ------------------------------------------------------------------------------ TO HANDLE A TIE (which you did not have to do) if Both numCompletelyVisible > numPartiallyVisible and numCompletelyVisible > numInvisible is true then this.peacock say "There are more visible penguins" else if both numPartiallyVisible > numinvisible and numPartiallyVisible > numinvisible is true then this.peacock say "There are more partially visible penguins" else if both numinvisible > numPartiallyVisible and numInvisible > numCompletelyVisible is true then this.peacock say "There are more invisible penguins" else this.peacock say "There is a tie somewhere" OR you could handle all the cases but there are a lot!