Indiana University of Pennsylvania Java Programming Code
Description
[40 points] Create a Java program that uses monitors to control access to a FoodBank object.
You will create a FoodBank class that has a food amount and methods to give and take food.
You will create two Thread classes for this program that will either put food into the food bank
or take food from the food bank. Food cannot be taken if there is no food available to take. This
is not a true producer/consumer problem. You only have one condition, which is to wait if there
is no food available to take. Both giving and taking food must involve locking the FoodBank
object and unlocking it when done. The FoodBank object must never enter an invalid state (a
negative amount of food) The methods in FoodBank to take and give food must print out exactly
what is happening with each action in the method, i.e. “Waiting to get food”, “Taking 10 items
of food, the balance is now 20 items”. NOTE: No other methods can print to the console.
A class FoodBank
FoodBank will have a single instance variable named food of type int. FoodBank will
define a default constructor which initializes food to zero. FoodBank will have two methods:
giveFood and takeFood. Both methods will have a single parameter of type int. giveFood will
add the value of the parameter to the food instance variable, takeFood will subtract the value.
A class FoodProducer
FoodProducer will have a single instance variable named bank of type FoodBank.
FoodProducer will have a parameterized constructor with a single parameter of type FoodBank.
The parameterized constructor will initialize the value of bank to the single parameter.
FoodProducer will extend the Thread class and override Thread’s run method. FoodProducer’s
run method will loop infinitely. On each loop iteration run will generate a random number from
1-100 and add that much food to the bank instance variable. After adding food, the thread will
sleep for 100 milliseconds.
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."