Question

In: Computer Science

There is already an existing class called Sizes, with an existing main method. Inside the Sizes...

There is already an existing class called Sizes, with an existing main method.

Inside the Sizes class, create a method called menu. The purpose of this method is to display the menu on the screen and allow the user to make a choice from the menu.

Do not change any of the existing code in the main method, and do not add code to the main method. Changing the main method in any way will give you an automatic grade of zero on this part of the assignment.

This method does not need to accept any parameters from the main method. Choices should be read into the menu method from the keyboard as a number 1, 2, 3 or 4. This method will need to contain a loop which will repeat the process of displaying the menu and getting an input value as long as the user does not enter a correct menu option. After a correct menu option has been entered, the method should return that choice back to the main method.

Note that this method is required to verify that the entered choice is correct, so the main method does not have to do that task.

The following is an example of what your MIGHT see on the screen when your menu method executes. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics. Note that your method must display the exact formatting shown here - including the spaces and blank lines.

Solutions

Expert Solution

import java.util.Scanner;

public class Sizes {

    public static Scanner kbd;
    public static void main(String[] args) {
        kbd = new Scanner(System.in);
        System.out.println("Begin test");
        System.out.println("Method returned: " + menu());
        System.out.println("Test complete");
        kbd.close();
    }

    // Write the menu method here
    public static int menu() {
        int num;
        while (true) {
            System.out.println("1. Calculate Hat Size");
            System.out.println("2. Calculate Jacket Size");
            System.out.println("3. Calculate Waist Size");
            System.out.println("4. No More Calculations");
            System.out.print("
Enter your choice: ");
            num = kbd.nextInt();
            if(num >= 1 && num <= 4) {
                return num;
            }
            System.out.println();
        }
    }

}


Related Solutions

Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
For question 1 , consider that inside the class Sky, we have already coded the following:...
For question 1 , consider that inside the class Sky, we have already coded the following:     public class Sky      {         private Color color;         public Sky( Color c)           {               color = c;           }       }   Consider the following method header:     public Color getColor(); Is this method a constructor, mutator or accessor?   Inside a method main, we see code like:      Airplane.foo3(34.6); From this, reconstruct the header of method foo3 (which belongs to class...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT