Introduction | FRQ Example | Quick Review | Feeder Class Simulation | Homework |
Feeder Class Simulation
AP CSA FRQ Tester: Feeder Class Simulation (2024)
This notebook allows you to test your implementation of the simulateOneDay
and simulateManyDays
methods for the Feeder class.
The test harness will:
- Append your code to a predefined test structure.
- Run several test cases.
- Display the results of the test cases.
Instructions
- Add the code as per the FRQ instructions in the //YOUR CODE HERE
-
Run both code cells to test your code for errors against provided test cases
// Your Implementation
import java.util.Random;
public class Feeder {
public int currentFood; // Amount of food in grams in the feeder
public Feeder(int initialFood) {
currentFood = initialFood;
}
/**
* Simulates one day with numBirds birds or possibly a bear at the feeder.
* Precondition: numBirds > 0
*/
public void simulateOneDay(int numBirds) {
Random rand = new Random();
int chance = rand.nextInt(100);
if (chance <= 95) {
int foodPerBird = rand.nextInt(41) ;
int totalFoodEaten = foodPerBird * numBirds;
if (currentFood >= totalFoodEaten) {
currentFood -= totalFoodEaten;
} else {
currentFood = 0;
}
} else {
currentFood = 0;
}
}
/**
* Simulates numDays of activity at the feeder with numBirds birds.
* Returns the number of days on which food was found in the feeder.
* Precondition: numBirds > 0, numDays > 0
*/
public int simulateManyDays(int numBirds, int numDays) {
int daysWithFood = 0;
int day = 0;
while (day < numDays) {
if (currentFood > 0) {daysWithFood++; }
simulateOneDay(numBirds);
// System.out.println(currentFood);
day++;
}
return daysWithFood;
}
}
public class FeederTest {
public static void main(String[] args) {
StringBuilder results = new StringBuilder();
// Test Case 1
Feeder feeder1 = new Feeder(2400);
feeder1.simulateOneDay(10);
results.append("Test Case 1: Current food after one day: ").append(feeder1.currentFood).append("\n");
// Test Case 2
Feeder feeder2 = new Feeder(250);
int daysWithFood2 = feeder2.simulateManyDays(3, 5);
results.append("Test Case 2: Days with food: ").append(daysWithFood2)
.append(", Remaining food: ").append(feeder2.currentFood).append("\n");
// Test Case 3
Feeder feeder3 = new Feeder(0);
int daysWithFood3 = feeder3.simulateManyDays(5, 10);
results.append("Test Case 3: Days with food: ").append(daysWithFood3)
.append(", Remaining food: ").append(feeder3.currentFood).append("\n");
// Print Results
System.out.println(results.toString());
}
}
// Run the Test Harness
FeederTest.main(null);
Test Case 1: Current food after one day: 2120
Test Case 2: Days with food: 5, Remaining food: 0
Test Case 3: Days with food: 0, Remaining food: 0