CS 1054: Laboratory Exercise 7

Objective

This exercise's objective is to get you some experience in creating a class and then instantiating objects from that class.

To Receive Credit

Show the work that you do to the GTA.

Activities

Programming Exericse 6.1 from textbook): After launching the Eclipse IDE, create a new project and name it Lab7. Create a class within this project called Rectangle. As discussed in class, classes characterize some real-life entity from which objects are instantiated. A rectangle can be characterized by its width, height and color. Assume that all rectangles created from this class have the same color. Within the class, these are the data-fields (also called instance variables), that exist within the class but outside of any methods. A class also has methods to alter the data-fields. The UML class diagram representing these data-fields and methods is shown below:

Each of the data fields and methods is described below:

Once you are finished with the Rectangle class, write a client program (another class called ClientClass to test the class Rectangle. This class should have a main method, while the Rectangle class should not. Within the main method, you should create two Rectangle objects. Assign width 4 and height 4 to each of the two objects. Assign color yellow. Display the properties of both objects and find their areas and perimeters.

You can use the no-args constructor method to create both objects and then use the set methods (called mutator methods) to change the properties of the two rectangles. The other option is to use the other constructor and pass the width, height and color as parameters to it. The other types of methods (with the prefix get) are called accessor methods, since they do not change the state of the object, but merely retrieve the values of the instance variables.


© Mir Farooq Ali 2005.