Questions
*OBJECT ORIENTED PROGRAMMING* GOAL: will be able to throw and catch exceptions and create multi-threaded programs....

*OBJECT ORIENTED PROGRAMMING*

GOAL: will be able to throw and catch exceptions and create multi-threaded programs.

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

In: Computer Science

*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create...

*OBJECT ORIENTED PROGRAMMING*

JAVA PROGRAMMING

GOAL: will be able to throw and catch exceptions and create multi-threaded programs.

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

In: Computer Science

1. Read a line of input from the user (as a string) and find out if...

1. Read a line of input from the user (as a string) and find out if there are any vowels in the string. Use a break or continue keyword (whichever is appropriate) to notify that a vowel has been found. Prompt for another string and search again until the user enters 'exit' into the program.

2. Ask the user how many random numbers they would like to see. Ask the user to provide the lowest number they would like to use and the highest number. Only return integers between those two numbers.

Java language

In: Computer Science

The minimum energy required to remove the electron from a particular excited state of Li2+ is...

The minimum energy required to remove the electron from a particular excited state of Li2+ is 327.9 kJ/mol.

a. What was the value of n for the excited state orbital that the electron came from?

b. For the energy level you determined in part a, what is the total orbital degeneracy, and what is the highest possible value of the angular momentum quantum number for this value of n?

In: Chemistry

The number of customers arriving per hour at a certain automobile service facility is assumed to...

The number of customers arriving per hour at a certain automobile service facility is assumed to follow a Poisson distribution with mean λ=9. ​(a) Compute the probability that more than 16 customers will arrive in a 2-hour period.​ (b) What is the mean number of arrivals during a 2-hour period?

​(a) The probability that more than 16 customers will arrive is

​(Round to four decimal places as​ needed.)

​(b) The mean number of arrivals is

​(Type an integer or a decimal. Do not​ round.)

In: Statistics and Probability

equation for part A ?(?) = −0.48?^2 + 7.2? + 63 equation for part B ?(?)...

equation for part A ?(?) = −0.48?^2 + 7.2? + 63

equation for part B ?(?) = −?^2 + 8? + 84

the Georgia marching band discovers that the amount of time it spends playing “Glory, Glory to Old Georgia” has a direct impact on the number of points Georgia’s team scores. If the band plays for x minutes, then the Bulldogs will score ?(?) = −0.48?^2 + 7.2? + 63. points in the game. Assume the band can play for a maximum of 10 minutes

. a. How long should the band play to maximize the number of points Georgia scores? Show your work and explain.

b. The band affects how many points Tennessee scores as well. When the UGA band plays for x minutes the Volunteers score ?(?) = −?^2 + 8? + 84. points in the game. Find the number of minutes the band should play to maximize the margin of victory for Georgia (i.e., the points by which Georgia wins). Again, please show all work. [Hint: You should use both V (x) and B(x).]

c. What will be the score of the game you found in part (b)?

In: Math

equation for part A ?(?) = −0.48?^2 + 7.2? + 63 equation for part B ?(?)...

equation for part A ?(?) = −0.48?^2 + 7.2? + 63

equation for part B ?(?) = −?^2 + 8? + 84

the Georgia marching band discovers that the amount of time it spends playing “Glory, Glory to Old Georgia” has a direct impact on the number of points Georgia’s team scores. If the band plays for x minutes, then the Bulldogs will score ?(?) = −0.48?+ + 7.2? + 63 points in the game. Assume the band can play for a maximum of 10 minutes.

a. How long should the band play to maximize the number of points Georgia scores? Show your work and explain.

b. The band affects how many points Tennessee scores as well. When the UGA band plays for x minutes the Volunteers score ?(?) = −?+ + 8? + 84 points in the game. Find the number of minutes the band should play to maximize the margin of victory for Georgia (i.e., the points by which Georgia wins). Again, please show all work. [Hint: You should use both V (x) and B(x).]

c. What will be the score of the game you found in part (b)?

In: Math

An Internet survey estimates that, when given a choice between English and French,60% of the population prefers to study English. Three students are randomly selected and asked which of the two languages they prefer.

An Internet survey estimates that, when given a choice between English and French,60% of the population prefers to study English. Three students are randomly selected and asked which of the two languages they prefer.

(a) Find the probability distribution for Y , the number of students in the sample who prefer English.

(b) What is the probability that exactly one of the three students  prefer English?

(c) What are the mean and standard deviation for Y ?

(d) What is the probability that the number of students prefer English falls within 2 standard deviations of the mean?

 

In: Statistics and Probability

A basketball player is practicing his free throws. This player's probability of making a free throw...

A basketball player is practicing his free throws. This player's probability of making a free throw over his career is 0.592. He will shoot 140 free throws.

a) Define a random variable, and write out the probability mass function for the number of free throws this player makes on his 140 attempts.

b) What is the probability that this player makes between 60 and 62 free throws, inclusive?

c) What is the expected value and variance of the number of free throws this player will make during his practice session?

In: Statistics and Probability

(9) Shaquille O’Neal is practicing his free throws in the gym. Shaq’s probability of making a...

(9) Shaquille O’Neal is practicing his free throws in the gym. Shaq’s probability of making a free throw over his career is 0.527. He will shoot 150 free throws. a) Define a random variable, and write out the probability mass function for the number of free throws Shaq makes on his 150 attempts. b) What is the probability that Shaq makes between 78 and 80 free throws, inclusive? c) What is the expected value and variance of the number of free throws Shaq will make during his practice

In: Statistics and Probability