Write a program that has 2 classes. The first class is a test class with 2 metho
ID: 3796312 • Letter: W
Question
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, here’s 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.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Random;
public class DrinkMilkDemo {
public static void drinkMilk() throws OutOfMilkException{
long start = System.currentTimeMillis();
try{
int num= 123;
Random random = new Random();
while(true){
int denom = random.nextInt(1000);
System.out.print("Gulp");
int div = num/denom;
}
}catch(ArithmeticException ex){
System.out.println();
long end = System.currentTimeMillis();
throw new OutOfMilkException(end-start);
}
}
public static void main(String[] args) {
try{
drinkMilk();
}catch(OutOfMilkException ex){
System.out.println(ex.getMessage());
}
}
}
/*
* OutOfMilkException class
*/
class OutOfMilkException extends Exception{
private long time;
public OutOfMilkException(long time) {
this.time =time;
}
public String getMessage(){
return "It took "+time+" milliseconds to drink milk";
}
}
/*
Sample run:
GulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulpGulp
It took 4 milliseconds to drink milk
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.