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

Create a new Java project. This does not need to be a web application, but it ca

ID: 3691765 • Letter: C

Question

Create a new Java project. This does not need to be a web application, but it can be.

Create a class with one instance method that accepts two ints as parameters and does something simple with them, such as adding them together a returning the result.

Create two new exception classes: one checked, and one unchecked. Call the checked exception something innocent like "RandomInterruptionException" and call the unchecked exception something significant like "RandomCatastrophicException".

Using Math.random() modify your instance method as follows:

If either int is less than zero or greater than 1000 the method should generate a SEVERE log message and then throw an IllegalArgumentException (this exception is built in to Java, don't create it).

Generate a random number...

If the random number is between 0.0 and 0.1 then generate a WARNING log message and throw a RandomCatastrophicException.

If it's between 0.1 and 0.5 then throw a RandomInterruptionException.

Otherwise, just allow the method to resolve normally.

Create a main method (or Servlet/JSP if doing a web app) which allows the user to enter two integers for your new class's instance method. Do everything you can to prevent IllegalArgumentExceptions. Catch the RandomInterruptionException when it happens, generate a FINE log message, and retry the user's two numbers without letting the user know. Do not catch RandomCatastrophicException (allow it to crash your program/page), but make sure the message it displays is useful to the user.

Explanation / Answer

import java.io.*; import java.util.logging.Logger; class Test { private static final Logger LOGGER = Logger.getLogger(Test.class.getName()); public static void main(String[] args) throws IOException, RandomCatastrophicException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter 2 ints"); int a = Integer.parseInt(br.readLine()); int b = Integer.parseInt(br.readLine()); try { System.out.println(method1(a, b)); }catch (RandomInterruptionException r){ LOGGER.fine("FINE"); System.out.println("Enter 2 ints"); a = Integer.parseInt(br.readLine()); b = Integer.parseInt(br.readLine()); method1(a,b); } } public static int method1(int a, int b) throws RandomCatastrophicException { int r = (int) Math.random(); if(r1000){ LOGGER.severe("SEVERE"); throw new IllegalArgumentException(); } double d = Math.random(); if(d>=0 && d=0 && d
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote