Question

In: Computer Science

Java Programming - please read the description I need this description. Inside a do/ while loop...

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!!

Solutions

Expert Solution

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


Related Solutions

JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method. This application needs to hold the user input's numbers as an array. Thanks for your time. It is a great help!
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
C programming. Explain by taking a programming example how do while loop is different from while...
C programming. Explain by taking a programming example how do while loop is different from while loop?
The following code is inside a do while loop. choice is supposed to be an integer...
The following code is inside a do while loop. choice is supposed to be an integer input by the user. However, when a character is put instead of an integer, the code loops infinitely. Putting break; or exit(); after the catch statements terminates the whole program, which is wrong. How can i fix this? #include <iostream> #include <vector> #include <iomanip> #include<stdlib.h> #include <cctype> #include "MyGrades.h" using namespace std; int main () {    int choice; // user input for menu...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT