In this project, is asking to write a simple calculator which can add, subtract,
ID: 3863643 • Letter: I
Question
In this project, is asking to 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.
Explanation / Answer
/**
* @(#)Calculator.java
*
*
* @author
* @version 1.00 2017/3/4
*/
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. ");
continue;
}
}
char op = operator.charAt(0);
if(op=='P'){
System.out.printf("Accumulator :%.1f ",acc);
}
else if(op=='R') {
acc=0.0;
System.out.printf("Accumulator :%.1f ",acc);
}
else if(op=='+'){
operand = Readnumber(scan);
acc = acc + operand;
System.out.printf("Accumulator :%.1f ",acc);
}
else if(op=='-'){
operand = Readnumber(scan);
acc = acc - operand;
System.out.printf("Accumulator :%.1f ",acc);
}
else if(op=='*'){
operand = Readnumber(scan);
acc = acc * operand;
System.out.printf("Accumulator :%.1f ",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 ");
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. ");
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 ");
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.