Question

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

Write a simple command-line calculator with an exception handler that deals with nonnumeric operands. Your program should display 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. PLEASE WRITE IN JAVA.

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

Java Program:

import java.util.*;

class Calculator_CL
{
   //Main method
   public static void main(String[] args)
   {
       //Creating a scanner class object
       Scanner reader = new Scanner(System.in);
      
       //Displaying operations available
       System.out.println("\n The calculator supports the following operations: \n + \n - \n * \n / \n\n ");
      
       //Reading operation from user
       System.out.print("\n Select an operation: ");
       String operator = reader.nextLine();
      
       double oper1=0.0, oper2=0.0, result=0.0;
      
       //Reading operands
       if(operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/") || operator.equalsIgnoreCase("POW"))
       {
           boolean loop = true;
           while(loop)
           {
               try
               {
                   //Reading operand 1
                   System.out.print("\n Enter operand 1: ");
                   oper1 = reader.nextDouble();
                   loop = false;
               }
               catch(Exception ex)
               {
                   System.out.println("\n Error!!! Invalid Operand.. Re-Enter... \n");
                   reader.nextLine();
               }
           }
          
           loop = true;
           while(loop)
           {
               try
               {
                   reader.nextLine();
                   //Reading operand 2
                   System.out.print("\n Enter operand 2: ");
                   oper2 = reader.nextDouble();
                   loop = false;
               }
               catch(Exception ex)
               {
                   System.out.println("\n Error!!! Invalid Operand.. Re-Enter... \n");
               }
           }
          
           //Addition operation
           if(operator.equals("+"))
           {
               //Addition operation
               result = oper1 + oper2;
           }
          
           //Subtraction operation
           else if(operator.equals("-"))
           {
               //Addition operation
               result = oper1 - oper2;
           }
          
           //Multiplication operation
           else if(operator.equals("*"))
           {
               //Multiplication operation
               result = oper1 * oper2;
           }
          
           //Division operation
           else if(operator.equals("/"))
           {
               //Division operation
               result = oper1 / oper2;
           }
          
           //Printing result
           System.out.printf("\n\n %f %s %f = %f \n\n", oper1, operator, oper2, result);
       }
       else
       {
           System.out.println("\n Invalid operation!!! \n");
       }
   }
}

__________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Write a simple command-line calculator with an exception handler that deals with nonnumeric operands. Your program...
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
  • (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands...

    (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands and perform the corresponding calculation on the operands. Specific requirements: (1) Your main function accepts inputs and display calculation result. (2) Write four functions to perform the four calculations. Call these functions in your main function. (3) Repeatedly asks for inputs, calculate and display result until user input nonnumeric value for operands. Hint: you can put getchar() right after scanf() to get rid of...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • Write a C program that implements a simple shell. The shell takes a user command as...

    Write a C program that implements a simple shell. The shell takes a user command as input and execute the command. When a shell is started, it should take user command, execute it and display the output.

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

  • A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction,...

    A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction, multiplication and division) of two numbers, using your understanding of Java. Description: You need to write a program that will display a menu when it is run. The menu gives five choices of operation: addition, subtraction, multiplication, division, and a last choice to exit the program. It then prompts the user to make a choice of the calculation they want to do. Once the...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

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