In: Computer Science
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers.
Problem 7: Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result.
Your calculator must support addition (+), subtraction (-), multiplication (*), division (/), modulus(%), and exponentiation (**).
Facts
● Mathematical expressions in the form of: num1 operator num2
● Exponentiation should be in the form base ** exponent
○ Example: 3 ** 2 = 9 Input First line is the number of test cases. Each line thereafter is an integer-based mathematical expression in the form defined in the above facts.
Output
For each test case, display the result of the mathematical expression terminated with a new line
character
Sample Input
4
3 + 7
5 - 1
4 ** 2
19 / 5
Sample Output
10
4
16
3
START
STOP
import java.util.Scanner;
public class calc {
static Scanner sc=new Scanner(System.in);
static void perform(String s[]) {
int
op1=Integer.parseInt(s[0]);
int
op2=Integer.parseInt(s[2]);
String op=s[1];
switch(op) {
case "+" :
System.out.println(op1+op2);
break;
case "-" :
System.out.println(op1-op2);
break;
case "*" :
System.out.println(op1*op2);
break;
case "/" :
if(op2==0)
System.out.println("Infinite");
else
System.out.println(op1/op2);
break;
case "%" :
System.out.println("op1%op2");
break;
case "**" :
System.out.println((int)Math.pow(op1, op2));
break;
}
}
public static void main(String[] args) {
System.out.println("enter no of test cases : ");
int n;
n=sc.nextInt();
sc.nextLine();
for(int i=1;i<=n;i++) {
System.out.println("ENTER INPUT NO
"+i);
String s[];
s=sc.nextLine().split(" ");
perform(s);
}
}
}