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.
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:
Get Answers For Free
Most questions answered within 1 hours.