In: Computer Science
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
}
}
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