Important Notes:
What Is a Java Method?
- A method in Java is a reusable block of code that performs a specific task.
- Consists of an access modifier (e.g., public or private), a return type (e.g., void if it doesn’t return a value), a method name, optional parameters enclosed in parentheses, and a method body enclosed in curly braces.
Part (a):
- this question usually requires you to write a certain method that fits the given test cases and "rules" provided by the problem
- rules + test cases may be provided as a table or as example method calls
- a precondition/postcondition may be provided by the problem.
- a boilerplate segment containing a method declaration is also given
Point Deductions:

Part (b):
- this part is similar to part (a) and typically asks you to write a method that satisfies the given set of rules.
- it may build upon the method created in part (a) (when this is done, it allows you to assume that the method in part (a) works as intended)
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) {
double conditions = (Math.random() * 100);
int totalFoodEaten = 0;
if(conditions >= 5.0)
{
for (int birds = 1; birds <= numBirds; birds++) {
int foodEaten = (int) (Math.random() * (50 - 10 + 1)) + 10;
totalFoodEaten += foodEaten;
}
if (totalFoodEaten >= currentFood)
{
System.out.println("All food in feeder eaten!");
currentFood = 0;
}
else
{
currentFood = currentFood - totalFoodEaten;
System.out.println(currentFood + " grams left in feeder");
}
}
else
{
currentFood = 0;
System.out.println("Bear ate all the food. " + currentFood + " grams left in feeder.");
}
}
/**
* 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;
for (int days = 1; days <= numDays; days++)
{
if (currentFood > 0)
{
System.out.println("Day: "+ days);
simulateOneDay(numBirds);
daysWithFood++;
}
else
{
break;
}
}
return daysWithFood;
}
}
Feeder feeder = new Feeder(2400);
// feeder.simulateOneDay(12); // Runs one day simulation with 12 as number of birds in simulation.
feeder.simulateManyDays(10, 5); // Runs multiple days with 10 being number of birds and 5 being days in simulation.
Day: 1
2125 grams left in feeder
Day: 2
Bear ate all the food. 0 grams left in feeder.
2