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

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

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • QUESTION 21 What exception will the following program raise? class Main f public static void main(Stringl...

    QUESTION 21 What exception will the following program raise? class Main f public static void main(Stringl args) try System.out.print Hello"1/0); catch ( e) f codes are not provided** DataFormatException InputMismatchException ArithmeticException DividedByZeroException QUESTION 19 Suppose we had the following method header: public void someMethod (int someNumber) Which of the following is an overloaded version of this method? public void someMethod (int someNumber, int anotherNumber) O public int someMethod (int anotherNumber) O public void someMethod (int anotherNumber) public void anotherMethod (int...

  • This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions...

    This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program. Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class. The regular expression you will need for a Phone number is: "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$" and for an SSN: "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$" This exercise is meant to be done with the video listed in the...

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