Make a choice: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modul
ID: 3746074 • Letter: M
Question
Make a choice: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus 6. Quit CalculatorDriver Calculatorlnterface main: voia -Calculator calculator +Calculatorlnterface) +menu(): void choice: 3 Operand 1: 9 Operand 2: 5 9.0 5.045.0 Calculator +Calculator) + add(operator1: double, operator2: double): double + subtract(operator1: double, operator2: double): double + multiply(operator1: double, operator2: double): double + divide(operator1: double, operator2: double): double + modulus(operator1: double, operator2: double): double Make a choice: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus 6. Quit choice: 6 GoodbyeExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Calculator.java
public class Calculator {
/**
* Default constructor
*/
public Calculator() {
}
/**
* method to add two numbers and return the sum
*/
public double add(double operator1, double operator2) {
return operator1 + operator2;
}
/**
* method to subtract two numbers and return the difference
*/
public double subtract(double operator1, double operator2) {
return operator1 - operator2;
}
/**
* method to multiply two numbers and return the product
*/
public double multiply(double operator1, double operator2) {
return operator1 * operator2;
}
/**
* method to divide two numbers and return the result
*/
public double divide(double operator1, double operator2) {
return operator1 / operator2;
}
/**
* method to find modulus of two numbers and return the result
*/
public double modulus(double operator1, double operator2) {
return operator1 % operator2;
}
}
// CalculatorInterface.java
import java.util.Scanner;
public class CalculatorInterface {
//Calculator instance
private Calculator calculator;
//default constructor
public CalculatorInterface() {
calculator = new Calculator();
}
/**
* method to display the menu until user quits
*/
public void menu() {
//scanner to read user input
Scanner scanner = new Scanner(System.in);
int choice = 0; //loop controller
double operand1 = 0, operand2 = 0;
//looping until user quit
while (choice != 6) {
//displaying menu
System.out.println("Make a choice");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("6. Quit");
System.out.print(" Choice: ");
//getting choice
choice = scanner.nextInt();
//validating choice, getting operands
if (choice >= 1 && choice <= 5) {
System.out.print("Operand 1: ");
operand1 = scanner.nextDouble();
System.out.print("Operand 2: ");
operand2 = scanner.nextDouble();
}
//performing actions based on choice
switch (choice) {
case 1:
System.out.println(operand1 + " + " + operand2 + " = "
+ calculator.add(operand1, operand2));
break;
case 2:
System.out.println(operand1 + " - " + operand2 + " = "
+ calculator.subtract(operand1, operand2));
break;
case 3:
System.out.println(operand1 + " * " + operand2 + " = "
+ calculator.multiply(operand1, operand2));
break;
case 4:
System.out.println(operand1 + " / " + operand2 + " = "
+ calculator.divide(operand1, operand2));
break;
case 5:
System.out.println(operand1 + " % " + operand2 + " = "
+ calculator.modulus(operand1, operand2));
break;
case 6:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
// CalculatorDriver.java
public class CalculatorDriver {
public static void main(String[] args) {
// creating a CalculatorInterface object and invoking menu()
CalculatorInterface calculatorInterface = new CalculatorInterface();
calculatorInterface.menu();
}
}
/*OUTPUT*/
Make a choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Quit
Choice: 1
Operand 1: 123
Operand 2: 456
123.0 + 456.0 = 579.0
Make a choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Quit
Choice: 3
Operand 1: 100
Operand 2: 5
100.0 * 5.0 = 500.0
Make a choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Quit
Choice: 6
Goodbye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.