Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, Please Create following program. Thank you. Create a class called Anima

ID: 3597475 • Letter: I

Question

In Java, Please Create following program. Thank you.

Create a class called Animal that implements the Runnable interface.

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.

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.

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 don’t 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.

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.

After the the program is running---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. It’s 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. 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.

Explanation / Answer

//Animal.java

package emp;

import java.util.Random;

public class Animal extends Thread implements Runnable {

private String animalName;

private int position;

private int speed;

private int restMax;

private Food food;

private static boolean winner = false;

private static int finishPosition = 100;

Random rnd = new Random();

public Animal(String animalName, int position, int speed, int restMax,Food food) {

this.animalName = animalName;

this.position = position;

this.speed = speed;

this.restMax = restMax;

this.food=food;

}

public String getAnimalName() {

return animalName;

}

public void setAnimalName(String animalName) {

this.animalName = animalName;

}

public int getPosition() {

return position;

}

public void setPosition(int position) {

this.position = position;

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

public long getRestMax() {

return restMax;

}

public void setRestMax(int restMax) {

this.restMax = restMax;

}

@Override

public void run() {

while (this.position <= finishPosition && !winner) {

try {

long randomRest = rnd.nextInt(restMax);

Animal.sleep(randomRest); food.eat(this.animalName);

this.position += this.getSpeed();

System.out.println(this.animalName + " is at position " + this.position);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

if (this.position >= finishPosition) {

Animal.winner = true;

System.out.println("The race is over!");

System.out.println(this.animalName + " wins!");

}

}

}


//Race.java

package emp;

public class Race {

public static void main(String[] args) {

Food food=new Food();

Animal rabbit = new Animal("rabbit", 0, 5,300,food);

Animal turtle = new Animal("turtle", 0, 3, 100,food);

rabbit.setDaemon(false);

turtle.setDaemon(false);

rabbit.start();

turtle.start();

}

}


//Food.java

package emp;

public class Food {

private static boolean eating = false;

public Food() {

}

synchronized public int eat(String o) {

System.out.println(o+" Starting to eat");

eating = true;

try {

Thread.sleep((long) (150 * Math.random()));

} catch (InterruptedException e) {

e.printStackTrace();

}

return 0; // Return something here

}

}


/*output:-

turtle Starting to eat

rabbit Starting to eat

turtle is at position 3

turtle Starting to eat

rabbit is at position 5

turtle is at position 6

turtle Starting to eat

turtle is at position 9

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 10

turtle is at position 12

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 15

rabbit Starting to eat

turtle is at position 15

rabbit is at position 20

rabbit Starting to eat

rabbit is at position 25

turtle Starting to eat

rabbit Starting to eat

turtle is at position 18

turtle Starting to eat

rabbit is at position 30

rabbit Starting to eat

turtle is at position 21

turtle Starting to eat

rabbit is at position 35

turtle is at position 24

turtle Starting to eat

rabbit Starting to eat

turtle is at position 27

turtle Starting to eat

rabbit is at position 40

turtle is at position 30

turtle Starting to eat

turtle is at position 33

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 45

rabbit Starting to eat

turtle is at position 36

turtle Starting to eat

rabbit is at position 50

turtle is at position 39

turtle Starting to eat

turtle is at position 42

turtle Starting to eat

turtle is at position 45

rabbit Starting to eat

rabbit is at position 55

turtle Starting to eat

turtle is at position 48

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 60

turtle is at position 51

rabbit Starting to eat

rabbit is at position 65

turtle Starting to eat

turtle is at position 54

turtle Starting to eat

rabbit Starting to eat

turtle is at position 57

turtle Starting to eat

rabbit is at position 70

turtle is at position 60

turtle Starting to eat

turtle is at position 63

rabbit Starting to eat

rabbit is at position 75

turtle Starting to eat

turtle is at position 66

rabbit Starting to eat

rabbit is at position 80

turtle Starting to eat

turtle is at position 69

turtle Starting to eat

rabbit Starting to eat

turtle is at position 72

rabbit is at position 85

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 90

rabbit Starting to eat

turtle is at position 75

turtle Starting to eat

rabbit is at position 95

turtle is at position 78

turtle Starting to eat

turtle is at position 81

rabbit Starting to eat

turtle Starting to eat

rabbit is at position 100

turtle is at position 84

turtle Starting to eat

rabbit Starting to eat

turtle is at position 87

rabbit is at position 105

The race is over!

rabbit wins!

turtle Starting to eat

turtle is at position 90

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote