Question

In: Computer Science

TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...

TO BE DONE IN JAvA

Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line.

Possible operations are + - * /

No other words or symbols should be output, just the answer.

Requested files

import java.util.Scanner;

/**

* This is a simple calculator Java program.

* It can be used to calculate some simple calculation

* such as +, -, *, /. of two integers.

*/

public class Calculator {

     // the result of the math operation

     private int result;

     /**

      * This method is used to calculate a + b

      * @param a - an integer

      * @param b - an integer

      */

     public void add(int a, int b) { //a+b    

     }

     /**

      * This method is used to calculate a - b

      * @param a - an integer

      * @param b - an integer

      */

     public void subtract(int a, int b) { //a-b    

     }

     /**

      * This method is used to calculate a * b

      * @param a - an integer

      * @param b - an integer

      */

     public void multiply(int a, int b) { //a*b

   }

    

     /**

      * This method is used to calculate a/b

      * @param a - an integer

      * @param b - an integer

      */

     public void divide(int a, int b) {

     }

     /**

      * This method is used to return the intance variable result

      * @return The String result of the instance variable

      * Tips: Convert int to String

      */

     @Override   

     public String toString() {

         return "";

     }

    

     public static void main(String[] args){

        

         Calculator calc = new Calculator(); //use this instance to call the methods above to solve the problem

         }

}

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------Calculator.java----------------

import java.util.Scanner;

/**
* This is a simple calculator Java program.
* It can be used to calculate some simple calculation
* such as +, -, *, /. of two integers.
*/
public class Calculator {
   // the result of the math operation
   private int result;
   /**
   * This method is used to calculate a + b
   * @param a - an integer
   * @param b - an integer
   */
   public void add(int a, int b) { //a+b
       result = a + b;            //store the sum in the result
   }

   /**
   * This method is used to calculate a - b
   * @param a - an integer
   * @param b - an integer
   */
   public void subtract(int a, int b) { //a-b
       result = a - b;            //store the difference in the result
   }

   /**
   * This method is used to calculate a * b
   * @param a - an integer
   * @param b - an integer
   */
   public void multiply(int a, int b) { //a*b
       result = a * b;            //store the product in the result
   }

   /**
   * This method is used to calculate a/b
   * @param a - an integer
   * @param b - an integer
   */
   public void divide(int a, int b) {
       result = a / b;            //store the division answer in the result
   }

   /**
   * This method is used to return the intance variable result
   * @return The String result of the instance variable
   * Tips: Convert int to String
   */
   @Override
   public String toString() {
       return Integer.toString(result);
   }

   public static void main(String[] args) {
       Calculator calc = new Calculator(); //use this instance to call the methods above to solve the problem
       Scanner sc = new Scanner(System.in);
      
       int a, b;
       System.out.print("Enter integer a: ");
       a = sc.nextInt();                           //read the two integers a, b
       System.out.print("Enter integer b: ");
       b = sc.nextInt();

       char operator;
       System.out.print("Enter operation: ");
       operator = sc.next().charAt(0);               //read the operation: + - * /
       if(operator == '+'){
           calc.add(a, b);                           //addition
       }
       else if(operator == '-'){
           calc.subtract(a, b);                   //subtraction
       }
       else if(operator == '*'){
           calc.multiply(a, b);                   //multiplication
       }
       else if(operator == '/'){
           calc.divide(a, b);                       //division
       }
       System.out.println(calc);   //print the result: This will automatically call the toString method, which prints the result
   }
}

--------------Screenshots-------------------

------------Output-----------------

--------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a java program that prompts the user to see if they wish to encode or...
Write a java program that prompts the user to see if they wish to encode or decode a message. (a) If they choose to encode a message: i. Ask them to enter the message and store it in a String variable. ii. Once the message is entered, ask them for a “shift” integer, similar to Lab 6. iii. Using that shift, move all alphabetic characters in the message (i.e. excluding spaces and punctuation) using char and int arithmetic. Be sure...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT