View the step-by-step solution to:

I am asked to overridden the playerRoom method and reuse switch in the sleepRoom class and invoke the method from the CustomPlayer class.

I am asked to overridden the playerRoom method and reuse switch in the sleepRoom class and invoke the method from the CustomPlayer class. I found that when I run it I cannot get the correct answer when I type "sleep" as a command. (The Room and Player class cannot be modified.)


the output I got:


You are in the Red Room

What would you like to do?

go north

You are in the Sleep Room

What would you like to do?

sleep

Invalid command. Type help for details.



the output should be:


You are in the Red Room

What would you like to do?

go south

You are in the Sleep room.

What would you like to do?

sleep

Ah. Good Idea! Before you lay down ...

You have 22.0 points

You awake after 3 hours, and then ...

You have 34.3 points

What would you like to do?


The code:

import java.util.StringTokenizer;

import java.util.Scanner;

// class for a generic player

public class Player {


   // fields

   private int roomNum;

   private String name;

   private String playerInventory;

   private boolean continuePlay;


   // constructor

   public Player(String name, int room, String inventory) {

       this.name = name;

       roomNum = room;

       continuePlay = true;

       playerInventory = inventory;

   }


   // getter method to return player's inventory

   public String getInventory() {

       return playerInventory;

   }


   // getter function to get listing of inventor

   public void printInventory() {

       StringTokenizer strT = new StringTokenizer(playerInventory, ";");

       System.out.println(name + "'s items: ");

       int numItems = 0;

       while (strT.hasMoreTokens()) {

           System.out.println(" " + strT.nextToken());

           numItems++;

       }

       System.out.println(" A total of " + numItems + " item(s)");

   }


   // setter function

   public void setContinuePlay(boolean continuePlay) {

       this.continuePlay = continuePlay;

   }


   // getter function

   public boolean getContinuePlay() {

       return continuePlay;

   }


   // getter function

   public String getName() {

       return name;

   }


   // add an item to user's inventory

   public void addToInventory(String item) {

       playerInventory = Utilities.addToString(item, playerInventory);

   }


   // remove item from user's inventory

   public void removeFromInventory(String item) {

       playerInventory = Utilities.removeFromList(item, playerInventory);

   }

}



class CustomPlayer extends Player {

   private double vitalityPoints;


   public CustomPlayer(String name, int room, String inventory, double vitalityPoints) {

       super(name, room, inventory);

       this.vitalityPoints = vitalityPoints;


   }

   protected void addToVitalityPoints(double pointsToAdd){




   }

   public void printVitalityPoints() {


     double pointsToAdd=14.3;

     double sum = pointsToAdd + this.vitalityPoints;

     System.out.println("Ah. Good Idea! Before you lay down ... ");

     System.out.println("You have"+ this.vitalityPoints +"points ");

     System.out.println("You awake after 3 hours, and then ... ");

     System.out.println("You have"+ sum+" points ");


   }


   public void printInventory() {

       StringTokenizer strT = new StringTokenizer(getInventory(), ";");

       System.out.println(getName() + "'s items: ");

       int numItems = 0;

       while (strT.hasMoreTokens()) {

           System.out.println(" " + strT.nextToken());

           numItems++;

       }

       System.out.println(" A total of " + numItems + " item(s)");

       System.out.println("Vitality:n"+vitalityPoints+" points");


       }



}



// import statements





// The superclass with fields and methods

// for a generic game room

 class Room {


   // fields

   private String roomObjects;

   private String roomName;

   private String roomExits;

   private int roomNum;


   // The playRoom method can be invoked with either

   // a CustomPlayer object OR a Player object. They

   // both invoke the same method, getNextRoom   

   public int playRoom(CustomPlayer player) {

       return getNextRoom(player);

   }


   public int playRoom(Player player) {

       return getNextRoom(player);

   }


   private int getNextRoom(Player player) {


       // inform the player of his/her current room

       System.out.println("You are in the " + roomName);

       System.out.println("What would you like to do?");


       // variables for player interaction

       String command;

       int newRoomNum = roomNum;

       Scanner keyboard = new Scanner(System.in);

       command = keyboard.nextLine();


       // switch on the user's command

       switch (command) {

           case "exits":

               printListOfExits();

               break;

           case "look":

               printObjectsInRoom();

               break;

           case "inventory":

               player.printInventory();

               break;

           case "go west":

           case "go east":

           case "go south":

           case "go north":

               newRoomNum = leaveRoom(command);

               break;

           case "quit":

               player.setContinuePlay(false);

               break;

           case "pick up":

               userPicksUp(player);

               break;

           case "drop":

               userDrops(player);

               break;

           case "help":

               Utilities.printHelp();

               break;

           default:

               System.out.println("Invalid command. Type help for details.");

       }

       return newRoomNum;

   }


   // transfer item from room to user's inventory

   protected void userPicksUp(Player player) {

       System.out.println("What would you like to pick up?");

       Scanner keyboard = new Scanner(System.in);

       String itemToPickUp = keyboard.nextLine();

       if (Utilities.isItemInContainer(itemToPickUp, roomObjects)) {

           player.addToInventory(itemToPickUp);

           roomObjects = Utilities.removeFromList(itemToPickUp, roomObjects);

       } else {

           System.out.println("That item is not in the room");

       }

   }


   // transfer item from user's inventory to room     

   protected void userDrops(Player player) {

       System.out.println("What would you like to drop?");

       Scanner keyboard = new Scanner(System.in);

       String itemToDrop = keyboard.nextLine();

       if (Utilities.isItemInContainer(itemToDrop, player.getInventory())) {

           player.removeFromInventory(itemToDrop);

           roomObjects = Utilities.addToString(itemToDrop, roomObjects);

       } else {

           System.out.println("You cannot drop something that you don't have");

       }

   }


   // constructor  

   public Room(String roomName, String objects, int roomNum, String listOfExits) {

       this.roomName = roomName;

       this.roomObjects = objects;

       this.roomNum = roomNum;

       this.roomExits = listOfExits;

   }


   // getter function

   public String getRoomName() {

       return roomName;

   }


   // getter function

   public String getRoomObjects() {

       return roomObjects;

   }


   // getter function

   public int getRoomNum() {

       return roomNum;

   }


   // add an item to room's inventory

   public void addToObjects(String item) {

       roomObjects = Utilities.addToString(item, roomObjects);

   }


   // remove item from rooms's inventory

   public void removeFromObjects(String item) {

       roomObjects = Utilities.removeFromList(item, roomObjects);

   }


   // determine new room number if user proceeds through an exit

   public int leaveRoom(String command) {


       StringTokenizer exitsT = new StringTokenizer(roomExits, ";");

       int exitRoom = -1;


       if (command.equals("go west")) {

           while (exitsT.hasMoreTokens()) {

               String nextToken = exitsT.nextToken();

               if (nextToken.substring(0, 1).equals("w")) {

                   exitRoom = Integer.parseInt(nextToken.substring(1));

               }

           }

       } else if (command.equals("go east")) {

           while (exitsT.hasMoreTokens()) {

               String nextToken = exitsT.nextToken();

               if (nextToken.substring(0, 1).equals("e")) {

                   exitRoom = Integer.parseInt(nextToken.substring(1));

               }

           }

       } else if (command.equals("go north")) {

           while (exitsT.hasMoreTokens()) {

               String nextToken = exitsT.nextToken();

               if (nextToken.substring(0, 1).equals("n")) {

                   exitRoom = Integer.parseInt(nextToken.substring(1));

               }

           }

       } else if (command.equals("go south")) {

           while (exitsT.hasMoreTokens()) {

               String nextToken = exitsT.nextToken();

               if (nextToken.substring(0, 1).equals("s")) {

                   exitRoom = Integer.parseInt(nextToken.substring(1));

               }

           }


       }

       if (exitRoom == -1) {

           System.out.println("That exit is not available");

           return roomNum;

       } else {

           return exitRoom;

       }

   }


   // getter function to list items in room

   public void printObjectsInRoom() {


       StringTokenizer strT = new StringTokenizer(roomObjects, ";");

       System.out.println("Items in " + roomName);

       int numItems = 0;

       while (strT.hasMoreTokens()) {

           System.out.println(" " + strT.nextToken());

           numItems++;

       }

       System.out.println(" A total of " + numItems + " item(s)");

   }


   // getter function to get list of exits

   public void printListOfExits() {


       StringTokenizer exitsT = new StringTokenizer(roomExits, ";");

       while (exitsT.hasMoreTokens()) {

           String nextToken = exitsT.nextToken();

           if (nextToken.substring(0, 1).equals("n")) {

               System.out.println("There is an exit to the north");

           } else if (nextToken.substring(0, 1).equals("e")) {

               System.out.println("There is an exit to the east");

           } else if (nextToken.substring(0, 1).equals("s")) {

               System.out.println("There is an exit to the south");

           } else if (nextToken.substring(0, 1).equals("w")) {

               System.out.println("There is an exit to the west");

           }

       }

   }

}





class Utilities {


   public static String addToString(String item, String list) {

       StringBuilder listSB = new StringBuilder(list);

       listSB.append(";" + item);

       return listSB.toString();

   }


   public static boolean isItemInContainer(String item, String container) {

       StringTokenizer strT = new StringTokenizer(container, ";");

       while (strT.hasMoreTokens()) {

           String nextToken = strT.nextToken();

           if (nextToken.equals(item)) {

               return true;

           }

       }

       return false;

   }


   public static String removeFromList(String item, String list) {

       String newList = "";

       StringTokenizer listT = new StringTokenizer(list, ";");

       while (listT.hasMoreTokens()) {

           String nextToken = listT.nextToken();

           if (!nextToken.equals(item)) {

               newList += nextToken + ";";

           }

       }

       return newList;

   }


   public static void printHelp() {

       System.out.println("The commands that you can use: ");

       System.out.println(" exits     - will print out all exits from current room");

       System.out.println(" look      - will display all (visible) items in the room");

       System.out.println(" inventory - list the items that you are carrying");

       System.out.println(" go dir    - exit the room, where dir is east, west, north or south");

       System.out.println(" quit      - quit the game");

       System.out.println(" pick up   - pick up an item (another question will display)");

       System.out.println(" drop      - drop an item (another question will display)");

       System.out.println(" help      - this menu.");

       System.out.println(" sleep     - not work in all rooms.");

   }

}




class SleepRoom extends Room{


  public SleepRoom(String roomName, String objects, int roomNum, String listOfExits){

  super(roomName, objects, roomNum, listOfExits);

  }


  @Override

  public int playRoom(CustomPlayer player){

  System.out.println("You are in the Sleep room ");

       System.out.println("What would you like to do?");

       String command;

       int newRoomNum = 4;

       Scanner keyboard = new Scanner(System.in);

       command = keyboard.nextLine();


     switch (command) {

           case "exits":

               printListOfExits();

               break;

           case "look":

               printObjectsInRoom();

               break;

           case "inventory":

               player.printInventory();

               break;

           case "go west":

           case "go east":

           case "go south":

           case "go north":

               newRoomNum = leaveRoom(command);

               break;

           case "quit":

               player.setContinuePlay(false);

               break;

           case "pick up":

               userPicksUp(player);

               break;

           case "drop":

               userDrops(player);

               break;

           case "help":

               Utilities.printHelp();

               break;

           case "sleep":

                sleepOnStraw(player);    

               break;

           default:

               System.out.println("Invalid command");

       }

       return newRoomNum;

       }






  public void sleepOnStraw(CustomPlayer player){


     System.out.println("Hello");

     player.printVitalityPoints();

              }




}



class GameV4{


      public static void main(String args[]) {


       // designate how many rooms are in the game

       final int NUM_ROOMS = 6;


       // declare array of rooms

       Room[] gameBoard = new Room[NUM_ROOMS];


       // populate the room array, using the superclass Room

       gameBoard[0] = new Room("Red Room", "1 apple;pumpkin", 0, "e4;w4;n4;s4");

       gameBoard[1] = new Room("Blue Room", "3 grapes;barking Germany Shephard", 1, "w0;e2");

       gameBoard[2] = new Room("Green Room", "meowing kittengo", 2, "w1;e3");

       gameBoard[3] = new Room("Entry Room", "smelly steak", 3, "w4;e4");

       gameBoard[5] = new Room("Telephone Room", "fish", 4, "n1;e0");

       gameBoard[4] = new SleepRoom("Sleep Room","lay down",4,"n2");





       // keep track of which room player is in

       int playerRoomNumber = 0;


       // create player object, using the superclass Player

       Player player = new Player("Tatiana", playerRoomNumber, "bubble gum;band-aid;toothpick");

        player = new CustomPlayer("Filip's", playerRoomNumber, "bubble gum",22.0);


       // play the game, as long as player object has field continuePlay set to true

       while (player.getContinuePlay() == true) {

           playerRoomNumber =

                   gameBoard[playerRoomNumber].playRoom(player);

       }

   }


}

Top Answer

The output after correction is: You are in the Red Room What would you like to do? go south You... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online