View the step-by-step solution to:

Is there any mistakes when I write the switch?

Is there any mistakes when I write the switch?I type the sleep command but it does not invoke the method and shows ""Invalid command. Type help for details.""



import java.util.Scanner;

public class SleepRoom extends Room{


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

  super(roomName, objects, roomNum, listOfExits);

  }


  public int playRoom(CustomPlayer player){

  String command;

  int newRoomNum = roomNum;

  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. Type help for details.");

       }

       return newRoomNum;

       }





  protected void sleepOnStraw(CustomPlayer player){

     System.out.println("Hello");

     player.printVitalityPoints();

              }




}





import java.util.StringTokenizer;

public 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");


       }



}




public 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[3] = new Room("First Room", "1 apple;pumpkin", 3, "n5;w2");

       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[0] = new Room("Red Room", "smelly steak", 0, "n4;e4;w4;s4");

       gameBoard[5] = new Room("Telephone Room", "fish", 5, "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);

       }

   }


}

Recently Asked Questions

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