ITEC 120
L09a: Room

Objectives

To successfully complete this lab you will develop classes that represents a room. You will declare and access private instance variables and you will develop a con­structor, getter and setter methods, and toString method.

Assignment

In the last lab, you created a class that represents a Treasure. In this lab, you will create a class that represents a Room. A treasure has 4 properties: name, description, weight, and value. Those 4 properties are represented by the instance data in the Treasure class, and they were two Strings, and int, and a double.

A room has three properties:

But wait!

A room has, as a property, another object! There is a treasure in a room. Before you go any further, you need to understand pointers, which is a powerful programming concept.

Pointers

In TreasureTest.java, you might have declared a variable of type Treasure, thus:

Treasure t1;

The variable t1 stores the address of a treasure. Note that we have not created a treasure. We have only created a reference to a treasure. t1 is a variable – it may refer to any treasure. It is helpful to think of a reference as a pointer. The variable t1 points to a treasure. You may also think of t1 as a handle to a treasure object.

Null

The variable above, t1, has been declared but not initialized. In other words, it doesn't yet point to a treasure. Object reference variables in JAVA are, by default, initialized to null. null is a JAVA reserved word, and you can set a reference variable to null if you want to.

Treasure t1;

is the same thing as:

Treasure t1 = null;

Initialization

To have t1 point to a treasure, we could do this:

t1 = new Treasure("Key", "A Golden Key", 4.5, 8);

In the above line of code, a new Treasure is created in memory, and the address of that treasure is assigned to the variable t1. A picture of this treasure could look like this:

The arrow represents an address in memory. t1 does not actually contain a treasure; it contains an address of a treasure.

Now back to the Room lab

Create a class called Room with with three properties that are only accessible from within the class.

Constructors, Getters, Setters, and toString()

Create a constructor for Room. Have the caller pass a value for all of the object's properties. Create getter and setter methods for all instance variables. Create a toString() method that returns a string containing the name of each property and the current value.  For example:

Room: Davis 225
Description: Computer lab
Treasure: computer

RoomTest

Create a test driver named RoomTest. In the driver, create a treasure named lint, a piece of dust, with zero weight and zero value.  Create a second treasure, a key, a golden key, with weight 5 and value 25.  Create a Start Room, a cold, dark, damp place that contains the golden key.  Create a room Davis 225, computer lab, that contains the lint. Print your rooms and print the treasure that is in each room. (The room's toString() only contains the name of a treasure. The treasure's toString() has more info about the treasure.) Note: in order to run your RoomTest driver, you will need Treasure.java as well as Room.java in the same directory as your RoomTest.java. Copy Treasure.java into the directory where you are storing the files for this lab.

Submit Your Assignment

Submit RoomTest.java, Room.java, and Treasure.java to the L09a dropbox on D2L.