Write a program that is designed as a calculator for elementary school children.
ID: 3549914 • Letter: W
Question
Write a program that is designed as a calculator for elementary school children. It will calculate addition, subtraction, multiplication, and division. The program will ask for the first digit, then the operator, then the second digit. It will display the answer to the problem after the second digit is entered.
In the output, no answer is to be displayed with more than 2 decimal points.
Care should be taken to write your code using proper formatting, including a header, appropriate indentation, and adequate code comments. The program should also use good user dialog. Remember, this program is for elementary school children so you have to tell them exactly what to do. You also need to make the user dialog fun without being silly.
If comments are added I will add more points!
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class calculator{
public static void main(String[] args){
int num1,num2,res = 0;
char operator = '';
Scanner in = new Scanner(System.in);
System.out.println("Enter the first number : ");
num1 = in.nextInt();
try{
System.out.println("Enter operator : ");
operator = (char)System.in.read();
}catch(IOException e){
System.out.println("IOException has been caught");
}
System.out.println("Enter the second number : ");
num2 = in.nextInt();
switch(operator){
case '+' :
res = num1 + num2;
break;
case '-' :
res = num1 - num2;
break;
case '*' :
res = num1 * num2;
break;
case '/' :
res = num1 / num2;
break;
default :
}
//System.out.println(num1 + " " + operator + " " + num2 " = " + res);
System.out.println(res);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.