Copy, run, and study the TestExceptions Java application Which is below. Modify
ID: 3773100 • Letter: C
Question
Copy, run, and study the TestExceptions Java application Which is below. Modify the program so that it gets its inputs from the user via the standard input stream rather than the command-line. In addition, a new user-defined Exception class is added to the program.
Write a private static method named menu() that returns a String object. The method must declare that it throws the GoofyUserException.
The menu() method prints the following to the standard output stream.
Throw a GoofyUserException object if the user enters an invalid choice. The main()method needs to be modified to catch the GoofyUserException. Print the following in the catch block for GoofyUserException.
Return following String objects depending on user input.
Exit the program using System.exit(0) if menu() returns null (i.e. the user enters an 8).
If choice 2 entered, prompt the user to enter either a number or a word. Append this input to the return String. Note: There must be a space between the "-d" and the input that is appended.
The modified program should not have any code related to theNeedsHelpException.
Testing the Program
The modified program can be tested by comparing its output with the output produced when the TestExceptions program is executed.
Explanation / Answer
import java.io.*;
import java.io.InputStream;
import java.io.inputstreamReader;
{
try{
System.out.println(“the different choices available are:”);
String result= menu(choice);
}
catch(GoofyUserException g)
{
System.out.println(“Goofy User! Learn to follow instructions”);
}
try
{
if( result ==”-d 0”)
{
divide(0);
}
else if(result == (“-d”+a))
{
System.out.println(“-d ”+a);
}
else if(result == “-p”)
else if(result ==”-r”)
else if(result == “-t”)
{
System.out.println(“enter a string”);
}
}
}
{
}
{
}
{
}
{
}
}
Private static String menu( int choice) throws GoofyUserException
{
System.out.println(“select one of the following choices”);
System.out.println(“1) force FloatingPointDivideByZeroException”);
switch(choice)
{
case 1: return “-d 0”;
break;
case 2: System.out.println(“enter a number or word”);
String a= in.readLine();
return (“-d”+a);
break;
case 3: return “-p”;
break;
case 4: return “-r”;
break;
case 5: return “-t r”;
break;
case 6: return “-t e”;
break;
case 7: return “-t E”;
break;
case 8: System.exit( );
break;
}
}
}