2024 AP Computer Science A FRQ #1A

Write the simulateOneDay method, which simulates numBirds birds or possibly a bear at the feeder for one day. The method determines the amount of food taken from the feeder on this day and updates the currentFood instance variable. The simulation accounts for normal conditions, which occur 95% of the time, and abnormal conditions, which occur 5% of the time. Under normal conditions, the simulation assumes that on any given day, only birds visit the feeder and that each bird at the feeder consumes the same amount of food. This standard amount consumed is between 10 and 50 grams of food, inclusive, in 1-gram increments. That is, on any given day, each bird might eat 10, 11, . . . , 49, or 50 grams of food. The amount of food eaten by each bird on a given day is randomly generated and each integer from 10 to 50, inclusive, has an equal chance of being chosen. For example, a run of the simulation might predict that for a certain day under normal conditions, each bird coming to the feeder will eat 11 grams of food. If 10 birds come to the feeder on that day, then a total of 110 grams of food will be consumed. If the simulated food consumed is greater than the amount of food in the feeder, the birds empty the feeder and the amount of food in the feeder at the end of the day is zero. Under abnormal conditions, a bear empties the feeder and the amount of food in the feeder at the end of the day is zero. The following examples show possible results of three calls to simulateOneDay. • Example 1: If the feeder initially contains 500 grams of food, the call simulateOneDay(12) could result in 12 birds eating 20 grams of food each, leaving 260 grams of food in the feeder. • Example 2: If the feeder initially contains 1,000 grams of food, the call simulateOneDay(22) could result in a bear eating all the food, leaving 0 grams of food in the feeder. • Example 3: If the feeder initially contains 100 grams of food, the call simulateOneDay(5) could result in 5 birds attempting to eat 30 grams of food each. Since the feeder initially contains less than 150 grams of food, the feeder is emptied, leaving 0 grams of food in the feeder. Intro: 00:00 Pseudocode: 02:00 Writing the code: 04:54 Testing the first method call: 11:59 Testing the second method call: 13:03 Testing the third method call: 13:33