java
JAVA Activity - Handling Exceptions Completion Criteria · Application.java contains correct exception reporting, compiles, and is able to run. · All unit tests pass in ApplicationTest.java and CustomEmptyStringExceptionTest.java. · There is an outer try/catch around block of code in the main() method and an additional nested try/catch around the while loop block. · When the user response is a non-numeric string, the exception is caught and the appropriate error message is displayed. · When the user response is a number that is out of range, the exception is caught and a different error message is displayed. · The program loop executes until a numeric response is entered or an invalid numeric response causes the program to exit gracefully. · The response to the reflection question is entered in the Comment in the submission window Topics Covered: · Exception handling · try/catch blocks · throw keyword Learning Outcomes: · You will be able to catch and handle exceptions in your code. · You will be able to throw exceptions in your code intentionally. · You will be able to use exceptions to communicate with other programmers. · You will be able to implement code that throws an exception when input is invalid. · You will be able to write code that is organized, easily understood, and free of errors. Open activity and clone it to your computer. You will be working with a command-line interface (CLI) that isn't working because of unhandled Exceptions. For this activity, you will need to make the required corrections to report and handle exceptions for the CLI to run end-to-end successfully. Your job is to do quality assurance testing (QA) by entering wrong data to see if the program will handle it and then write code to process input and check validity of input, catching exceptions so that the program exits gracefully. You want the program to have this behavior: 1. If a non-numeric input is entered at any point, print out an appropriate error message, then loop around to ask the user to try again. 2. If a number outside of 1-3 is entered, print out the appropriate error message and the program will exit gracefully. 3. If option 1 is entered, print out a lucky number and exit. 4. If option 2 is entered, print out a random fortune and exit. 5. If option 3 is entered, print out a goodbye message and exit. The program already contains the logic for steps 3 through 5. Your job will be to process and validate input, throwing the correct exceptions if input is invalid. Overview This UML class diagram shows the required classes for the solution. Most of Application.java is already coded. You will only be adding code to the methods marked with the ( * ) to set up error handling. Step One - Run the program After downloading the activity, open the project and try to run the main() method in the Application class. You will see a list of checked exceptions. Resolve the compiler issues by identifying and reporting the exceptions. Do this before attempting to run the tests in ApplicationTest.java. Once you have resolved the compiler issues, you may attempt to run Application.java Step Two - add a try/catch in main to include all of the statements. When first creating the try/catch block in main, just add the generic Exception class to the catch block and print out the exception using `System.out.println(). You will be adding more onto the main try/catch after you've identified what exceptions need to be caught. Step Three - Test invalid character input Run the Application and enter an invalid String with characters when prompted. This tests the convertStringInput() method. There is no code to add to this method. This method automatically throws an Exception when a non-numeric String is entered. You need to determine what type of error that is so you can catch it and handle it correctly in Step Four. To determine what type of Exception is thrown in convertStringInput() you have several options. 1. Print out the Exception object in the catch block in main(). You can pass an Exception object to System.out.println(). 2. Use the debugger to view the type of Exception thrown Step Four - wrap the code inside the while loop in a nested try/catch block Write a try/catch block just inside the while loop. Inside the try block, place the code that existed before in the while loop. The exception type for the catch block will the same as the one you found for convertStringInput(). Inside the catch block, print out the user-friendly message: "Non-numeric string was entered. Please try again." Step Five - Fill in isInputValid() This method will be coded to check for a valid number input. If a number less than 1 or greater than 3 was entered, we want to throw a standard exception, IllegalArgumentException with the custom message "Number must be between 1 and 3". This will be caught in the main() try/catch so the program exits gracefully. If the number is valid, return true. Step Six - Create a Custom Exception class and use it in your code · First complete the CustomEmptyStringException class definition that extends the RuntimeException class. Do this by creating a constructor that takes in a String argument for the exception message. · Fill out the checkForEmptyString() message in order to check for an empty string and throw the custom exception with the message: "Invalid input: Empty string entered." · Modify the nested try/catch block in Step Four to add an additional catch block for the CustomEmptyStringException and print the message from the exception. After the exception message, also print: "Please try again." · After an empty string is entered, the application should continue to loop and ask for input. Testing your code Execute the tests in ApplicationTest.java and CustomEmptyStringExceptionTest.java to ensure that your methods work correctly. Once those tests pass and you have completed the steps above in the correct order, you will be able to test the program as it is shown below. The program will be tested based on expected output. The program should run without crashing and follow this example: Incorrect user input Non-numeric string entered: Empty string entered: Invalid number entered: (exits gracefully, does not loop) Correct user input Output option 1: Output option 2: (fortune will vary) Output option 3: IDE (IntelliJ) Application.Java package com.kenzie.app; import java.io.*; import java.util.Scanner; /* * Java Program picks a random lucky number or a fortune, but only if exceptions are handled */ public class Application { public static final String FORTUNE_FILENAME = "fortunes.txt"; public static void main(String args[]) { //TODO: Wrap the main code in a try/catch statement //DO NOT MODIFY THIS BLOCK Scanner scan = new Scanner(System.in); String input; int numberInput = 0; boolean needInput = true; //END BLOCK //Read in user input - loop until user enters correct input while (needInput) { //TODO: wrap the code inside the while event loop with a try/catch block displayMenu(); input = scan.nextLine(); checkForEmptyString(input); numberInput = convertStringInput(input); if (numberInput > 0) { needInput = false; } } //DO NOT MODIFY THIS BLOCK //Check user input for valid number value isInputValid(numberInput); //Selects and displays fortune based on number found displayResult(numberInput); //END BLOCK } // TODO: throw correct exceptions in method signature private static void displayResult(int number) { // display result switch (number) { case 1: System.out.print("Your lucky number is: "); System.out.println(pickRandomNumber()); break; case 2: System.out.print("Your lucky fortune is: "); System.out.println(pickFortune(FORTUNE_FILENAME)); break; case 3: System.out.println("Goodbye. Try your luck another time!"); break; default: System.out.println("Input not recognized."); break; } } // this method throws an automatic exception public static int convertStringInput(String input){ //TODO: Do not change anything in this method. // This code automatically throws an exception if a non-number value is converted. // Run the application and enter a non-numeric string to find out what exception to catch in main(). // return Integer.parseInt(input); } // this method will be coded to throw a standard exception public static boolean isInputValid(int cleanNumber){ // TODO: fill in this method; throw IllegalArgumentException if the number is invalid // otherwise return true return false; //replace when writing method } // Use this method to throw a custom Exception of type CustomWhiteSpaceException // With message: "Invalid input: Empty string entered." public static void checkForEmptyString(String input){ // TODO: fill in this method; throw CustomEmptyStringException if the input is empty string } public static void displayMenu() { System.out.println(); System.out.println("*******Hello Fortune Hunter!*******"); System.out.println(); System.out.println("Enter a number between 1-3:"); System.out.println("1. Generate your lucky number"); System.out.println("2. Get your fortune"); System.out.println("3. Exit program"); System.out.println(); System.out.print(">"); } public static int pickRandomNumber() { int min = 1; int max = 10; // generate random int value from 50 to 100 int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); return random_int; } public static String pickFortune(String filename) throws FileNotFoundException, IOException { int random_int = pickRandomNumber(); // open the file FileInputStream fstream = new FileInputStream(filename); BufferedReader breader = new BufferedReader(new InputStreamReader(fstream)); String strLine = ""; // read File Line By Line for (int i = 1; i < random_int; i++) { strline = breader.readline(); } // close the input stream fstream.close(); return strline; } } customemptystringexception package com.kenzie.app; // todo: fill in the class to define customemptystringexception which extends runtimeexception public class customemptystringexception extends runtimeexception { } applicationtest package com.kenzie.app; import random_int;="" i++)="" {="" strline="breader.readLine();" }="" close="" the="" input="" stream="" fstream.close();="" return="" strline;="" }="" }="" customemptystringexception="" package="" com.kenzie.app;="" todo:="" fill="" in="" the="" class="" to="" define="" customemptystringexception="" which="" extends="" runtimeexception="" public="" class="" customemptystringexception="" extends="" runtimeexception="" {="" }="" applicationtest="" package="" com.kenzie.app;=""> random_int; i++) { strline = breader.readline(); } // close the input stream fstream.close(); return strline; } } customemptystringexception package com.kenzie.app; // todo: fill in the class to define customemptystringexception which extends runtimeexception public class customemptystringexception extends runtimeexception { } applicationtest package com.kenzie.app; import>