Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java problem GIVEN=======================room.java======================================= package hw; public class Room { String name; String description; public String getName(){ return name; } public void setName(String newName){ name =

Java problem

GIVEN=======================room.java=======================================

package hw;

public class Room {

String name;

String description;

public String getName(){

return name;

}

public void setName(String newName){

name = newName;

}

public String getDescription(){

return description;

}

public void setDescription(String newDescription){

description = newDescription;

}

public String toString() {

return "Name: " + name + ", Description: " + description;

}

Exit[][] exits = new Exit [100][100];

Item[][] items = new Item [100][100];

}

=====================================ITEM.java========================================

package hw;

public class Item {

String name;

String description;

int weight;

public String getName(){

return name;

}

public String getDescription(){

return description;

}

public int getWeight(){

return weight;

}

}

=======================================Exit.java=========================

package hw;

public class Exit {

String shortName;

String longName;

Room target;

public String getshortName(){

return shortName;

}

public String getlongName(){

return longName;

}

public Room getRoomTarget(){

return target;

}

}

================================Main==============================

package hw;

import java.util.Arrays;

public class World {

public static void main(String []args) {

int columns = 4;

int rows = 4;

Room aRoom = null;

Room[][] room = new Room[columns][rows];

aRoom = new Room();

aRoom.setName("Room A");

aRoom.setDescription("Huge");

room[0][0] = aRoom;

aRoom = new Room();

aRoom.setName("Room B");

aRoom.setDescription("Small");

room[0][1] = aRoom;

aRoom = new Room();

aRoom.setName("Room C");

aRoom.setDescription("Medium");

room[0][2] = aRoom;

aRoom = new Room();

aRoom.setName("Room E");

aRoom.setDescription("Gigantic");

room[0][3] = aRoom;

System.out.println(Arrays.deepToString(room));

Room currentRoom = null;

Item[] inventory = new Item[100];

}

}

After the initial configuration of the Room objects, add another Room variable called currentRoom , an array of Item variables called inventory , and a while loop. Set the value of currentRoom to the first Room object in your array of Rooms. The loop should continue until the user inputs the command exit . Within the loop, add code (probably a series of if statements operating on the user input) which performs the following tasks: 1. Print the name and description of the currentRoom variable, and the names of any Exit and Item objects it contains. 2. Get a String from the user (remember the Scanner method .nextLine() ) 3. Split the string into substrings, one for each word. That is to say, convert the string into tokens using the space character as a delimiter. 4. Using if statements and for loops as necessary, a. Loop over each Exit object in the currentRoom variable. Remember that each Exit has a Room variable called target , which should have been initialized in the beginning of the main function. Compare the first word in the input string to each target Room objects short AND long name. If there is a match, set the currentRoom variable to the matched Room . b. Otherwise (else if ), if the first word in the input string is equal to get , loop over each Item object in the currentRoom variable. Compare the second word in the input string to the name of the Item object. If there is a match, remove the Item from the currentRoom and add it to the inventory array. You may find that this requires some effort. c. Otherwise (else if ), if the first word in the input string is equal to look , display the name and description of the currentRoom variable, and the names of any Exit and Item objects it contains (again). d. Otherwise (else if) , if the first word in the input string is equal to inventory , print the names of the Item objects in the inventory variable

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions