In: Computer Science
Using Java
Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator:
The calculator will need to trap the following exception conditions:
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:
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. |
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: