Takes three inputs from the keyboard, two of them single digits (0-9) and the ot
ID: 3530024 • Letter: T
Question
Takes three inputs from the keyboard, two of them single digits (0-9) and the other a char (+,-,*,/,^) representing addition, subtraction, multiplication, division, and exponentiation. Outputs the description of the operation in plain English, as well as the numeric result. Example-- 2 inputs are 5 and 3 and operation is *. Output should be "five multiplied by three is 15 Use pow method of the Math class to perform exponentiation. If the two numbers are 25 and 3, the operation +. The output should be "Invalid number". Translated as follows: + plus - minus * multiplied by / divided by ^ to the power Use the switch...case selection statement to translate the input values into words. Cannot divide by 0 so test whether second number is 0 for division and output a message saying that you are not allowed to divide by 0. If the operator is not one of the 5, output saying that it is not a valid operator. One or two of the numbers is not a valid digit, should output a message saying invalid input. You can deal with the above 3 situations in the default statement and possibly use some boolean variables to keep track of this information, as you may need it later in your program.Explanation / Answer
mport java.util.Scanner;
public class EnglishCalculator {
static int num1;
static int num2;
static char operator;
static double result;
static String str;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter the first number(0-9) :");
num1 = input.nextInt();
if (num1 < 0 || num1 > 9) {
System.out.println("Invalid number");
return;
}
System.out.println("enter the second number(0-9) :");
num2 = input.nextInt();
if (num2 < 0 || num2 > 9) {
System.out.println("Invalid number");
return;
}
System.out.println("Enter the operator :");
operator = input.next().charAt(0);
if (operator != '+' && operator != '-' && operator != '*'
&& operator != '/' && operator != '^') {
System.out.println("Invalid Operator");
return;
}
switch (operator) {
case '+':
str = "plus";
result = num1 + num2;
break;
case '-':
str = "minus";
result = num1 - num2;
break;
case '*':
str = "multiplied by";
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
System.out.println("Division by zero is not allowed");
return;
}
str = "divided by";
result = num1 / num2;
break;
case '^':
str = "to the power";
result = Math.pow(num1, num2);
break;
}
System.out.println(num1 + " " + str + " " + num2 + " is " + result);
}
}
Output
enter the first number(0-9) 3
enter the second number(0-9) 7
Enter the operator : *
3 multiplied by 7 is 21.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.