Question

A question very much like the one posted by another user that had not been answered...

A question very much like the one posted by another user that had not been answered yet:

https://www.chegg.com/homework-help/questions-and-answers/question-1-calculator-6-pts-question-write-simple-calculator-3141592-write-program-prompts-q41214637

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//For any queries, feel free to comment.

**CODE**

import java.util.*;

public class Main
{
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
      
       //Loop ot continue until
       //user wants
       while(true){
            System.out.print("Enter expression('exit' to end): ");
            String str = in.nextLine();
            int index = -1;
          
            //Check input with exit
            if(str.equals("exit")){
                System.out.println("Program Ended.");
                break;
            }
          
            //Finding the operator
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)=='+' || str.charAt(i)=='*' || str.charAt(i)=='-' || str.charAt(i)=='/' || str.charAt(i)=='%'){
                    index = i;
                    break;
                }
            }
          
            //Extrcting the operator and operand
            String op1 = str.substring(0,index);
            String ope = str.substring(index,index+1);
            String op2 = str.substring(index+1,str.length());
          
            //Parsing String into integer
            int num1 = Integer.parseInt(op1);
            int num2 = Integer.parseInt(op2);
          
            //Calculating answer according to operator
            if(ope.equals("+")){
                int ans = num1 + num2;
                System.out.println("Output: " + ans);
            }
            else if(ope.equals("-")){
                int ans = num1 - num2;
                System.out.println("Output: " + ans);
            }
            else if(ope.equals("*")){
                int ans = num1 * num2;
                System.out.println("Output: " + ans);
            }
            else if(ope.equals("/")){
                int ans = num1 / num2;
                System.out.println("Output: " + ans);
            }
            else if(ope.equals("%")){
                int ans = num1 % num2;
                System.out.println("Output: " + ans);
            }
       }
   }
}

**SNIPPET**

**OUTPUT**

Add a comment
Know the answer?
Add Answer to:
A question very much like the one posted by another user that had not been answered...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i can't believe i have yet to get this javascript question answered correctly?   One final try:...

    i can't believe i have yet to get this javascript question answered correctly?   One final try: Here goes..... Please write a very simple Javascript HTML program. The purpose is to calculate an employees pay. I need 4 prompts. The # of hours the employees worked The # of hours the employee is contracted to work before they receive overtime the hourly payrate do they get 1.5 or 2 times their hourly rate for overtime TEST TEST TEST the program with...

  • I asked a question similar to this one, which was answered perfectly. Another practice problem is...

    I asked a question similar to this one, which was answered perfectly. Another practice problem is now asking me to use two classes and get user input. For this Java program, you will write two classes: GradeCalculator and GradeCalculatorDriver In the GradeCalculator class, compute the final average and letter grade for a particular student. The final average is calculated according to the following rules: 1) There are ten exams scored out of 100 points 2) The lowest exam score is...

  • There was another answer for this question but it was not done correctly. Here is the...

    There was another answer for this question but it was not done correctly. Here is the link to it: https://www.chegg.com/homework-help/questions-and-answers/objective-working-arrays-write-c-program-record-gpas-class-find-number-students-eaclh-gpa--q29791390 That code only was able to calculate the GPA of exactly 8 students. The question is asking to calculate for n number of students. Write a C program to record the GPAs of a class and find the percentage of students in each GPA he program first allows the user to enter the number of students enrolled in the class,...

  • Hi I need help with these questions For each of the following compounds, write a) the...

    Hi I need help with these questions For each of the following compounds, write a) the correct molecular formula b) the correct structural formula c) correct name according to the rules of the IUPAC First compound: 2 fluoro 1 chloro 3 methyl 4 ethyl 5 vinyl 1(2 propyl) 5 terbutyl 1 ene 3,4 hexanediol Second compund: 6,6 dichloro 2 methyl 4 ethyl 5 cyano 3 nitro 2 amino 3 vinyl 6(2 propyl) 4 ene hexanoic Indicate the products that will...

  • 1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or **...

    1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or ** 2. Prompt the user for two input integers ('a' and'b') 3. The input numbers can be positive or negative 4. Perform the arithmetic operation using the two input numbers 5. The first entered number ('a') is the left side, the second ('b') the right side of the operation For exponentiation, the first number is the base, the second the exponent Print (display) the following...

  • [Using Python] Create a program that uses import random and while loops to generate one random...

    [Using Python] Create a program that uses import random and while loops to generate one random question, from a list of 5, for each repeated run. Report the number of the correct answers after all five questions have been answered. Your 5 questions should be a simple math problem, like "what is 7-5?"

  • In c++ Write a program that allows the user to type in any one-line question and then answers tha...

    in c++ Write a program that allows the user to type in any one-line question and then answers that question. Your program won't really respond to the question, rather it will read a random response from a file. Requirements Your program should have a function called getQuestion() which prompts the user for a question and returns the number of characters in the question (including spaces and punctuation, not including the ending newline). If the user enters an empty string they...

  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • What instrument is used for food canning? Question 1 Not yet answered Select one: O a....

    What instrument is used for food canning? Question 1 Not yet answered Select one: O a. autoclave Points out of 1.00 b. oven P Flag question c. regenerator d. steamer Question 4 When performing a gram stain, what is the purpose of heating the slide once the bacteria have been added? Not yet answered Points out of 1.00 Flag question Select one: a. kill the balleria b. attach the bacteria to the slide c. increase the permeability of the bacteria...

  • use python IDEL Question 17 5 pts Problem 5 Write a program. You have been hired...

    use python IDEL Question 17 5 pts Problem 5 Write a program. You have been hired as a programmer and a Unix administrator asked you to check these three lists of users to find who had logged into all Unix servers and who had logged only into the main Unix server. Here are the lists: Main Unix Server: Server II: Server III: alex maya akhmed linda mila kyrill kyrill andrew linda akhmed rosa maya sandra linda alex You need to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT