| CPSC 124 | Introduction to Programming | Fall 2006 |
In this lab, you'll continue writing object-oriented programs.
Create a lab08 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/f06/cs124/labs/lab08/ to your lab08 directory.
This lab covers Chapter 5 of the book. If you don't have your book with you, the book can be found online at http://math.hws.edu/javanotes5/.
Here are the exercises for this week's lab, due next Thursday.
In the file Game.java (in the lab08 directory), you will find three classes: Game, Player, and Room. This program represents a very simplistic game. In the game, the player can basically wander through various rooms. Both players and rooms are represented as objects. The Game class is the main class for running the program.
Here are some guidelines. A player can only be in one room at a time. If the room has a connecting room then the player can move to that room. A room may or may not have connecting rooms in the north side, south side, east side, or the west side. Each player and room has a name ("marc" or "closet", respectively), and each room also has a description ("a dusty closet"). There is no end goal in this game. A player simply moves from room to room. The moving of players is coded in the main() method in the class Game.
The room class should contain a constructor for setting the name and description. The room should contain variables representing the name (a string), description (a string), room to the north (a room object), room to the south (a room object), room to the east (a room object), and room to the west (a room object). If there is no connecting room in some direction (e.g., north), then that object should be null (see chapter 5 for details on null). Otherwise, it should be set to the appropriate object. These variables should not be directly accessible from outside the class. There should be the following getter and setter methods for reading and writing these variables:
getName() getDescription() getNorthRoom() getSouthRoom() getEastRoom() getWestRoom() /** * These setter methods take as input a variable of type Room * and sets the respective room to that room. */ setNorthRoom(..) setSouthRoom(..) setEastRoom(..) setWestRoom(..)
The player class should contain a constructor for setting the name and the current room. It should contain variables representing the name (a string) and the current room (a room object). These variables should not be directly accessible from outside the class. There should be the following getter and setter methods for reading and writing these variables:
getName() getCurrentRoom()
In addition, there should be the following methods for moving a player to a new room:
goNorth() goSouth() goWest() goEast()
These methods should only move the player if there is a connecting room (i.e., the connecting room is not null). Otherwise, it should not move the player. The methods should return a boolean indicating whether the player was moved or not.
When you're finished implementing Room and Player, you should run the program and make sure you get the following output:
Output:
bash$ java Game student.getCurrentRoom().getDescription()=lansing computer lab student.goNorth()=true student.getCurrentRoom().getDescription()=a dusty broom closet student.goWest()=false student.getCurrentRoom().getDescription()=a dusty broom closet
Finally, you should modify the main() method and add some new rooms and move the player around to these rooms. Comment your new code so I can clearly see what you have added.
Modify the file DMV.java to extend the Vehicle class as described in class. You should create three new classes that extend vehicle: Car, Truck, and Motorcycle. The Car class should contain a variable that indicates the number of doors, the Truck class should contain a variable that contains the number of axels, and the motorcycle should contain a boolean that indicates whether it has a sidecar. You should also write a print method in each of the three classes that prints out all the variables in that class. Finally, in the main() method in the class DMV, create some Car, Truck, and Motorcycle objects and print them out. You should also call the method transferOwnership() at least once.
Verify that your lab08 folder contains all of the files you created or modified for this lab, then copy your entire lab08 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).