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

Java. 1. Create a class called Animal that implements the Runnable interface. 2.

ID: 3809470 • Letter: J

Question

Java.

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 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.

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.

Dont forget to add this part, Please!

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 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.

Explanation / Answer

PROGRAM CODE:

Animal.java

package runnable;

public class Animal implements Runnable{

   static boolean isGameDone;

   String name;

   int position;

   int speed;

   int maxRest;

   Food food;

  

   public Animal(String name, int pos, int speed, int maxrest, Food food) {

       isGameDone = false;

       this.name = name;

       this.position = pos;

       this.speed = speed;

       this.maxRest = maxrest;

       this.food = food;

   }

   @Override

   public void run() {

       while(position<100 && !isGameDone)

       {

           position += speed;

           System.out.println(this);

           food.eat(maxRest);

       }

       isGameDone = true;

  

       System.exit(0);

   }

   public int getPosition() {

       return position;

   }

     

   @Override

   public String toString() {

       // TODO Auto-generated method stub

       return "Name=" + name + " CurrentPosition=" + position;

   }

}

Food.java

package runnable;

public class Food {

  

   public synchronized void eat(int time)

   {

       try {

           System.out.println("I'm eating now.");

           Thread.sleep(time*10);

           System.out.println("I'm done eating.");

       } catch (InterruptedException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

   }

}

MainClass.java

package runnable;

public class MainClass {

  

   public static void main(String args[])

   {

       Food food = new Food();

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

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

      

       Thread turtleThread = new Thread(turtle);

       Thread rabbitThread = new Thread(rabbit);

      

       turtleThread.start();

       rabbitThread.start();

      

   }

}

OUTPUT:

Game ended!

Name=Turtle CurrentPosition=3

Name=Rabbit CurrentPosition=5

I'm eating now.

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=6

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=10

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=9

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=15

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=12

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=20

I'm done eating.

Name=Turtle CurrentPosition=15

I'm eating now.

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=25

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=18

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=30

I'm done eating.

Name=Turtle CurrentPosition=21

I'm eating now.

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=35

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=24

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=40

I'm done eating.

Name=Turtle CurrentPosition=27

I'm eating now.

I'm done eating.

Name=Rabbit CurrentPosition=45

I'm eating now.

I'm done eating.

Name=Turtle CurrentPosition=30

I'm eating now.

I'm done eating.

Name=Rabbit CurrentPosition=50

I'm eating now.

I'm done eating.

Name=Rabbit CurrentPosition=55

I'm eating now.

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=33

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=60

I'm done eating.

Name=Turtle CurrentPosition=36

I'm eating now.

I'm done eating.

Name=Rabbit CurrentPosition=65

I'm eating now.

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=39

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=70

I'm done eating.

Name=Turtle CurrentPosition=42

I'm eating now.

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=75

I'm done eating.

Name=Turtle CurrentPosition=45

I'm eating now.

I'm done eating.

Name=Rabbit CurrentPosition=80

I'm eating now.

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=48

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=85

I'm done eating.

I'm eating now.

Name=Turtle CurrentPosition=51

I'm done eating.

Name=Rabbit CurrentPosition=90

I'm eating now.

I'm done eating.

Name=Turtle CurrentPosition=54

I'm eating now.

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=95

I'm done eating.

Name=Turtle CurrentPosition=57

I'm eating now.

I'm done eating.

I'm eating now.

Name=Rabbit CurrentPosition=100

I'm done eating.

Name=Turtle CurrentPosition=60

I'm eating now.

I'm done eating.

I'm eating now.

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