Question: Create a Class Called Room Implement a class to maintain information about a room. Rooms can have an unlimited number of neighbors in unique directions.
Create a Class Called Room
Implement a class to maintain information about a room. Rooms can have an unlimited number of neighbors in unique directions. For example, a room could have neighbors to the north, upstairs, outside, southwest or any other direction. Allowing an unknown number of neighbors requires a dynamic data structure. Use a HashMap described later in this document.
Class Fields
Declare meaningful names with appropriate data types for each of these private instance fields:
a description of the current room (String)
an optional item (Item)
a list of all adjacent Rooms (HashMap)
Methods
public Room (String pDescription) - a constructor that is passed the description. The rooms item is set to null.
public Room (String pDescription, Item pItem) - a constructor that is passed the description and the item.
provide get methods for the description and item
public void addItem (Item i) add the provided item to the room. If an item is already in the room it gets replaced by the new item.
public boolean hasItem () return true if the room has an item.
public void addNeighbor (String pDirection, Room r) add the provided room and corresponding direction to the HashMap of neighbors (see below).
public Room getNeighbor (String pDirection) return the adjacent room in the requested direction. Return null if there is no Room in that direction.
public Item removeItem () remove and return the rooms item. The instance variable for the rooms item is set to null. Warning: this can be a bit tricky!
public String getLongDescription () return a String that begins with You are followed by the room description. If there is an item in the Room, also include You see followed by the items description.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
