Question
the program is written however there is missing parts(written in bold) that should be completed... public class ExceptionWithThrow { public static Scanner openFile(String fileName) throws
the program is written however there is missing parts(written in bold) that should be completed...
public class ExceptionWithThrow {
public static Scanner openFile(String fileName) throws FileNotFoundException{ FileReader fr = new FileReader (fileName); Scanner sc = new Scanner (fr); return sc;
} public static boolean isValidIdentifier(String name){
// check here if the name is a valid identifier name
}
public static String readValidIdentifierName() throws InputMismatchException{ Scanner input = new Scanner (System.in); String name = input.next(); if ( ! isValidIdentifier() )
throw new InputMismatchException();
return name; }
public static int readInteger() throws InputMismatchException { Scanner input = new Scanner (System.in); int num = input.nextInt(); return num;
}
public static boolean isDivisible(int x, int y) throws ArithmeticException { if (y == 0)
throw new ArithmeticException(); if (x % y == 0)
return true; else
return false;
}
public static void main(String[] args) {
// Add try and catch blocks around appropriate statements // Two exceptions are expected hereSystem.out.println("Please enter the input file name:"); String fName = readValidIdentifierName();
Scanner fsc = openFile(fName);
// One exception is expected here
System.out.println("Please give me an integer number"); int firstNum = readInteger(); System.out.println("Please give me another integer number"); int secondNum = readInteger();
// One exception is expected here
if (isDivisible(firstNum, secondNum)){ System.out.println(firstNum + " is divisible by " + secondNum);
} else{
System.out.println(firstNum + " is NOT divisible by " + secondNum); }
} }
Problem 2 (throws and throw) Complete the program below in order to make run properly by adding try-catch blocks in the main method public class Exceptionwi thThrow public static Scanner openFile (String fileNae throws FileNotFoundExceptioni FileRe ader frnew FileReader (fileName) ; Scanner sc-new Scanner (fr) return sc: public static boolean isValidIdentifier (String name) // check here if the name is a valid identifier name public static String readValidIdentifierName( throws InputMi smatchException Scanner input- new Scanner (System.in) String name- input .next } ; if isValidIdentifier) throw new InputMismatchException ) return name: public static int readInteger throws InputMismatchException Scanner input-ne Scanner (System.in) int numinput.nextinto return num public static boolean isDivisible (int x, int y) throws ArithmeticException if (y-0) if (x % y0) else throw new ArithmeticException) return true return false public static void main (String) args) // Add try and catch blocks around appropriate statements /I Two exceptions are expected here System.out println("Please enter the input file name:") String Name-readValidI dentifierName O Scanne r fsc- openFile(fName); // One exception is expected here System.out println("Please give me an integer number") int firstNum-readInteger System.out println("Please give me another integer number" int secondNum- readInteger ) // One exception is expected here if sDivi sible (firstNun, se condNum } }{ System.out.printin (first Numis divisible bysecondNum) else System.out.printin (firstNum "is NOT diviile by"secondNum)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started