Question

In: Computer Science

Write a method in JAVA that does this: Presents the user with a header stating this...

Write a method in JAVA that does this:

Presents the user with a header stating this is for assignment: Lab, and the names Bob and Bill

Present the user with a menu to run a random method(just make this a dummy method) , another random method method,(just make this a dummy method) or another dummy method (just make this a dummy method)

Repeat the menu until the user enters -1.

Solutions

Expert Solution

Below is your code:

import java.util.Scanner;

public class Assignment {
        public static void main(String[] args) {
                // printing header
                System.out.println("This is for assignment: Lab, Names: Bob and Bill");
                // intializing Scanner to take user input
                Scanner sc = new Scanner(System.in);
                // Initializing variable to get user choice
                int choice = 0;
                // loop to run till user selects Exit
                while (choice != -1) {
                        // printing menu
                        System.out.println("Select a method to run: ");
                        System.out.println("1. Method 1");
                        System.out.println("2. Method 2");
                        System.out.println("3. Method 3");
                        System.out.println("Enter -1 to exit");
                        System.out.print("Enter your choice: ");
                        // getting user input
                        choice = Integer.parseInt(sc.nextLine());
                        // according to input, selecting which method to run
                        if (choice == 1) {
                                printMessage1();
                        } else if (choice == 2) {
                                printMessage2();
                        } else if (choice == 3) {
                                printMessage3();
                        } else if (choice == -1) {
                                // if -1 selected, get out of the loop
                                break;
                        } else {
                                // if invalid input, then print error message.
                                System.out.println("Please enter a correct choice.");
                        }
                }
                // closing the scanner after running the complete program
                sc.close();
        }

        // dummy method 1
        public static void printMessage1() {
                System.out.println("Selected Choice: " + 1);
        }

        // dummy method 2
        public static void printMessage2() {
                System.out.println("Selected Choice: " + 2);
        }

        // dummy method 3
        public static void printMessage3() {
                System.out.println("Selected Choice: " + 3);
        }
}

Output

This is for assignment: Lab, Names: Bob and Bill
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 1
Selected Choice: 1
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 3
Selected Choice: 3
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 2
Selected Choice: 2
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 2
Selected Choice: 2
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 3
Selected Choice: 3
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 5
Please enter a correct choice.
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: 1
Selected Choice: 1
Select a method to run:
1. Method 1
2. Method 2
3. Method 3
Enter -1 to exit
Enter your choice: -1


Related Solutions

in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation.
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Here is the code that I have so far I can't seem to get it...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation. This is the code I have from the...
Write a method in JAVA to do the following: As the user to select from 2...
Write a method in JAVA to do the following: As the user to select from 2 different (default) pokemon or load their pokemon by giving you the name of their pokemon. Based on their selection (default or supplied name), read the move list and damage range from the input file(you do not need to create this) for the selected pokemon. Randomly select one of the default pokemon - Bulbasaur, Charmander, Squirtle (or you can add additional computer only options if...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Write a java program that does the following: a) The user will enter data such as...
Write a java program that does the following: a) The user will enter data such as client name and client balance. The user can stop inputting data by entering "stop". The program should store the user inputs as a data member of an array. The type of this array is a class named ClientData. Below the output should be displayed by the program. The parts in bold are inputs from the user and not hard coded in the program Client...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT