Question

In: Computer Science

This is a Entry to Java question. Please show me the PseudoCode or Commits first so...

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.

Solutions

Expert Solution

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.


Related Solutions

This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
Please give me the journal entry for the first interest payment on each of the following...
Please give me the journal entry for the first interest payment on each of the following bond issues: 1. We issued bonds at 102% with a par of $1,000 a life of 20 years with an interest rate of 10% 2. We issued bonds at 98% with a par of $1,000 a life of 5 years with an interest rate of 9% 3. We issued bonds at 100% with a par of $1,000 a life of 2 years with an...
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently. Problem 5: Player Move Dungeon (10 points) (Game Development) You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row...
Can someone please show me how to do the work on this please. Question The transactions...
Can someone please show me how to do the work on this please. Question The transactions of the Fury Delivery Service are recorded in the general journal below. Instructions: 1. Post the journal entries to the attached general ledger “T” accounts. 2. Prepare a trial balance on the form provided after the “T” accounts. General Journal Date Account Titles and Explanation Debit Credit 2017 Sept. 1 Cash Common Stock (Stockholders invested cash in business) 25,000 25,000 4 Equipment Cash Notes...
Please walk me through SPSS to answer this question, NOT just answer it but please show...
Please walk me through SPSS to answer this question, NOT just answer it but please show me how you did it on SPSS... Consider the data below of inches of rainfall per month for two different regions in the Northwestern United States:    Plains Mountains April 25.2 13.0 May 17.1 18.1 June 18.9 15.7 July 17.3 11.3 August 16.5 14.0 Using SPSS, perform a two-sample t-test for the hypothesis that there is not the same amount of rainfall in both...
Java Please Source Only Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and...
Java Please Source Only Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT