Find and fix the error in the below Program import java . io . BufferedReader; import java
Question:
Find and fix the error in the below Program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
publicclassWonderland {
staticActions actions;
publicstaticvoidmain(String[] args)throwsIOException{
BufferedReader in;
String input;
String output = "";
actions = new Actions();
in = new BufferedReader(new InputStreamReader(System.in));
actions.showIntro();
do {
System.out.print("> ");
input = in.readLine();
output = actions.runCommand(input);
System.out.println(output);
} while (!"q".equals(input));
}
}
import java.util.*;
publicclassActions {
privateArrayList map;
privatePlayer player;
publicActions() {
this.map = new ArrayList();
ItemList gardenList = new ItemList();
gardenList.add(new Item("key", "What a shiny Key, it must be useful"
, true, false, false));
gardenList.add(new Item("hat", "It's the Crazy Mad Hatters Hat, not much value, but I like it!"
, true, false, false));
gardenList.add(new Item("mushroom", "I probably shouldn't experiment"
, true, false, false));
ItemList safeRoomList = new ItemList();
safeRoomList.add(new Item("","", false , false, false));
ItemList courtRoomList = new ItemList();
courtRoomList.add(new Item("chromosphere", "This looks ancient!"
, true, true, false));
courtRoomList.add(new Item("sword", "With this I can protect myself!"
, true, false, false));
ItemList croquetList = new ItemList();
croquetList.add(new Item("card", "The Queens Favourite Card, I could use itfor leverage."
, true, false, false));
croquetList.add(new Item("oraculum", "This map seems to have ink that moves, showing some strange scenes."
, true, false, false));
ItemList duchessList = new ItemList();
duchessList.add(new Item("grinder", "A pepper grinder, never know when I'll need a perfect sandwich, this pepper might be yummy."
, true, false, false));
duchessList.add(new Item("cup", "A Fancy China Cup."
, true, false, false));
ItemList rabbitHouseList = new ItemList();
rabbitHouseList.add(new Item("watch", "An expensive pocket watch that the Rabbit forgot, might need to track the time later."
, true, false, false));
ItemList rabbitHoleList = new ItemList();
rabbitHoleList.add(new Item("bottle", "A bottle with DrinkMeBottle written on it. Should I drink this?",
true, false, true));
rabbitHoleList.add(new Item("cake", "A cake with EatMeCake written on it. Should I eat this?",
true, false, true));
ItemList playerlist = new ItemList();
map.add(new Locations("in the Rabbit Hole","This is where the Rabbit went. I can't believe I fit through the hole.",
"Door Knob" ," Welcome to Wonderland, to leave you must find the Chromosphere located in one of the rooms. Activate it and find your way back home. But first you must get through me, choose wisely between the bottle and the cake!", Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, 1,rabbitHoleList));
map.add(new Locations("at the Rabbit House", "A mushroom Shaped House with that talking Rabbit from before.",
"White Rabbit", " I'm late, I'm Late. The Queen will have my head! You there strange creature, get out of my house!!!I'm Late, I'm Late.",Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, 2, Directions.NOEXIT, rabbitHouseList));
map.add(new Locations("in the Garden", "A beautiful smelling garden.", "Mad Hatter", "Oh, what a delightful child! " + "This is an unbirthday party!",Directions.NOEXIT, Directions.NOEXIT, 1, 3, Directions.NOEXIT, gardenList));
map.add(new Locations("at the Duchess House", "A House with Black pepper inthe air, I hear sneezing.","Duchess", "Achoo Oh Hello there.",4, 5, 2, 6, Directions.NOEXIT, duchessList));
map.add(new Locations("in the Court Room", "What a glamorous Cour Room, That must be the King on the throne.",
"Red King", "I don't like the look of it at all, said the King: "+"however, it may kiss my hand, if it likes. ",Directions.NOEXIT, 3, Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, courtRoomList));
map.add(new Locations("in the Safe Room", "A safe room where you won't be found.","Whisper","Come home!",3, Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT, Directions.NOEXIT,safeRoomList));
map.add(new Locations("at the Croquet PlayGround", "A playground for the rich. The Queen seems a bit stuck up.","Queen of Hears", "Where is my favourite card? ...Now, where do you come from? I warn you, child... if I lose my temper, you lose your head!What do you know about this unfortunate affair regarding my card?",Directions.NOEXIT, Directions.NOEXIT, 3, Directions.NOEXIT, Directions.NOEXIT, croquetList));
player = new Player("player", "a loveable game-player", playerlist,
map.get(0));
}
privateArrayList getMap() {
return map;
}
privatevoidsetMap(ArrayList aMap) {
map = aMap;
}
privatePlayer getPlayer() {
return player;
}
privatevoidsetPlayer(Player aPlayer) {
player = aPlayer;
}
privatevoidtransferOb(Item t, ItemList fromlist, ItemList tolist) {
fromlist.remove(t);
tolist.add(t);
}
String takeOb(String obname) {
String retStr = "";
Item t = player.getLocation().getItem().thisOb(obname);
if (obname.equals("")) {obname = "nameless object";// if no object specified
}
if (t == null) {
retStr = "There is no " + obname + " here!";
} else {
transferOb(t, player.getLocation().getItem(), player.getItem());
retStr = obname + " taken!";
}
return retStr;
}
String dropOb(String obname) {
String retStr = "";
Item t = player.getItem().thisOb(obname);
if (obname.equals("")) {
retStr = "You'll have to tell me which object you want to drop!";// ifno object specified
} else if (t == null) {
retStr = "You haven't got one of those!";
} else {
transferOb(t, player.getItem(), player.getLocation().getItem());
retStr = obname + " dropped!";
}
return retStr;
}
// move a Player to a location.
privatevoidmoveActorTo(Player p, Locations aRoom) {
p.setLocation(aRoom);
}
// move the player in direction
privateintmoveTo(Player anPlayer, Directions directions) {
Locations r = anPlayer.getLocation();
intexit;
switch (directions) {
case NORTH:
exit = r.getN();
break;
case SOUTH:
exit = r.getS();
break;
case EAST:
exit = r.getE();
break;
case WEST:
exit = r.getW();
break;
case DOWN:
exit = r.getDown();
break;
default:
exit = Directions.NOEXIT;// noexit - stay in same room
break;
}
if (exit != Directions.NOEXIT) {
moveActorTo(anPlayer, map.get(exit));
}
return exit;
}
voidmovePlayerTo(Directions directions) {
// if roomNumber = NOEXIT, display a special message, otherwise
// display text (e.g. name and description of room)
if (moveTo(player, directions) == Directions.NOEXIT) {
showStr("No Exit!");
} else {
look();
}
}
voidgoN() {
movePlayerTo(Directions.NORTH);
}
voidgoS() {
movePlayerTo(Directions.SOUTH);
}
voidgoW() {
movePlayerTo(Directions.WEST);
}
voidgoE() {
movePlayerTo(Directions.EAST);
}
voidgoD(){
movePlayerTo(Directions.DOWN);
}
String consumeItem(String obname) {
String retStr = "";
Item t = player.getItem().thisOb(obname);
if (obname.equals("")) {
retStr = "You'll have to tell me which object you want to consume!";//if no object specified
} else if (t == null) {
retStr = "You haven't got one of those!";
} else if (t.isConsumable()) {
System.out.println("You consume " + obname);
System.out.println("Your Start to feel strange, the world around you is spinning, " +"and your find yourself in a new place. ");
transferOb(t,player.getItem(), player.getLocation().getItem());
movePlayerTo(Directions.DOWN);
}
else {
System.out.println("You cannot consume that!");
}
return retStr;
}
String activateItem(String obname) {
String retStr = "";
Item t = player.getItem().thisOb(obname);
if (obname.equals("")) {
retStr = "You'll have to tell me which object you want to activate!";// if no object specified
} else if (t == null) {
retStr = "You haven't got one of those!";
} else if (t.isActive()) {
System.out.println("You Activate " + obname);
System.out.println("Lights seem to spin, and you feel disoriented. You wake up in the park, back in your world. Good Bye! ");
System.exit(0);
}
else {
System.out.println("You cannot consume that!");
}
return retStr;
}
voidlook() {
showStr(" You are " + getPlayer().getLocation().describe());
}
voidshowStr(String s) {
System.out.println(s);
}
voidshowInventory() {
showStr("You have: " + getPlayer().getItem().describeThings());
}
voidshowMap() {
showStr("Map: tDuchess House: Central. tCourt Room: North tCroquet Playground: East tSafe Room: South tGarden: West tRabbit's House: West");
}
privateString parseCommand(List wordlist) {
String msg;
if (wordlist.size() == 1) {
msg = Control.Actions(wordlist);
} else if (wordlist.size() == 2) {
msg = Control.Actions(wordlist);
} else {
msg = "Only 2word commands allowed!";
}
return msg;
}
privatestaticList wordList(String input) {
String delims = "[ t,.:;?!"']+";
List strlist = new ArrayList<>();
String[] words = input.split(delims);
for (String word : words) {
strlist.add(word);
}
return strlist;
}
publicvoidshowIntro() {
String s;
s = " Welcome to the Game of Alice in Wonderland. Instructions: [Enter n,s, w or e] to move, "
+ "(or enter q to quit)." + " You can also interact with objects by typing take, drop, activate, and eat."
+ " ---------------------------------------------------------------------"
+ " You wake up in a daze and see a White Rabbit rush past, saying I'm late, I'm Late' "
+ "He jumps straight into a Rabbit Hole "
+ "and you follow without thinking. ";
showStr(s);
look();
}
publicString runCommand(String inputstr) {
List wordlist;
String s = "ok";
String lowstr = inputstr.trim().toLowerCase();
if (!lowstr.equals("q")) {
if (lowstr.equals("")) {
s = "You must enter a command";
} else {
wordlist = wordList(lowstr);
s = parseCommand(wordlist);
}
}
return s;
}
publicstaticbooleancontains(String verb) {
return false;
}
}
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.*;
publicclassControl {
publicstaticString Actions(List wordlist) {
String noun;
String verb;
String msg = "";
verb = wordlist.get(0);
noun = wordlist.get(1);
if (!Actions.contains(noun)) {
msg = noun + " is not a known noun! ";
}
if (!Item.contains(noun)) {
msg += (noun + " is not a known noun!");
}
else {
switch (noun) {
case "take":
msg = Wonderland.actions.takeOb(noun);
break;
case "drop":
msg = Wonderland.actions.dropOb(noun);
break;
case "eat":
case "drink":
case "consume":
msg = Wonderland.actions.consumeItem(noun);
case "activate":
msg = Wonderland.actions.activateItem(noun);
default:
msg += "";
break;
}
}
if (!Actions.contains(verb)) {
msg = verb + " is not a known verb! ";
} else {
switch (verb) {
case "n":
case "north":
Wonderland.actions.goN();
break;
case "s":
case "south":
Wonderland.actions.goS();
break;
case "w":
case "west":
Wonderland.actions.goW();
break;
case "e":
case "east":
Wonderland.actions.goE();
break;
case "l":
case "look":
Wonderland.actions.look();
break;
case "inventory":
case "i":
Wonderland.actions.showInventory();
break;
case "m":
case "map":
Wonderland.actions.showMap();
default:
break;
}
}
return msg;
}
}
publicenumDirections {
NORTH,
SOUTH,
EAST,
WEST,
DOWN,;
publicstaticfinalintNOEXIT = -1;
}
publicclassInventoryextendsItem {
privateItemList items = new ItemList();
publicInventory(String aName, String aDescription, ItemList tl) {
super(aName, aDescription);
items = tl;
}
publicInventory(String aName, String aDescription,booleancanTake,
booleancanConsume,booleancanActive, ItemList tl) {
super(aName, aDescription, canTake, canActive, canConsume);
items = tl;
}
publicItemList getItem() {
return items;
}
publicvoidsetItem(ItemList items) {
this.items = items;
}
}
publicclassItem {
// Basic Thing type that defines all objects in the Adventure
privateString name;
privateString description;
privatebooleantakable;
privatebooleanactive;
privatebooleanconsumable;
publicItem(String aName, String aDescription) {
// constructor
this.name = aName;
this.description = aDescription;
}
publicItem(String aName, String aDescription,booleancanTake,boolean
canActive,booleancanConsume) {
// constructor
this.name = aName;
this.description = aDescription;
this.takable = canTake;
this.active = canActive;
this.consumable = canConsume;
}
publicString getName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicString getDescription() {
return description;
}
publicvoidsetDescription(String description) {
this.description = description;
}
publicbooleanisTakable() {
return takable;
}
publicvoidsetTakable(booleantakable) {
this.takable = takable;
}
publicbooleanisActive() {
return active;
}
publicvoidsetActive(booleanactive) {
this.active = active;
}
publicbooleanisConsumable() {
return consumable;
}
publicvoidsetConsumable(booleanconsumable) {
this.consumable = consumable;
}
publicString describe(){
return name + " " + description;
}
publicstaticbooleancontains(String noun) {
return false;
}
}
import java.util.ArrayList;
publicclassItemListextendsArrayList {
publicString describeThings() {
String s = "";
if (this.size() == 0) {
s = "";
} else {
for (Item t : this) {
s = s + t.getName() + ": " + t.getDescription() + " ";
}
}
return s;
}
publicItem thisOb(String aName) {
Item athing = null;
String thingName = "";
String aNameLowCase = aName.trim().toLowerCase();
for (Item t : this) {
thingName = t.getName().trim().toLowerCase();
if (thingName.equals(aNameLowCase)) {
athing = t;
}
}
return athing;
}
}
publicclassLocationsextendsInventory {
privateintn, s, w, e, down;
privateString charDescription;
privateString charName;
publicLocations(String aName, String aDescription, String cName, String aCDescription,intaN,intaS,intaW,intaE,intaDown,ItemList tl) {
super(aName, aDescription, tl);
this.charDescription = aCDescription;
this.charName = cName;
this.n = aN;
this.s = aS;
this.w = aW;
this.e = aE;
this.down = aDown;
}
publicintgetN() {
return n;
}
publicvoidsetN(intn) {
this.n = n;
}
publicintgetS() {
return s;
}
publicvoidsetS(ints) {
this.s = s;
}
publicintgetW() {
return w;
}
publicvoidsetW(intw) {
this.w = w;
}
publicintgetE() {
return e;
}
publicvoidsetE(inte) {
this.e = e;
}
publicintgetDown() {
return down;
}
publicvoidsetDown(intdown) {
this.down = down;
}
publicString getCharName(){return charName;}
publicString getCharDescription() {
return charDescription;
}
publicString describe() {
String roomdesc;
String thingsdesc;
roomdesc = String.format("%s. %s %s: "%s."", getName(),
getDescription(), getCharName(), getCharDescription());
thingsdesc = getItem().describeThings();
if (!thingsdesc.isEmpty()) {
roomdesc += " >>>><<<<"+" Items here: " + thingsdesc;
}
return roomdesc;
}
}
publicclassPlayerextendsInventory{
privateLocations location;// the Room where the Person is at present
publicPlayer(String aName, String aDescription, ItemList tl, Locations aRoom)
{
super(aName, aDescription, tl);// init super class
this.location = aRoom;
}
publicvoidsetLocation(Locations aRoom) {
this.location = aRoom;
}
publicLocations getLocation() {
return this.location;
}
}
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp