I need a designed as a calculator for elementary school children. It will calcul
ID: 3549945 • Letter: I
Question
I need a 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.
Explanation / Answer
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String option;
do{
System.out.println("****Welcome to calculator*******");
System.out.print("Enter first number:: ");
int first = in.nextInt();
System.out.print("Enter the operator('+','-','*' or '/')::");
String operator = in.next();
System.out.print("Enter second number");
int second = in.nextInt();
double result = 0;
if(operator.equals("+")){
result = first+second;
}
else if(operator.equals("-")){
result = first-second;
}
else if(operator.equals("*")){
result = first*second;
}
else if(operator.equals("/")){
result = first/second;
}
String resultFormatted = String.format("%.2f",result);
System.out.println("Result for "+first+operator+second+" = "+resultFormatted);
System.out.println("Do you want to continue?(Y/N)");
option = in.next();
}
while(option.equalsIgnoreCase("Y"));
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.