Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a java code for the following * A program to test the ArrayMenu implementation of MenuInterface, using a * text adventure style game. *

write a java code for the following
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
* A program to test the ArrayMenu implementation of MenuInterface, using a * text adventure style game. * eauthor */ ublic class A03 f private static final Scanner kbd = new Scanner(System.in); public static void main(String[] args) \{ // create variables String[] allways = new string[]\{"north", "east", "south", "west" }; MenuInterface [] rooms = new MenuInterface [1} // constructor with just a title new Arraymenu( "You are in the start room. You can go:"), // constructor with title and initial capacity new Arraymenu( "You are in room 1. You can go:", 3), // constructor with title and options new ArrayMenu( "You are in room 2. You can go:", new String[1\{" northerly", "southerly", "westerly" } ), // constructor with just initial capacity new Arraymenu(4), // constructor with just options new ArrayMenu(new string[\}\{"northish", "eastish", "southish" } ), // no-args constructor new Arraymenu() \}i: int[][] exits = new int [][]{// where we go new int [1{1,2,3,4}, // from room[0] new int [1{2,0,4}, 1/ from room[1] new int \{]{1,3,0}, 1/ from room[2] new int [1{0,2,5,4},1/ from room[ 3} new int {1{1,0,3}, 1/ from room [4] new int {1{3}// from room[5] \}; int currentroom; int choice; final int END_ROOM =5; // print introduction printintroduction(); // finish making the Menus rooms [0] addopt ions (al1ways); // finish making the Menus rooms [0] -addoptions (allWays); rooms [1] addoption ( "sorta east"); rooms [1] addoption( "sorta south"); rooms [1] addoption ("sorta west"); rooms [3] . addoptions (new string[]\{ "north", "east")), rooms [3] addoptions (new string[]\{"south", "west")); rooms [4].setTitle( "You are in room 4. You can go: "); rooms [5]. setTitle("This is the END room!"); I/ neither of these commands should alter the program's behaviour al1Ways [0]= "to the great white north"; if (rooms [ 3] addoption ("up, up, and away!")) f 3 system.out.println("It added an option when it shouldn't have!") ; 1/ start wandering from room 0 currentRoom =0; // while we haven 't got to the end room while (currentRoom t= END_ROOM) \{ 1/ display the menu for this room and get the choice rooms [currentRoom]. display (); choice = rooms [ currentRoom] . getChoice (kbd); I/ deal with the choice if (choice >0 ) f 1/ move to the selected room currentRoom = exits [currentRoom] [choice 1 ]; y else \{ 1/ invalid response - check for quitting System. out.println(); if (confirm( "Would you like to quit? ")) \{ System.out.println("Seeya, quitter!"); showMenus (rooms, exits); System.exit(0); \} If finish up with a test of the empty menu System.out, println( ); try \{ rooms [END-ROOM] , display (); rooms [END ROOM], getChoice (kbd); * Main Menu 1. New 2. Open 3. Save 4. Quit * */ public void display(); 1 * Prompt the user to enter a menu choice and then reads the choice * entered by the user at the keyboard. If the method is called for a * menu that has zero options, an IllegalstateException is thrown. * The user is prompted for input, and is given three chances to enter * a valid choice. * Returns 1 if the reading fails for any reason. * eparam in the Scanner to read the choice from (usually on system.in) * ereturn the number of the option chosen; or 1 if no valid option * was chosen. * ethrows IllegalstateException if this Menu has no options. / public int getChoice(Scanner in)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logistics Lifeline Supply Chain Strategies

Authors: Ehsan Sheroy

1st Edition

7419377502, 978-7419377503

More Books

Students also viewed these Databases questions

Question

Are my points each supported by at least two subpoints?

Answered: 1 week ago