Homework 10 Designing Classes
|
Modified:
|
SI - Next session will give additional discussion of Homework 10.
Overview - The purpose of the homework is to provide practice in
responsibility-driven design and use of collections, specifically HashMaps. In
the class diagram at right, an Item class has been added to support a
room that contains items and a player that can carry items.
An item has a name for identification and weight. For example, an item could
be a soda with a weight of 500 grams.
A room can contain any number of items but only one of a particular item
(i.e. only one soda).
A player can take items from the current room they are in or drop
items in the room.
Getting started
- Open your Homework 9 project file as a starting point.
- Save project as Homework 10.
Assignment
- Create an Item class that has responsibility for the item data.
- Create a new class Item with name and weight fields,
- add an appropriate constructor to initialize the fields,
- add mutator and accessor methods for each field:
- getName(), getWeight(), etc.
Test by:
- Create an Item object,
- call the mutator and accessor methods
- The Room class is to be extended to hold multiple Items.
Room class will still have responsibility for all room data; a room
description, room exits and, now, room items. Examine the exits field
as a model.
- Add a field for holding items in the room. Assume each item is uniquely
named so that a HashMap can be used.
- Add void setItem(String name, Item theItem) method to place an item in the
room. The name serves as the key to the HashMap holding the room
items, and is the same as the name field of an Item.
- Add Item getItem(String name) method to not only return the named
item but also remove the item from the room. Locate documentation on
HashMap in the Help | Java Class Library for method remove.
- Add a method to return a String with the names of all items in the room.
Use getExitString() as a pattern.
- Similar to: "Items here: squirrel cookie"
- Extend the method to return the room description to include the names of
all items in the room.
- Similar to: "Exits: west east south Items here: squirrel cookie".
Positive test by:
- Create a Room object,
- create several Items,
- place the items in the room using setItem(),
- call Room method to return the names of all items in the room,
- get one of the items using getItems(). Inspect the object
reference result,
- call Room method to return room description with the names of all items
in the room to verify the item has been removed.
Negative test by:
- Create a Room object,
- call Room method to return the names of all items in the room,
- get one item using getItems() that is not in the room.
- The Player class is to be extended to have responsibility for the
player to carry any number of items.
The class diagram should appear as at right after the extension of Player.
- Add a field for the items carried. Assume each item is uniquely named so
that a HashMap can be used.
- Add boolean take(String name) method to take a named item from
the current room.
- Return false if the item is not in the current room.
- Remove the item from the current room and add to the items carried;
return true.
- Add boolean drop(String name) method to drop a named item carried
into the current room.
- Return false if the item is not carried.
- Remove the item from items carried and add to the items in the current
room; return true.
- Add a method to return a String with the names of all items carried. Use
Room getExitString() as a pattern.
- Similar to: "Items carried: squirrel cookie"
- Extend the method that returns the room description to include the names of
all items carried.
- Similar to: "Exits: west east south Items here: nuts candy Items
carried: squirrel cookie".
- Extend the createRooms() method to place unique items in each room.
Positive test by:
- Create a Player object,
- display the room description to verify the items in the current room,
- take one of the items,
- display the room description to verify the item has been removed from
the current room and is now carried,
- drop the item,
- display the room description to verify the item has been dropped in the
current room and is not carried.
Negative test by:
- Create a Player object,
- display the room description to verify the items in the current room,
- take an item not in the room,
- drop an item not carried.
- The Game is to be extended for the new commands take and
drop. These commands have a second word, the name of the item,
similar to the go command.
- Add take and drop to the CommandWords class for the command to
be parsed.
- Add the take and drop commands in the processCommand()
method of Game.
- Add tests of the take and drop commands to the test()
method.
Test Game class by:
- Create a Game object,
- move into several rooms,
- give the take and drop commands (test what happens on
take and drop of non-existent items),
- quit.
Test Test class by:
- Create a Test object,
- execute the test() method.
- (Extra credit) Implement an item command that prints out all the items carried by
the player and the total weight.
- 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 HW10 folder in
the C201 OnCourse DropBox. Copy the following:
- Test, Game, Player, Room, Item and CommandWords JAVA files.
- A Word file with Terminal Window printouts of a-f.