CS123 Project: Ships in a Tree

Note: This project is designed to be done with a lab partner.

Goals: By the end of this project, you should...

Overview: An advantage of object-oriented software development is the ability to create very large applications as the composition of many smaller interacting objects. However, large software systems not only have many objects, they also have many classes. Creating and keeping track of all the different classes and how they relate to each other would be difficult without some organizational aids. We have already seen and used packages as a way to keep related classes together. Grouping classes in folders, however, is not enough to capture important relationships. For example, simply putting files named "Shape," "Ellipse," and "Rectangle" together in a folder tells us that they relate, but it doesn't tell us exactly how.

A common approach for handling relationships among many types of objects is to devise a classification system. For example, biologists have constructed an elaborate taxonomy of living organisms, with many levels of subclassification. From the taxonomy, we can tell that all humans are mammals, and that fish are not plants. In the case of biologists, the classification system is built on observing the properties of living things and putting them in a hierarchy based upon commonalities. Properties are inherited down the hierarchy, with each classification being more specific than the ones above it. Object-oriented software development uses classification systems in very similar way. However, we are in complete control of what properties each object will have! Like biologists, we may create a hierarchy that reflects real-world relationships, but more importantly, we use the hierarchy to help define the properties we want our objects to have. In other words, when we put one class below another in a class hierarchy, we are saying that the subclass (the one below) will inherit all the properties of the parent class, including instance variables and methods, except for any modifications we make to the subclass. This accomplishes several important things. It gives us a very structured way of describing relationships between classes, and it saves of the trouble of redefining a lot of things that we can simply inherit from a parent class. In addition, this simplifies modification of programs, because if we change the definition of a class, subclasses automatically inherit the changes.

In this project, you will read a specification for a software system. Within it, you will identify various types of objects and relationships in order to construct a class hierarchy that captures the given information. Then you will implement a portion of the hierarchy as Java classes.


Part I: Relationship Identification

In constructing a class hierarchy, it is important to distinguish between two fundamentally different ways a class might relate to another.

  1. "has-a" relationships: properties, information the object has or knows (for example, every bird has a wing span)
  2. "is-a" relationships: is this a special case of a more general type? (for example, every recliner is a chair)
Distinguishing between these two kinds of relationships is important because "is-a" relationships dictate the subclassing in the hierarchy, whereas "has-a" relationships determine the properties of a class, which are then inherited by subclasses. For example, since every recliner "is a" chair, you would put the Recliner class below the Chair class in the hierarchy, and it would therefore inherit all the properties of the Chair class. However, you would not say that a bird "is a" wing span, so the Bird class would not be a subclass of a WingSpan class. Instead, you would probably create an instance variable called wingSpan inside the Bird class, and any subclasses of Bird would inherit it as well.

Discuss the provided specification for a fictitious company's software system (below). Think about classes that could be used as abstractions of the various objects described in the narrative, and fill in the table with as many "has-a" or "is-a" relationships that you can identify or infer from the specification. For the "has-a" relationships, give both the name of the property and the type of the value. The specification is not presented in a very organized way... your job is to create order from the seemingly random pieces of information! Check that the entire specification is captured in the table. You may add rows as needed. Show your completed table to the instructor or TA before going on.

Part II: Designing a Class Hierarchy

Draw a class hierarchy that captures the is-a relationships in your table. Draw the class hierarchy as a tree with the class Object as the root (top) of the tree. Represent each class as a rectangle. Inside each rectangle, write the name of the class and a list of the properties ("has-a" relationshiops) defined in that class. Draw branches in the tree from each class to each of its immediate subclasses. In subclasses, list only the new properties (not the ones that are already inherited from above).

Remember that the class hierarchy is a tree, which means that each class has exactly one parent (except the root, which has no parent). Some programming languages (like C++) allow a class to have multiple parent classes, but Java does not, so you will have to make a choice if a class has "is-a" relationships with two or more other classes. If those other classes already have an "is-a" relationship among themselves, then you can simply choose the most specific one (the lowest one in the hierarchy) as the parent, and therefore inherit from all of them. However, if the other classes are in different parts of the tree, you won't be able to inherit from more than one, so you may decide to use the more "important" one as the parent and create "has-a" relationships to capture the other information. When you get to this part of the lab, place a green card on your monitor to let us know you are ready for a group discussion. Meanwhile, you can continue with the lab.

Part III: Implementing the Hierarchy as Java Classes

Let the instructor or TA review your class hierarchy and help you choose which classes to implement. (There may not be time to implement all of them.)

In the JPie Packages and Classes window, choose the "Edit In your implementation, create instance variables for each "has-a" relationship, and extend a class to capture each "is-a" relationship. In Java, when B is a subclass of A, we say that B extends A. To extend a class in JPie, you select the parent class in the Packages and Classes window, and select "Class Hierarchy" from the edit menu to open the class hierarchy editor. You can use the following features to create your class hierarchy.

As you create your class hierarchy, notice the items that are inherited from the parent (in the methods summary). Also notice that if you add or remove items in the parent, you'll see those changes in the subclass as well.

When you are finished, show your relationship table, class hierarchy, and implementation to the instructor or TA to receive credit for this project.

Specification: Washington Transport Corporation (WTC) is a worldwide conglomerate that owns many different "Transport Units" that go by land (buses and trucks), sea (ships, including cruise ships and barges), and air (airplanes). WTC leases these units to other companies and individuals for various transportation needs. For each transportation unit, the company must at all times keep track of who (if anyone) is currently leasing it and in which country they are located.
The company is organized as two major divisions based on transportation purpose: commercial freight and passenger service. Transportation units may be switched between divisions, so one day an airplane may be carrying passengers, and the next day pineapples. Each transportation unit has a purpose, depending upon which division it is in. When a unit is being used for passenger service, it has a maximum number of passengers and specified crew size. On the other hand, when a unit is being used for freight, it has a maximum shipping capacity (in pounds) and a specified crew size. When used for commercial freight, some transport units are permitted to carry hazardous materials and some are not. However, this can change over time, according to applicable regulations.
All airplanes and ships (including cruise ships and barges) have a country of registration. All land vehicles have both a country of registration and a licence number. From time to time, any transportation unit may be taken out of service for maintenance, in which case the information system must be able to report that the unit is not available for lease until a certain date.

Class "Has-a" Relationships "Is-a" Relationships
TransportUnit   
Ship   
Barge   
Cruise Ship   
Airplane   
Bus   
Truck   
TransportPurpose   
CommercialFreight   
PassengerService   
LandVehicle   
Country