Homework 9 Designing Classes
|
Modified:
|
SI - Next session will give additional discussion of Homework 9.
Overview - The purpose of the homework is to provide practice in code
reuse and refactoring. In the class diagram at right, a Player and
Test class have been added, and the Game class has been simplified by
moving Room responsibility to the Player class.
Getting started
- Open chapter07/zuul-better project file.
- Save project as HW9.
Assignment
- Create a Test class to help in testing.
- Create a new class Test with responsibility for a Game
object,
- add a test() method that issues commands to move from room to
room, print help, and finally quit.
- Use the following Test class definition as a starting point:
public class Test
{
Game game = new Game();
public Test() {}
public void test()
{
game.processCommand(new
Command("go","south"));
game.processCommand(new
Command("go","north"));
game.processCommand(new
Command("quit",null));
System.out.println("Test done");
}
} |
- Change the Game method:
private boolean processCommand(Command command)
to:
public boolean processCommand(Command command)

Test by:
- Create a Test object,
- execute the test() method.
Should the game not quit properly after testing, you will not be able to
compile.
- Save the project, exit, and restart BlueJ or,
- Reset Machine by pressing Ctrl+Shift+R.
- Refactor the project to add a separate Player class.
A Player
object has responsibility for the player's current room.
- Create a new class Player,
- identify and factor out from Game class all Room class
objects and methods,
- place all the Room class objects, method definitions and
references factored out
of Game in Player class,
- in simple cases, the field or complete method can be cut-n-pasted from
Game to Player
- when a Room object is referenced in a Game method (e.g.
currentRoom), the code should be refactored to a new Player method
and that method called from Game.
- add a Player field to Game class,
- the class diagram should appear as at right.
- The arrows indicate that Game uses Player because of the
Player field you added to Game.
- To regenerate the arrows to show the current use:
- Right click on the arrow and Remove.
- Edit each class, making a trivial change, e.g. add a space at the end of
a line.
- Compile all the classes.
Test by:
- Create a Test object, execute the test() method.
- Remember that you can play the game by creating a Game object and
executing the play() method.
- When the player quits, automatically display the path followed throughout
the game.
- In Player, use an ArrayList to add the current room to the path each time the
player issues a valid go command,
- In Game, on the quit command, print the short description of each room in
the ArrayList,
- add a Player class method to print the path list of rooms
visited.
Test by:
- Create a Test object, execute the test() method.
- Add the back command. This command does not have a second word.
Entering the back command takes the player into the previous room.
- Add back to the CommandWords for the command to be parsed,
- add the back command in the Game processCommand() method,
- implement moving to the previous room in Player class which has
responsibility for the player's current room,
- don't forget to add the current room to the path when the back
command is processed,
- add tests of the back command to the test() method.
Test Game class by:
- Create a Game object,
- move into several rooms,
- give the back command (test what happens if back is
entered twice),
- quit.
Test Test class by:
- Create a Test object,
- execute the test() method.
- Refactor the back command to move repeatedly back
until the beginning room is reached.
Hints:
- Use an ArrayList, adding rooms when the go command is
issued and removing rooms when the back command is issued.
- Or use a Stack, you will need to find out about stacks through
the Java documentation.
Test by:
- Create a Game object,
- move into several rooms,
- give the back command (test what happens if back is
entered repeatedly),
- quit.
- Add to the Game class the method main() so that the program
can be run outside of BlueJ. The method follows:
public static void main(String args[])
{
( new Game() ).play();
} |
Test:
- Compile in BlueJ.
- Open a Command prompt window.
Start | Programs | Accessories | Command Prompt
- Change to the project directory using a command similar to:
- cd \Users\username\blueJ\Hw10
- To execute the Game, enter:
- Capture the game screen as it is being played and paste to a Word file.
Turn in
C201 OnCourse Dropbox - Create a HW9 folder in
the C201 OnCourse DropBox. Copy the following:
- Test, Game, Player and CommandWords class JAVA files.
- A Word file with Terminal Window printouts of a-e and Command Prompt
Window of f.