In: Computer Science
This is a Entry to Java question. Please show me the PseudoCode or Commits first so I can learn to analyze everything a a little bit better.
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
The main techniques that should be used are selection statements and repetition statements.
PseudoCode |
start 1. ask user for number of inputs 2. accept user input 3. initialize loop variable to 0 3.1 if loop variable less than (input count - 1) then execute till 3.9 else goto 4 3.2 accept user input 3.3 if user input contains % then 3.3.1 split input by the character % 3.3.2 compute modulus of the two operands 3.3.3 save the result in an array 3.4 if user input contains ** then 3.4.1 split input by the character ** 3.4.2 compute op1^op2 (op1 raised to the power of op2) 3.4.3 save the result in an array 3.5 if user input contains + then 3.5.1 split input by the character + 3.5.2 compute sum of the two operands 3.5.3 save the result in an array 3.6 if user input contains - then 3.6.1 split input by the character - 3.6.2 subtract second operand from the first operand 3.6.3 save the result in an array 3.7 if user input contains * then 3.7.1 split input by the character * 3.7.2 compute product of the two operands 3.7.3 save the result in an array 3.8 if user input contains / then 3.8.1 split input by the character / 3.8.2 compute op1 / op2 (division) 3.8.3 save the result in an array 3.9 increase loop variable by 1 4. initialize a loop variable to 0 4.1 if loop variable (k) less than (input count - 1) then execute till 4.3 else goto end 4.2 print array[k] 4.3. increment loop variable k by 1 end |
Java code |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Calculator { public static void main(String[] args) throws IOException { Calculator calculator = new Calculator(); InputStream in; try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){ String inputCountStr = br.readLine(); int inputCount = Integer.parseInt(inputCountStr); String line; String[] operands; long[] output = new long[inputCount]; for(int i = 0; i < inputCount; i++){ line = br.readLine(); if(line.contains("%")){ operands = line.split("%"); output[i] = calculator.mod(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } else if(line.contains("**")){ operands = line.split("\\*\\*"); output[i] = calculator.pow(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } else if(line.contains("+")){ operands = line.split("\\+"); output[i] = calculator.add(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } else if(line.contains("-")){ operands = line.split("-"); output[i] = calculator.subtract(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } else if(line.contains("*")){ operands = line.split("\\*"); output[i] = calculator.multiply(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } else if(line.contains("/")){ operands = line.split("/"); output[i] = calculator.divide(Integer.parseInt(operands[0]), Integer.parseInt(operands[1])); } } for(int k = 0; k < inputCount; k++){ System.out.println(output[k]); } } } public long add(int a, int b){ return a + b; } public int subtract(int a, int b){ return a - b; } public long multiply(int a, int b){ return a * b; } public int divide(int a, int b){ return a / b; } public int mod(int a, int b){ return a % b; } public long pow(int a, int b){ return (long)Math.pow(a, b); } } |
Sample input/output:
6
7+6
5-4
3*6
9/3
8%3
2**3
13
1
18
3
2
8
Summery: the program first asks (however, no message in prompt - as per question) for the number of inputs (calculations) the user intends to enter. then the program takes the inputs - one input per line; and once all the inputs are taken, all the results are printed in the console in the order of the given inputs. the program ends there.
Please provide your feedback for the answer.