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) 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.
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()...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT