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 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...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT