Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please i need some help with my programing assigment in Java Part 1: Write a pro

ID: 3793901 • Letter: P

Question

Please i need some help with my programing assigment in Java

Part 1: 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 drinkMilko. The drinkMilk method returns void, and ill 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 drink Milk method will: save what time you start drinking your milk, here's how In the System class there is a method public static long current l'imeMi 11 is Returns the current time in milliseconds Then, in an infinite loop, wwhile (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 printin. When a division by zcro cxccption is thrown, thc drinkMilk method will catch the ArithmeticException exception. Within the catch block throw an exception of type outof MilkException. (You are catching one kind of exception, creating a more descriptive exception that describes the situation, and throwing that exception.) Thc outOfMilkExccption objcct will contain a value that indicatcs how long it took to drink the milk in milliseconds, 1. e., how long it took to generate a 0 value. The main method that catches the xception will printout the number of seconds 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 oncc in a whilc. Thc mcthods in thc cxccptions should ncver 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

*/