Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that has 2 classes. The first class is a test class with 2 methods; a main method, and a static method called

Write a program that has 2 classes. The first class is a test class with 2 methods; a main method, and a static method called drinkMilk(). The drinkMilk method returns void, and will throw an exception of type OutOfMilkException. The purpose of this exercise is to demonstrate that a method can get information back to the caller through an exceptions object that is thrown inside the called method and caught in the calling method. (OutOfMilkException is the second class that you will create.)

The drinkMilk method will: save what time you start drinking your milk, heres how:

In the System class there is a method:

public static long currentTimeMillis()

Returns the current time in milliseconds.

Then, in an infinite loop, while (true) generate random integers between 0 1000 and perform an int division using the random integer as the denominator. Use anything except 0 as the numerator. You must use the Random class to generate random integers, not the Math class. Eventually you will execute a divide by zero.

Each time you generate a random number print out Gulp. Use print instead of println. When a division by zero exception is thrown, the drinkMilk method will catch the ArithmeticException exception. Within the catch block throw an exception of type OutOfMilkException.

(You are catching one kind of exception, creating a more descriptive exception that describes the situation, and throwing that exception.)

The outOfMilkException object will contain a value that indicates how long it took to drink the milk in milliseconds, i.e., how long it took to generate a 0 value. The main method that catches the OutOfMilkException will printout the number of milliseconds that it took to drink the milk.

This demonstrates that information is "thrown" from the drinkMilk method to the main method. Keep in mind that the purpose of this exercise is to throw a piece of information from one method to another without using the usual mechanisms for passing information from one method to another, i.e., parameters and return values.

Note: It is not good practice to put any calculation inside an Exception object. These objects are holders of data only! No arithmetic operators, no method calling. Maybe a String concat + once in a while. The methods in the exceptions should never contribute to the solution; their role is exclusively to know about what went wrong, and to be thrown and caught.

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

More Books

Students also viewed these Databases questions

Question

Are robots going to displace all workers? Explain your answer.

Answered: 1 week ago