Question

In: Computer Science

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

Solutions

Expert Solution

import java.io.*;

class ExceptionExample{

public static void main(String args[]){

FileInputStream fis = null;

//FileInputStream throws

//FileNotFoundException which is

// a checked exception.

fis = new FileInputStream("D:/abc.txt");

int i;

//read() method of FileInputStream

//class also throws an IOException.

while(( i = fis.read() ) != -1 ){

System.out.println((char)i);

}

//close() method used for

//close the file input stream

//it throws an IOException.

fis.close();

}

}

import java.io.*;

class ExceptionExample{

public static void main(String args[]){

FileInputStream fis = null;

try{

fis = new FileInputStream("D:/abc.txt");

}

catch(FileNotFoundException fn){

System.out.println("The file is not present at given path");

}

int i;

try{

while(( i = fis.read() ) != -1 ){

System.out.println((char) i);

}

fis.close();

}

catch(IOException ie){

System.out.println("IOException occured: " +ie);

}

}

}


Related Solutions

*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...
C++In Object Oriented Programming, classes represent abstractionsof real things in our programs. We must...
C++In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition. The class must have at least three properties/data fields and three functions/behaviors (constructors and get/set functions don’t count). Do not pick examples from the textbook and do not redefine...
In Object Oriented Programming, classes represent abstractionsof real things in our programs. We must quickly...
In Object Oriented Programming, classes represent abstractions of real things in our programs. We must quickly learn how to define classes and develop skills in identifying appropriate properties and behaviors for our class. To this end, pick an item that you work with daily and turn it into a class definition. The class must have at least three properties/data fields and three functions/behaviors (constructors and get/set functions don’t count). Do not pick examples from the textbook and do not redefine...
This week, you will create and implement an object-oriented programming design for your project. You will...
This week, you will create and implement an object-oriented programming design for your project. You will learn to identify and describe the classes, their attributes and operations, as well as the relations between the classes. Create class diagrams using Visual Studio. Review the How to: Add class diagrams to projects (Links to an external site.) page from Microsoft’s website; it will tell you how to install it. Submit a screen shot from Visual Studio with your explanation into a Word...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the...
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the exception or you can provide a throws clause. To which exceptions does the catch-or-declare requirement apply? Errors All Exceptions except RuntimeExceptions RuntimeExceptions All Throwable except Errors Flag this Question Question 21 pts Assume you have an array called numbers. It's elements are of type int and it was declared like this: int[] numbers = { 3, 6, 9, 12 }; Assume that you also...
Briefly explain the terms used in object-oriented programming with examples.
Briefly explain the terms used in object-oriented programming with examples.
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities....
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities. Group of answer choices A. True B. False 2. __________ programming is centered on the procedures or actions that take place in a program. Group of answer choices A. Class B. Object-oriented C. Menu-driven D. Procedural/ Structural 3. __________ programming encapsulates data and functions in an object. Group of answer choices A. Object-oriented B. Class C. Menu-driven D. Procedural 4. The data in an...
What are design patterns in object oriented programming? Minimum 200 words
What are design patterns in object oriented programming? Minimum 200 words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT