SimpleCalculator.java I need help with this program, i have no idea where to sta
ID: 3557673 • Letter: S
Question
SimpleCalculator.java
I need help with this program, i have no idea where to start. I need to have a different class for Calculator, CalculatorTest and UnknownOperatorException
Write a program that can serve as a simple calculator. This calculator keeps track of a single number (of type double) that is called result and that starts out as 0.0. Each cycle allows the user to repeatedly add, subtract, multiply, or divide by a second number. The result of one of these operations becomes the new value of result. The calculation ends when the user enters the letter R for
Explanation / Answer
some other outline ...
import java.io.*;
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
boolean flag=true;
double result=0;
while(flag){
System.out.println("Please Enter Operation and number ");
String line=sc.nextLine();
char ch=line.charAt(0);
switch(ch){
case '+':
result+=Integer.parseInt(line.substring(...
System.out.println("Result :"+result);
break;
case '-':
result-=Integer.parseInt(line.substring(...
System.out.println("Result :"+result);
break;
case '*':
result*=Integer.parseInt(line.substring(...
System.out.println("Result :"+result);
break;
case '/':
result/=Integer.parseInt(line.substring(...
System.out.println("Result :"+result);
break;
default :
System.out.println(ch+" Unknown Operator ");
flag=false;
}}
if(!flag)
try {
throw new Exception("Unkown Operator ");
} catch (Exception ex) {
ex.printStackTrace();
}
} }
here is some output :
Please Enter Operation and number
+5
Result :5.0
Please Enter Operation and number
-2
Result :3.0
Please Enter Operation and number
*5
Result :15.0
Please Enter Operation and number
%2
% Unknown Operator
java.lang.Exception: Unkown Operator
at Main.main(Main.java:41)
BUILD SUCCESSFUL (total time: 24 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.