Question

In: Computer Science

JAVA !!! EX 8.1Implementing a Menu Class Problem StatementIn this Worked Example, our task is to...

JAVA !!!

EX 8.1Implementing a Menu Class

Problem StatementIn this Worked Example, our task is to write a class for displaying a menu. An object of this class can display a menu such as

1) Open new account
2) Log into existing account
3) Help
4) Quit

Reimplement the Menu class of Worked Example 8.1 so that it stores all menu items in one long string. Hint: Keep a separate counter for the number of options. When a new option is added, append the option count, the option, and a newline character.

Solutions

Expert Solution

Hi, Please go through code and output.

CODE:

public class Menu { // class Menu
   public String menuItems; // menu string
   public Integer counter; // counter
   public static void main(String[] args) // Main
   {
       Menu obj = new Menu(); // take object of Menu class
       obj.addItem("1) Open new account\n"); // add menu Item in string
       obj.addItem("2) Log into existing account\n");
       obj.addItem("3) Help\n");
       obj.addItem("4) Quit\n");
       obj.printMenu(); // print Menu Items
   }
   Menu()
   {
       menuItems = "";
       counter = 0;
   }
   public void addItem(String str) // add Item in string
   {
       menuItems += str;
       counter++; // increment counter
   }
   public void printMenu() // print Menu Items
   {
       System.out.println(menuItems);
   }
}

OUTPUT:



Related Solutions

Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
Stockholders' Equity. (Note: an example for this problem will be worked in class on Monday, June...
Stockholders' Equity. (Note: an example for this problem will be worked in class on Monday, June 11. You might want to wait until Monday evening to try this problem.)                            Given the following information for Company G at 1/1/16:                   Retained earnings at 1/1/16 were $800,000.                   Common stock at 1/1/16 was $ 300,000.                   Additional Paid-in Capital at 1/1/16 was $900,000.          The description of the common stock at 1/1/16 was: $10 par value, 100,000 shares authorized, 30,000...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G',...
Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G', 'D', and 'X'. If 'G' is selected, prompt the user for the ID of a president to display to the screen and then display the president's information. If 'D' is selected, display all the values in the LinkedHashMap to the user with their associated LinkedHashMap key. If 'X' is selected, exit the program. The class should have the method addPresident(id, lastName, firstName, middleInitial) which...
to be done in java Your task is to finish the StockItem class so that it...
to be done in java Your task is to finish the StockItem class so that it meets the following criteria • The StockItem class will have 4 attributes: a stock number; a name; the price of the item; the total number of items currently in stock • The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The...
Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
What are the Signature lines of a Java class code? With an example. and the UML...
What are the Signature lines of a Java class code? With an example. and the UML diagram.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT