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

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...
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...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT