In: Computer Science
Java Programming - please read the description I need this description.
Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods.
One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result.
Thanks for your help!
Thanks for asking.
The other choices are:
One call for a method that changes the user input for other numbers using an array to store this information.
And the other method is similar to addiction(), however is a subtraction()
Thanks for your help!!
import java.util.Scanner;
public class MenuDriver {
float a,b;
public void input(Scanner sc) {
System.out.println("Enter two numbers for addition");
a = Float.parseFloat(sc.nextLine());
b = Float.parseFloat(sc.nextLine());
}
public static void main(String[] args) {
MenuDriver d = new MenuDriver();
Scanner sc = new Scanner(System.in);
System.out.println("Enter choice ----> a for addition , s for subtraction and X for exit");
String ch = sc.nextLine();
do {
ch = ch.toLowerCase();
switch(ch) { // a-> addition. s-> subtraction
case "a":
d.input(sc); // taking user input.
float add = d.addition(); // doing addition and getting result in add variable
display(add);
break;
case "s":
d.input(sc);
float sub = d.subtraction(); // doing addition and getting result in add variable
display(sub);
break;
}
System.out.println("Enter choice ----> a for addition , s for subtraction and X for exit");
ch = sc.nextLine();
}while(!ch.equalsIgnoreCase("X"));
sc.close();
}
private float subtraction() {
return a-b;
}
private static void display(float res) {
System.out.println("Result is "+res);
}
private float addition() {
return a+b;
}
}
- Please note, 2 cases are required for addition and
subtraction.
- Case 3 is not specified clearly . so took input of floating
numbers.
Kindly upvote if
this helped/ comment for help
OUTPUT:
SNAPSHOTS