Question

In: Computer Science

Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem...

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

Solutions

Expert Solution

  • SUDO CODE OF THE PROBLEM : -

START

  1. TAKE INPUT IN VARIABLE 'N'
  2. FOR [1 TO N] DO 3 TO 11
  3. TAKE INPUT IN STRING 'S'
  4. SPLIT 'S' INTO 3 PARTS i.e 'OP1'=1st PART , 'OP2'=3rd PART , 'OP'=2nd PART
  5. SWITCH (OP)
  6. IF OP="+" : DO ADDITION AND PRINT ANSWER
  7. IF OP= "-" : DO SUBTRACTION AND PRINT ANSWER
  8. IF OP= "*" : DO MULTIPLICATION AND PRINT ANSWER
  9. IF OP= "/" : DO DIVISION AND PRINT ANSWER
  10. IF OP= "**" : DO EXPONENT AND PRINT ANSWER
  11. IF OP= "%" : DO MODULUS AND PRINT ANSWER

STOP

  • PROGRAM OF THE PROBLEM IN JAVA : -

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);
   }
}
}


Related Solutions

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...
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...
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 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,...
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...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for a program for Hansel's Housecleaning Service. The program prompts the user for a customer's last name only. While the last name is not “zzz” your program will ask for the number of bathrooms and the number of other rooms to be cleaned and display the cleaning charge. You should use a sentinel-controlled while loop in your main loop logic. A customer name of “zzz”...
Complete the following Java-like pseudocode. Your code should multiply all the numbers in nums and store...
Complete the following Java-like pseudocode. Your code should multiply all the numbers in nums and store it in a variable called mult      public static void main(String[] args){                         int[] nums = <some array values>;
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT