Question
Java Part 2. 1. Create a class called Animal that implements the Runnable interface. 2. In the main method create 2 instances of the Animal
Java Part 2.
1. Create a class called Animal that implements the Runnable interface.
2. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them user threads, as opposed to daemon threads.
3. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts as false. The position represents the position in the race for this Animal object. The restMax represents how long the Animal rests between each time it runs.
4. The rabbit rests longer than the turtle, but the rabbit has a higher speed. Let's make up some values to make this simulation more concrete. The race is 100 yards. The initial position is 0. Suppose the speed of the rabbit is 5, and its maxRest is 150. Suppose the speed of the turtle is 3, and its maxRest is 100.
I dont know if these rest times are interesting. Adjust them so that the rabbit wins sometimes, and the turtle wins sometimes.
5. In the main method start both the rabbit and the turtle and see which one wins the race.
6. Here is the behavior of the run method in the Animal class.
Loop until the position is >= 100 or there is a winner. Each time through the loop, sleep() some random number of milliseconds. This random number will be between 0 and maxRest. Advance the position of the Animal by its speed.
Print who is running, what their position is, each time through the loop.
When someone wins, set the static variable winner to true, and both threads will finish their run method, and thus stop.
The winner is announced from inside the run method.
Paste this part into the assignment document when you get it working, i.e., submit it separately from the next part.
Part 3. The objective here is to demonstrate the behavior of threads that share data, and use synchronized methods. You do NOT use wait / notify / notifyAll in this exericse.
When the above race is working, add to it in the following way.
Create a class called Food. It is not a Thread, and does not run. Its just a class that represents some data that will be shared by multiple threads.
Simulating an animal eating, simply means that the thread will sleep for some length of time. This is the same as the resting that the turtle an rabbit did in part 1.
There is one instance of the Food class that is shared by both of the animals. Pass it to the constructor of the Animal class for both the turtle and the rabbit. There is a method in the Food class called eat(). This method is synchronized, i.e., only one Animal can be eating at a time.
The rabbit eats the food (the thread will sleep) for a longer time than the turtle, thus giving an advantage to the rabbit.
But, the turtle must wait until the rabbit is done eating until it can eat, so the advantage is reduced.
Print out a message inside the eat method when the animal begins to eat, and when it is done eating. Indicate which animal it is that starts to eat.
Try making the eat method not synchronized, and observe the different behavior if the eat method allows the rabbit to begin eating before the turtle is done eating.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started