1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lesantik [10]
4 years ago
9

Write a simple command-line calculator with an exception handler that deals with nonnumeric operands. Your program should displa

y a message that informs the user of the wrong operand type before exiting. It should also ask the user to re-input the number to finish the calculation.

Computers and Technology
1 answer:
alexira [117]4 years ago
8 0

Answer:

Here is the JAVA program:

import java.util.InputMismatchException;  // exception that is thrown when input does not match the expected type

import java.util.Scanner;  //used to take input from user

public class Main {

public static void main(String[] args) { //start of main function

   Scanner input = new Scanner(System.in);  // creates Scanner class object

   double num1=0.0;  // double type operand 1

   double num2=0.0;  // double type operand 2

   System.out.println("Please enter 2 operands");  //prompts user to enter two operands

   try {  //defines a chunk of code to be tested for non numeric operand error

      num1 = input.nextDouble();  //reads operand 1 value

      num2 = input.nextDouble(); //reads operand 2 value

         

   } catch (InputMismatchException  ex) {  //code chunk executed when exception occurs in program

     System.out.println("wrong operand type, re-input " + input.next() + " to finish the calculation.");  //displays wrong operand type if user enters non numeric operands

     System.exit(1);  }//exits the program    

   System.out.print("Enter an operator (+, -, *, /): ");  //prompts user to enter an operator

       char operator = input.next().charAt(0);  //reads the input operator

       double result;  // to store the result of the operation

       switch(operator)         {  //decided operation on basis of input operator

           case '+':  // if user enters + operator

               result = num1 + num2;  //adds two operands

               break;

            case '-':  // if user enters - operator

               result = num1 - num2;  //subtracts two operands

               break;  

           case '*':  // if user enters * operator

               result = num1 * num2;  //multiplies two operands

               break;  

           case '/':  // if user enters / operator

               result = num1 / num2;  //divided two operands

               break;  

           default:  //if user enters wrong operator

               System.out.printf("wrong operator!");  //displays wrong operator

               return;          }  

       System.out.printf("%.1f %c %.1f = %.1f", num1, operator, num2, result); }} //displays the result of the operation

Explanation:

The program is well explained in the comments mentioned with each line of program. The user is prompted to enter the values of two operands num1 and num2. The try block is used to test for non numeric operand error and throws InputMismatchException when any of the input operands (num1 or num2) is non numeric. InputMismatchException is thrown by Scanner class. This informs that the token retrieved (which is the num1 or num2 operand) does not match the expected type (which is type numeric). When this error occurs in the program, catch block is used to execute a code chunk, which is the print statement in this program. The print statement informs the user of the wrong operand type before exiting. It also asks the user to re-input the number to finish the calculation and the wrong input type is also mentioned in the output that is to be re-input.

If you want the program to take the input again from user after throwing this exception then you can alter the above program by using two try catch blocks separately for the two operands:

//for operand 1 i.e. num1

System.out.println("Please enter operand 1: ");

   try {

      num1 = input.nextDouble();        

   } catch (InputMismatchException  ex) {

     System.out.println("wrong operand type, re-input " + input.next() + " to finish the calculation.");

      num1 = input.nextDouble();  }  //reads input of operand 1 again from user

//for operand 2 i.e. num2

          System.out.println("Please enter operand 2: ");  

      try {

      num2 = input.nextDouble();  

   } catch (InputMismatchException  ex) {

     System.out.println("wrong operand type, re-input " + input.next() + " to finish the calculation.");

     num2 = input.nextDouble();  } //reads input of operand 1 again from user

The program and its output is attached.

You might be interested in
And there you go <br> sorry its saying my thing is tooo small
zaharov [31]

Answer:

Global knowledge is the answer

6 0
4 years ago
Read 2 more answers
In a PC, which of the following components stores the BIOS
LiRa [457]
I think it’s rom because rom chip is on the PC mother chip
5 0
3 years ago
It is the job of the _____ to convert between 8-bit-wide digital data and 1-bit-wide digital data.
suter [353]
Multiplexer. A mux converts mutiple inputs into fewer outputs, for example combined with an address bus. Then the address bus decides which part of the input is linked to the output, so the inputs use the outputs each at a different point in time (=time division multiplexing)
3 0
4 years ago
Each high-level language has its own __________, or rules of the language. group of answer choices
il63 [147K]

Each high-level language has its own syntax which is also called rules of the programming language. Therefore, the correct choice is syntax.

High-level languages have a greater level of abstraction from details of the computer. They focus more on the programming logic which is closely associated with human understanding instead of the underlying hardware modules. Each high-level programming language has its own specific set of rules which is known as the syntax of that language.

Syntax of the high-level languages define structure and formation of the statements in the language. In order to write a program in any high-level programming language, proper syntax of that programming language requires to be followed.

If the syntax of the programming language is not followed properly, the compiler will produce errors. In result, required output of the program will not be generated because of the failure of the program execution.

You can learn more about syntax at

brainly.com/question/831003

#SPJ4

5 0
2 years ago
Properties of variable in QBASIC​
Arlecino [84]

Explanation:

assume numeric value and is represented by an alphabet or an alphabet followed by another alphabet or digit. ...

String variable : A string variable is represented by an alphabet followed by dollar ()sign.

4 0
3 years ago
Other questions:
  • Due to the shift from host-based networks to microcomputer based networks, more than _____ percent of most organizations' total
    8·1 answer
  • LAN security policies center on issues concerning connectivity; this includes determining how devices adhere to the network. Amo
    14·1 answer
  • Typically, what form do most database designers consider a database structure to be normalized?
    15·1 answer
  • A bowl contains 20 candies; 15 are chocolate and 5 are vanilla. You select 5 at random. What is the probability that all 5 are c
    6·2 answers
  • In some employment situations, not all personnel can immediately evacuate a work area per the eap. it may be that some equipment
    15·1 answer
  • What's ur favorite color and your dream car
    8·2 answers
  • Which ribbon tab is home to the Function library used to insert functions into worksheets?
    15·2 answers
  • Ict quiz I attached a picture
    11·2 answers
  • In the world of computer languages, whats the most popular language?
    13·1 answer
  • Developing algorithms using psuedocode
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!