Assignment 8Write the following Java programs and upload the FoodBank and RaceTrack files to Canvas
when the assignment is complete. Please note that the use of the stop and suspend methods are
prohibited for both programs. When doing this, please use Java 8 so that it works with
gradescope.
Note: The test cases require JUnit 4 (JUnit 5 will not work)
[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.
A class FoodConsumer
FoodConsumer is identical to FoodProducer except that the random number generated in
run will be removed from the FoodBank object.
A class FoodBankPatrons
FoodBankPatrons will have a main method in which a FoodBank, FoodProducer, and
FoodConsumer object are created. The FoodProducer and FoodConsumer must share the same
FoodBank object. Once created, the main method starts these threads.
[60 points] Create a Java program which will use multiple threads to simulate a race between
cars. The program should look like:
Create a class RaceTrack which uses JavaFX* (subclasses the Application class) and overrides
its start method to do the required drawing. Each car must be advanced by a separate Thread. On
each thread iteration, each car should advance a random 0-10 pixels forward. Once advanced, a
thread should sleep for 50 miliseconds. The threads will execute until a car reaches the end of the
track. Once this occurs, an Alert should be spawned alerting the user of the winner and all cars
stop. The primaryStage will be 500px by 200px and is not be resizable.
Please make the filepath for the car image a constant defined at the beginning of your
RactTrack.java file so I can easily modify it for grading. (Or leave a comment indicating that the
image path is a working web URL instead)
HINT: Updating the GUI from a non-GUI thread is considered bad practice as it leads to race
conditions and UI errors. If you need to update a GUI component from a non-GUI thread, please
use the runLater method defined in the Platform class; only update the GUI with runLater (it will
spin off its own thread). Don’t be dissuaded by the “future time” a runnable object passed to
runLater will be executed. We can expect that any runnable object passed to runLater will be
executed quickly enough for the purposes of this project.
*Java 8 comes with JavaFX and should work out of the box and you should be using Java 8. For
all later Java versions, you must install JavaFX. Instructions can be found here. Please note that
the provided URL has instructions for installing JavaFX both on a system and for specific IDE’s,
including Intelji. It is recommended you follow the tutorial for the specific IDE you are using.
Grading Rubric for FoodBank:
Category
Classes defined correctly
Correctly uses Java object monitors to enforce
mutual exclusion
FoodBank state remains valid even with
multiple Consumer threads active
Total
Points
20
10
10
40
Grading Rubric for RaceTrack:
Category
GUI Implemented correctly
Each car is run by a separate thread
Start button works as described
Winner is random and stops race from
continuing
Pause button works as described
Reset button works as described
Total
Points
15
15
10
10
5
5
60
Essay Writing Service Features
Our Experience
No matter how complex your assignment is, we can find the right professional for your specific task. Achiever Papers is an essay writing company that hires only the smartest minds to help you with your projects. Our expertise allows us to provide students with high-quality academic writing, editing & proofreading services.Free Features
Free revision policy
$10Free bibliography & reference
$8Free title page
$8Free formatting
$8How Our Dissertation Writing Service Works
First, you will need to complete an order form. It's not difficult but, if anything is unclear, you may always chat with us so that we can guide you through it. On the order form, you will need to include some basic information concerning your order: subject, topic, number of pages, etc. We also encourage our clients to upload any relevant information or sources that will help.
Complete the order form
Once we have all the information and instructions that we need, we select the most suitable writer for your assignment. While everything seems to be clear, the writer, who has complete knowledge of the subject, may need clarification from you. It is at that point that you would receive a call or email from us.
Writer’s assignment
As soon as the writer has finished, it will be delivered both to the website and to your email address so that you will not miss it. If your deadline is close at hand, we will place a call to you to make sure that you receive the paper on time.
Completing the order and download