Question

In: Computer Science

Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...

Using Java

Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator:

  • The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated).
  • On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and store the result back in the accumulator
  • The user can also enter ‘R’ to reset the accumulator to 0, or the letter “P” to turn the calculator’s power off

The calculator will need to trap the following exception conditions:

  • If the user enters something other than +, -, *, /, R, or P for the operator, an UnknownOperatorException exception should be thrown.
  • If the user enters something other than a valid number for the operand, a NumberFormatException exception should be thrown. (You can ignore this exception if the user enters R or P for the operator.)
  • If the user tries to divide by zero, an DivideByZeroException exception should be thrown.

The NumberFormatException is already provided in Java. You will write your own UnknownOperatorException and DivideByZeroException classes.

All exception classes should have at least two constructors:

  • one which takes no arguments and provides a default Exception message
  • another which takes a String as an argument and sets the contents of the String as the Exception message

Provide listings for your Calculator class (or classes) and your exception class. Also, provide a PrintScreen(s) demonstrating each of the six operators and three exceptions in use.

Here’s a sample dialog that a user might have with the calculator:

  Your calculator is now on.
  The result is currently 0.0

  Enter an operator (+, -, *, or /) and a number, 
     "R" to reset, or "P" to turn off power
  + 5
  The result of 0.0 + 5.0  is 5.0
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  / 2
  The result of 5.0 / 2.0 is 2.5
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  / 0
  You cannot divide by 0.  The result is still 2.5.
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power  
  R
  The calculator has been reset to 0.
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  – 18.4
  The result of 0.0 - 18.4 is -18.4
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  * 20
  The result of -18.4 * 20.0 is -368.0
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  + abc
  "abc" is not a valid number.  The result is still -360.0 

  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power
  & 15
  "&" is not a valid operator.  The result is still -368.0  
 
  Enter an operator (+, -, *, or /) and a number,
     "R" to reset, or "P" to turn off power  
  P
  Good bye.

Solutions

Expert Solution

CODE:

import java.util.Scanner;

public class Calculator {

    public static void main(String args[])
    {
        double acc=0.0,operand;
        String operator;
        Scanner scan = new Scanner(System.in);
        do
        {
            System.out.print("Enter Your operator : ");
            operator = scan.nextLine();
        
            if(operator.length()>1){
               try{
                   throw new UnknownOperatorException("Unknown Operator");
               }
               catch(UnknownOperatorException exp){
                   System.out.print(" UnknownOperatorException exeption handled try entering a valid operator.\n");    
                   continue;
               }
            }
            char op = operator.charAt(0);
            if(op=='P'){
               System.out.printf("Accumaltor %.1f", acc);
               }
            else if(op=='R') {
               acc=0.0;
               System.out.printf("Accumulator :%.1f\n",acc);
               }
            else if(op=='+'){
               operand = Readnumber(scan);
               acc = acc + operand;
               System.out.printf("Accumulator :%.1f\n",acc);
            }
            else if(op=='-'){
               operand = Readnumber(scan);
               acc = acc - operand;
               System.out.printf("Accumulator :%.1f\n",acc);
            
            }
            else if(op=='*'){
               operand = Readnumber(scan);
               acc = acc * operand;
                 System.out.printf("Accumulator :%.1f\n",acc);
       
            }
           else if(op=='/'){
               operand = Readnumber(scan);
               while(operand==0){
                   try{
                   throw new DivideByZeroException("Unknown Operator");}
                   catch(DivideByZeroException e) {
                       System.out.printf("DivideByZeroException handled try entering a valid non zero number \n");
                       operand=Readnumber(scan);
                   }
               }
               acc = acc/operand;
               System.out.printf("Accumulator :%.1f",acc);    
           }
           else {
               try{
                   throw new UnknownOperatorException("Unknown Operator");
               }
               catch(UnknownOperatorException exp){
                   System.out.print("UnknownOperatorException handled try entering a valid operator.\n");    
                   continue;
               }
           }
        }while(!operator.equals("P"));   
    }
    public static Double Readnumber(Scanner scan){
       System.out.println("Enter the operand : ");
         String number = scan.nextLine();
         Double result ;
         try
            {
                result = Double.parseDouble(number);
            }
         catch(NumberFormatException e) {
           System.out.printf("NumberFormatException handled try entering a valid number\n");
              return Readnumber(scan);
              }
         return result;
         }
}
class UnknownOperatorException extends Exception
{
      //Parameterless Constructor
      public UnknownOperatorException() {}

      //Constructor that accepts a message
      public UnknownOperatorException(String message)
      {
         super(message);
      }
}
class DivideByZeroException   extends Exception
{
      //Parameterless Constructor
      public DivideByZeroException() {}

      //Constructor that accepts a message
      public DivideByZeroException(String message)
      {
         super(message);
      }
}

OUTPUT SCREEN:


Related Solutions

PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
How to add, multiply, and divide numbers in flowgorithm
How to add, multiply, and divide numbers in flowgorithm
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
Make a python calculator which deals with arithmetic include plus, minus, divide, and multiply as well...
Make a python calculator which deals with arithmetic include plus, minus, divide, and multiply as well as explaining each answer and use if statement using sign operator.
Subtract/Multiply/Divide using Significant figures 1. (16.75 - 6.2) x (9.56 x 10-19) ------------------------------------------ (6.753.2) x (8.5679...
Subtract/Multiply/Divide using Significant figures 1. (16.75 - 6.2) x (9.56 x 10-19) ------------------------------------------ (6.753.2) x (8.5679 x 1011) This is just one problem, just dividing it 2. Convert 9.64 x 1014 pm3 to nanometers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT