Question

Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2...

Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2 plugin values 24 and 0, Run 3 plugin values 2e and view the exceptions thrown, if any.

Now modify the ExceptionOriginal class and place a test restricting user from dividing by o --- use if.... else block and display appropriate messages. Run 1 plugin values 12 and 5, Run 2 plugin values 24 and 0, and view the exceptions thrown, if any.

Now modify the ExceptionOriginal class and place a test restricting user from dividing by o (ArithmeticException class should be used to catch this exception) and other exception to be caught is (InputMismatchException)--- using try...catch block and display appropriate messages by the exception classes. Run 1 plugin values 45 and 2, Run 2 plugin values 18 and 0, Run 3 plugin values 2753 and 2f1rl.

public class ExceptionOriginal{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int divident, divisor, quotient;

System.out.print("Enter the divident: ");

divident = scan.nextInt();

System.out.println();

System.out.print("Enter the divisor: ");

divisor = scan.nextInt();

System.out.println();

quotient = divident/divisor;

System.out.print("Quotient = " + quotient);

}//end of main()

}//end of class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have uploaded the Images of the code,Typed code and Output of the Code.I have provided explanation using comments(read them for better understanding).

The given class ExceptionOriginal will raise the Arithmetic Exception and InputMismatch Exceptions.

If we use the if -else block with condition divisor is non Zero value. It may overcome Exception but still may raise InputMismatch Exceptions. So we use try catch block to handle this all Exceptions

Images of the Code:

{ { 1 //Importing required packages 2 import java.util.*;|| 3 public class ExceptionOriginal 4-{ 5 public static void main(St
Note:If the below code is missing indentation please refer code Images

Typed Code:

//Importing required packages
import java.util.*;
public class ExceptionOriginal
{
public static void main(String[] args)
{
//creating a Scanner Object
Scanner scan = new Scanner(System.in);
//variables to store input from user
int divident, divisor, quotient;
  
//try-catch block to Handle Exceptions
//placing the entire section of code which
//may raise Exceptions in try block
try
{
//reading input divident
System.out.print("Enter the divident: ");
divident = scan.nextInt();
System.out.println();
//reading input divisor
System.out.print("Enter the divisor: ");
divisor = scan.nextInt();
System.out.println();
  
//performing division if No Exceptions raised
quotient = divident/divisor;
System.out.print("Quotient = " + quotient);
}
//using ArithmeticException class
catch(ArithmeticException e)
{
//displaying the Exception
System.out.println("\nException:"+e);
}
//using InputMismatchException class
catch(InputMismatchException e)
{
//displaying the Exception
System.out.println("\nException:"+e);
}
}//end of main()
}//end of class
//code ended here

Output:

Enter the divident: 45 Enter the divisor: 2 Quotient = 22

Enter the divident: 18 Enter the divisor: 0 Exception:java.lang.ArithmeticException: / by zero

Enter the divident: 2753 Enter the divisor: 2firl Exception:java.util.InputMismatchException
If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!

Add a comment
Know the answer?
Add Answer to:
Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...

    The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...

  • File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute...

    File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT