
Code:
public class Feeder
{
// Instance variable
int currentFood;
/**
* Simulates one day with numBirds birds or possibly a bear at the bird feeder,
* as described in part (a)
* Precondition: numBirds > 0
*/
public void simulateOneDay(int numBirds)
{
double chance = Math.random();
// 5% chance a bear visits
if (chance < 0.05)
{
currentFood = 0;
}
else
{
int gramsPerBird = (int)(Math.random() * 41) + 10; // 10–50 inclusive
int totalConsumed = numBirds * gramsPerBird;
if (totalConsumed >= currentFood)
{
currentFood = 0;
}
else
{
currentFood -= totalConsumed;
}
}
}
/* There may be instance variables, constructors, and methods that are not shown. */
public static void main(String[] args)
{
Feeder feeder = new Feeder();
feeder.currentFood = 500;
System.out.println("Starting food: " + feeder.currentFood);
feeder.simulateOneDay(12);
System.out.println("Food after one day: " + feeder.currentFood);
}
}
Approach
- The method first generates a random number to determine whether the day is abnormal (a bear visits) or normal (only birds visit).
- If the random value indicates a bear visit (5% chance), the feeder is immediately emptied by setting currentFood to 0.
- Otherwise, the method randomly selects how many grams each bird eats (from 10 to 50 grams, inclusive).
- It then calculates the total food eaten by multiplying the number of birds by the grams per bird.
- If the total food eaten is greater than or equal to the amount in the feeder, the feeder is emptied; otherwise, the total eaten is subtracted from currentFood.

/**
* Returns the number of days birds or a bear found food to eat at the feeder
* in this simulation, as described in part (b)
* Preconditions: numBirds > 0, numDays > 0
*/
public class Feeder
{
int currentFood; // instance variable
// Minimal simulateOneDay so code compiles and runs
public void simulateOneDay(int numBirds)
{
int foodEaten = numBirds * 10;
currentFood = Math.max(0, currentFood - foodEaten);
}
public int simulateManyDays(int numBirds, int numDays)
{
int daysWithFood = 0;
for (int day = 0; day < numDays; day++)
{
if (currentFood == 0)
{
break;
}
daysWithFood++;
simulateOneDay(numBirds);
}
return daysWithFood;
}
public static void main(String[] args)
{
Feeder feeder = new Feeder();
feeder.currentFood = 50; // starting food
int result = feeder.simulateManyDays(2, 10);
System.out.println(result); // Expected output: 3
}
}
Approach
- The method starts by creating a counter called daysWithFood to track how many days food was available at the feeder.
- It runs a loop for at most numDays days to simulate each day one at a time.
- At the start of each loop iteration, it checks whether currentFood is zero and stops the simulation early if the feeder is empty.
- If there is still food, it counts the day by incrementing daysWithFood and then calls simulateOneDay(numBirds) to simulate visitors eating that day.
- After the loop finishes, the method returns daysWithFood, which represents the number of days visitors found food at the feeder.