Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi guys, I need help with my code. My java code saying that I have an error. I don't see what I'm doing wrong. Please

Hi guys, I need help with my code. My java code saying that I have an error. I don't see what I'm doing wrong. Please help. The error start in part 2, but I want someone to check my program for part 1.

Here my assignment:

Concepts

Interfaces

Synchronization

wait

notify

notifyAll

Part I

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

Adjust them so that the rabbit wins sometimes, and the turtle wins sometimes.

In the main method start both the rabbit and the turtle and see which one wins the races.

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.

Part II

The objective here is to demonstrate the behavior of threads that share data, and use synchronized methods. You do not NOT use wait/ notify/ notifyAll in this exercise.

When the above race 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 I.

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

*Note that this program will have in some cases exception handling. Make sure that all exceptions are handled according to standard programming practices.

Here my Part 1 code:

import java.util.concurrent.ThreadLocalRandom;

public class Animal implements Runnable{

private String names;

private int position;

private int speed;

private int rest;

private static boolean winner = false;

public String getNames() {

return names;

}

public void setNames(String names) {

this.names = names;

}

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 int getRest() {

return rest;

}

public void setRest(int rest) {

this.rest = rest;

}

public static void main(String[] args) {

Animal rabbit = new Animal();

rabbit.setNames("Rabbits");

rabbit.setPosition(0);

rabbit.setRest(150);

rabbit.setSpeed(5);

Animal turtle = new Animal();

turtle.setNames("Turtle");

turtle.setPosition(0);

turtle.setRest(100);

turtle.setSpeed(3);

Thread rabbitT = new Thread(rabbit);

Thread turtleT = new Thread(turtle);

rabbitT.start();

turtleT.start();

}

public void run() {

while(position <= 100 && !winner){

position += speed;

int rand = ThreadLocalRandom.current().nextInt(0, getRest());

System.out.println("Running: " + getNames() + "\tPosition: " + getPosition());

if(position >= 100){

winner = true;

System.out.println("Winner: " + getNames());

}

}

}

}

Here my Part 2: I recreated everything for part 2.

import java.util.concurrent.ThreadLocalRandom;;

public class Animal implements Runnable {

private Food food;

private String name;

private int position;

private int speed;

private int rest;

private static boolean winner = false;

public Animal(Food food) {

this.food = food;

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

public int getRest() {

return rest;

}

public void setRest(int rest) {

this.rest = rest;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getPosition() {

return position;

}

public void setPosition(int position) {

this.position = position;

}

public static void main(String[] args) {

Food food = new Food();

Animal rabbit = new Animal(food);

rabbit.setName("Rabbit");

rabbit.setPosition(0);

rabbit.setRest(150);

rabbit.setSpeed(5);

Animal turtle = new Animal(food);

turtle.setName("Turtle");

turtle.setPosition(0);

turtle.setRest(100);

turtle.setSpeed(3);

Thread rabbitT = new Thread(rabbit);

Thread turtleT = new Thread(turtle);

rabbitT.start();

turtleT.start();

//error start here.

public void run() {

while (position <= 100 && !winner) {

position += speed;

System.out.println(getName() + "Started eating");

food.eat(getRest());

System.out.println(getName() + "Stopped eating");

if(position >= 100) {

winner = true;

}

}

}

}

}

food program:

public class Food {

synchronized void eat(long sleep) {

try {

Thread.sleep(sleep);

}

catch (InterruptedException r) {

r.printStackTrace();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

What is BPR? What does it have to do with systems analysis?

Answered: 1 week ago

Question

Describe a persuasive message.

Answered: 1 week ago

Question

Identify and use the five steps for conducting research.

Answered: 1 week ago

Question

List the goals of a persuasive message.

Answered: 1 week ago