ITEC 120
L08b: Treasure

Objectives

To successfully complete this lab you will develop a class that represents a treasure. You will create and access public and private instance variables and you will develop a con­structor, getter and setter methods, and toString method.

Assignment

This assignment introduces the BlueJ integrated development environment (IDE). BlueJ provides a graphical user interface (GUI) that helps developers visualize the relati­onship between classes. BlueJ is available on the lab machines in Davis Hall.  BlueJ is an open source application that is free to use. You may download a copy of the applica­tion at: http://www.bluej.org/download/download.html

Treasure

Create a class named Treasure with the following properties:

Constructor

The purpose of the constructor is to return an instance of the class (an object) in a valid state. The state of an object is the value of each property (instance variable). Therefore, the constructor must initialize each instance variable.

Create a constructor for the Treasure class. Have the caller pass a value for all four of the object's properties. The names of the constructor's parameters should be different from the names of instance variables. A good convention is to start each parameter name with an underscore (e.g., _name). Whatever naming convention you choose, be consistent throughout the entire class.

Accessing Properties

Recall that we always call a method on an object using the pattern below.

    object.method_name(parameter values);

The dot indicates that the object contains the method. Dot notation is also used to indicate that a property belongs to an object. Thus, you can use the dot operator to access methods and properties of an object.

Create a test class named TreasureTest. In the main method, create a treasure named t1 that represents a key.

        Name:        Key
        Description: Golden Key
        Weight:      1.5
        Value:       25

Use the dot notation to print each property. For example:

    System.out.println("name: " + t1.name);

Modify the Treasure class and make each instance variable private. What happens when you run the test driver? Why?

Getters and Setters

Instance variables are almost always private to encapsulate and protect the object's data. As you learned, private variables may not be accessed using the dot notation. Therefore, classes create methods to get and set the values of instance variables.

Create getter and setter methods for all four instance variables in the Treasure class. In practice, getters and setters are only created as needed, but the objective for this lab is to give you practice.

Modify the TreasureTest driver to print each property using the getter methods. For example:

     System.out.println("name: " + t1.getName());

Use the setter methods to change the value of each property of t1 as shown below and print each property again to verify that the setter methods work correctly.

        Name:        Ring
        Description: Platinum ring
        Weight:      2.5
        Value:       80

toString Method

The toString method is a standard Java method that returns a string representation of an object. Create a toString() method for the Treasure class that returns a string containing the name of each property and the current value.  For example:

Name: Key
Description: Golden Key
Weight: 1.5  Value: 25

Recall that Java performs automatic conversions. For example, in the statement below, the number 21 is converted to a string before the parameter value is passed to the print statement.

     System.out.println(21);

If a class defines a toString method, Java will use the toString method to convert a handle to a string representation of the class. Extend your TreasureTest class to print the key and the ring.

     System.out.println(t1);

is the same thing as this:

     System.out.println(t1.toString());

Submit Your Assignment

Submit your Treasure.java and TreasureTest.java to the Lab08b dropbox on D2L. We will continue to build on this assignment so you must complete the entire assignment.