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

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)
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...
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 Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT