Implement a simple “English language\" calculator that reads from the standard i
ID: 3728519 • Letter: I
Question
Implement a simple “English language" calculator that reads from the standard input in the format and evaluates this expression. Your program should support four operators: +-, and/ and should repeatedly allow the user to enter expressions which are then evaluated. Use Exceptions to handle the case of non-integer data for the operands. (Catch the InputMismatchException.) Your program should also handle a non-valid string being entered for the operator by explicitly throwing an IllegalArgumentException (and dealing with it somewhere). In both cases, point out the error to the user and ask him/her to re-enter the entire expression. Note that division should perform a Java integer division with no fractional component displayed. In addition, disallow division by zero by catching the exception thrown by Java; simply print out a message identifying the error. Do your exception handling in separate methods as far as possible. If your code is cluttered or hard to read, you will be penalized A sample run is shown below (user input in bold). Simulate the behavior of the code below n your program. Starting Calculator vl.0 Enter an expression or Q to quit >>2 3 2 plus 3 is 5 >>2 3 2 multiplied by 3 is 6 12-5 12 minus 5 is7 >32 3 divided by 2 is 1 >30 Division by zero is not allowed >ab 5 Invalid operand. Operands must be integer Invalid operand. Operands must be integer >x +y Invalid operand. Operands must be integer >>2&3 Invalid operator. Operators must be one of +, -, * or / >>2+3Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class Calculator
{
public int add(int num1, int num2)
{
int sum;
sum = num1 + num2;
return sum;
}
public int sub(int num1, int num2)
{
int sum;
sum = num1 - num2;
return sum;
}
public int multi(int num1, int num2)
{
int sum;
sum = num1 * num2;
return sum;
}
public int div(int num1, int num2)
{
int sum;
sum = num1 / num2;
return sum;
}
public int mod(int num1, int num2)
{
int sum;
sum = num1 % num2;
return sum;
}
public int pow(int base, int exp)
{
int sum = 1;
if (exp == 0)
{
sum = 1;
}
while (exp > 0)
{
sum = sum * base;
exp--;
}
return sum;
}
public static void main(String[] args)
{
int choice;
int x;
int y;
int sum;
PrintStream out;
Calculator calc = new Calculator();
try
{
out = new PrintStream ("calclog.txt");
do
{
System.out.println("Calculator Program");
System.out.println("-------------------- ");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program ");
System.out.println("Enter Choice: ");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99)
{
System.out.println("Please enter a 1, 2, 3, 4, 5, or 6: " );
choice = input.nextInt();
}
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
switch (choice)
{
case 1:
sum = calc.add(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
sum = calc.div(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "/" + y + "=" + sum);
break;
case 5:
sum = calc.mod(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x,y);
System.out.printf("The sum is %d ",sum);
out.println(x + "^" + y + "=" + sum);
break;
}
}
while (choice != 99);
System.out.println("Ending program...");
}
catch(Exception e)
{
System.out.println("ERROR: Could not open file!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.