Question

The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber second
D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMis
public static void main (String args) Scanner scannernew Scanner (System.in); boolean continueLoop = true; // determines if m


Sample Output 1 Please enter first integer number: 20 Please enter second integer number: Exceptlon: Java.util. InputMismatch
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.InputMismatchException;
import java.util.Scanner;

public class Calculator {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        int fNum = 0;
        int sNum = 0;
        int result = 0;
        char operator = '#';

        try {
            System.out.print("Please enter first integer number: ");
            fNum = scanner.nextInt();
            System.out.print("Please enter second integer number: ");
            sNum = scanner.nextInt();
            scanner.nextLine();
            System.out.print("Please enter an operation (*, -, +, / ): ");
            operator = scanner.nextLine().charAt(0);

            switch (operator) {

                case '+':
                    result = addition(fNum, sNum);
                    break;
                case '-':
                    result = subtraction(fNum, sNum);
                    break;
                case '/':
                    try {
                        result = quotient(fNum, sNum);
                    } catch (ArithmeticException ae) {
                        System.out.println("Divide by zero is an invalid operation");
                        System.exit(1);
                    }
                    break;
                case '*':
                    result = multiplication(fNum, sNum);
                    break;
                default:
                    System.out.println("Invalid operator");
                    break;
            }


        } catch (InputMismatchException ime) {
            System.out.println("Invalid integer entered");
            System.exit(1);
        }

        if (operator == '+' || operator == '-' || operator == '*' || operator == '/') {
            System.out.println("Result: " + fNum + " " + operator + " "+sNum + " = " + result);
        }
    }

    static int addition(int firstNumber, int secondNumber) {
        return firstNumber + secondNumber;
    }

    static int subtraction(int firstNumber, int secondNumber) {
        return firstNumber - secondNumber;
    }

    static int quotient(int firstNumber, int secondNumber) {
        return firstNumber / secondNumber;
    }

    static int multiplication(int firstNumber, int secondNumber) {
        return firstNumber * secondNumber;
    }


}
Add a comment
Know the answer?
Add Answer to:
The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...
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
  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • 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...

  • In java need help with the TODO sections creating recursive methods public class CountUpDown { /**...

    In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...

  • How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting...

    How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting until the user enters a valid number. I know that I would need to try n = kbd.nextInt(); and catch a InputMismatchException and if n<1 or n>100 then I need to print Out of Range Exception. Must be in 1..100. However, I am unsure how to loop all of that. import java.io.*; import java.util.*; public class Lab5 { public static void main( String args[]...

  • LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a...

    LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass() Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class. Transfer the class variables from LE 6.2 to Family. Strip the word static from the class variables and the method headers in the Family...

  • I have the following java-program, that creates a random walk starting at (0,0) and continuing until...

    I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...

  • 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...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

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