Using inheritance for the World of Zuul project, create a TransporterRoom class
ID: 3817977 • Letter: U
Question
Using inheritance for the World of Zuul project, create a TransporterRoom class that inherits the Room class and transports you to another room
Some pieces of code that may help:
import java.util.Scanner;
public class Game
{
private Room currentRoom;
public Game()
{
createRooms();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theater, teleport;
// create the rooms
outside = new Room("outside *burp* the main entrance *burp* of the university");
theater = new Room("in a lecture theater");
teleport = new Room("in my space cruiser");
// initialise room exits
outside.setExit("east", theater);
theater.setExit("west", outside);
outside.setExit("north", teleport);
teleport.setExit("south", outside);
currentRoom = outside; //start game outside
}
/**
* Given a command, process (that is: execute) the command.
* @param command The command to be processed.
* @return true If the command ends the game, false otherwise.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
CommandWord commandWord = command.getCommandWord();
switch (commandWord) {
case UNKNOWN:
System.out.println("I don't know what you mean...");
break;
case GO:
goRoom(command);
setRoom();
break;
}
return wantToQuit;
}
/**
*Sets the current room for player if the description of
*the current location matches the room
*/
private void setRoom()
{
String str = currentRoom.getShortDescription().toString();
if (!(str.equals(null)))
{
if (str.equals("in a lecture theater"))
player.setCurrentRoom("theater");
else if (str.equals("outside *burp* the main entrance *burp* of the university"))
player.setCurrentRoom("outside");
else if (str.equals("in my space cruiser"))
{
player.setCurrentRoom("teleport");
}
}
else
return;
}
/**
* Try to go in a direction. If there is an exit, enter the new
* room, otherwise print an error message.
* @param command User input the direction desired
*/
private void goRoom(Command command)
{
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
currentRoom = nextRoom;
}
}
import java.util.Set;
import java.util.HashMap;
public class Room
{
private String description;
public HashMap<String, Room> exits; // stores "exits" of Room class
/**
* Create a room
* @param description The room's description
*/
public Room(String description)
{
this.description = description;
exits = new HashMap<>();
}
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public String getShortDescription()
{
return description;
}
public Room getExit(String direction)
{
return exits.get(direction);
}
}
Explanation / Answer
Ans:
Programs:
// TransporterRoom.java //
public class TransporterRoom extends Room // inheritance(extends) room
{
private Scenario scene;
// Create a room that can beam you anywhere.
public TransporterRoom(String description, Scenario scene)
{
super(description);
this.scene = scene;
}
// Exits
// clear exits
private String getExitString()
{
return "You feel quite dizzy. Something is strange. " +
"You cannot really see the exits...";
}
// Return the room that is reached if we go from this room in direction
//direction-- return null ff there is no room in that direction
public Room getExit(String direction)
{
return scene.getRandomRoom();
}
}
// end //
room.java
// this you can use as example room //
import java.util.Set;
import java.util.HashMap;
// import util_iterator
import java.util.Iterator;
// room_class
class Room
{
// String_Descrptn
private String description;
// exits- hash_map
private HashMap exits; // stores_exits of room.
public Room(String description)
{
this.description = description;
exits = new HashMap();
}
// exit
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public String getShortDescription()
{
return description;
}
public String getLongDescription()
{
return "You are " + description + ". " + getExitString();
}
private String getExitString()
{
String returnString = "Exits:";
Set keys = exits.keySet();
for(Iterator iter = keys.iterator(); iter.hasNext(); )
returnString += " " + iter.next();
return returnString;
}
public Room getExit(String direction)
{
return (Room)exits.get(direction);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.