Question: How do I add functioning operations + , - , * , / in this Java code? I've got the program to accept

How do I add functioning operations "+,-,*,/" in this Java code? I've got the program to accept and display two variables (numA, numB) but now I need to implement code so when the user types an operator, it manipulates the two values and displays the result.
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
Character command ='_';
// loop until user quits
while (command !='q'){
printMenu();
System.out.print("Enter a command: ");
command = menuGetCommand(scan);
executeCommand(scan, command);
if (command =='a'){
System.out.println("Enter a number: ");
double numA = scan.nextDouble();
if (numA >0){
System.out.printf("A ="+ numA +"
");
}
}
if (command =='b'){
System.out.println("Enter a number: ");
double numB = scan.nextDouble();
if (numB >0){
System.out.printf("B ="+ numB +"
");
}
}
}
scan.close();
}
//
// menu functions
//
private static void printMenuLine(){
System.out.println(
"----------------------------------------------------------"
);
}
private static void printMenuCommand(Character command, String desc){
System.out.printf("%s\t%s
", command, desc);
}
// prints the menu
public static void printMenu(){
printMenuLine();
System.out.println("ChavviCalc");
printMenuLine();
printMenuCommand('a', "Enter a value for A");
printMenuCommand('b', "Enter a value for B");
printMenuCommand('+', "Add");
printMenuCommand('-', "Subtract");
printMenuCommand('*', "Multiply");
printMenuCommand('/', "Divide");
printMenuCommand('c', "Clear");
printMenuCommand('q', "Quit");
printMenuLine();
}
// get first character from input
private static Character menuGetCommand(Scanner scan){
Character command ='_';
String rawInput = scan.nextLine();
if (rawInput.length()>0){
rawInput = rawInput.toLowerCase();
command = rawInput.charAt(0);
}
return command;
}
// calculator functions
private static Boolean executeCommand(Scanner scan, Character command){
Boolean success = true;
switch (command){
case 'q':
System.out.println("Thank you for using Chavvi Calc");
break;
case 'a','b':
break;
default:
System.out.println("ERROR: Unknown commmand");
success = false;
}
return success;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!