
// CODE_RUNNER: BirdProblemA (modified).
public class Feeder
{
// Tracks how much food is currently in the feeder
private int foodRemaining;
/**
* Simulates one day at the bird feeder.
* A bear may visit, or birds may eat food.
* Precondition: birds > 0
*/
public void simulateOneDay(int birds)
{
double rand = Math.random();
// Bear visits with a 5% probability
if (rand <= 0.05)
{
foodRemaining = 0;
return;
}
int gramsEach = (int)(Math.random() * 41) + 10; // 10–50 grams
int eaten = birds * gramsEach;
if (eaten >= foodRemaining)
{
foodRemaining = 0;
}
else
{
foodRemaining -= eaten;
}
}
/* Other constructors, variables, or methods may exist */
}
public class Main
{
public static void main(String[] args)
{
Feeder feeder = new Feeder();
// Initialize feeder with food
feeder.foodRemaining = 500;
System.out.println("Starting food: " + feeder.foodRemaining);
feeder.simulateOneDay(12);
System.out.println("Food after one day: " + feeder.foodRemaining);
}
}
Main.main(null);

// CODE_RUNNER: BirdProblemB (modified)
/**
* Simulates multiple days at a bird feeder and returns
* the number of days food was available.
* Preconditions: birds > 0, days > 0
*/
public class Feeder
{
int foodLeft; // instance variable
// Minimal version so the program runs
public void simulateOneDay(int birds)
{
int consumed = birds * 10;
foodLeft = Math.max(0, foodLeft - consumed);
}
public int simulateManyDays(int birds, int days)
{
int count = 0;
for (int i = 0; i < days; i++)
{
if (foodLeft <= 0)
{
break;
}
count++;
simulateOneDay(birds);
}
return count;
}
/* Other instance variables and methods not shown */
}
public class Main
{
public static void main(String[] args)
{
Feeder feeder = new Feeder();
feeder.foodLeft = 50; // initial food amount
int daysFed = feeder.simulateManyDays(2, 10);
System.out.println(daysFed); // Expected output: 3
}
}
Main.main(null);
Things I Learned
- I learned how to use instance variables to track food levels across multiple days and maintain the object’s state throughout the simulation.
- I learned how conditional logic models different outcomes, such as birds eating daily or a bear visiting occasionally, and how those conditions affect the flow of the program.
- I learned how loops can simulate repeated events over time and allow the program to automatically update values day by day.
- I learned how helper methods allow complex simulations to be broken into manageable parts, making the code more organized and easier to debug.
- I learned how to think step-by-step when designing a simulation so that each event happens in the correct order.
- I learned how small changes in logic can significantly impact the final result of a program.
- I learned how to test edge cases, such as when food is almost gone, to ensure the simulation behaves correctly.
Things I Struggled With
- I struggled with knowing when to stop the simulation once food ran out and ensuring the loop condition reflected that correctly.
- I struggled with correctly updating the food amount after each day without accidentally subtracting too much or too little.
- I struggled with tracing how values changed inside a loop, especially when multiple conditions were involved.
- I struggled with small logic errors that affected the final day count, even when the code seemed mostly correct.
- I struggled with debugging because sometimes the output was slightly off, making it hard to identify exactly where the mistake occurred.