go to previous page   go to home page   go to next page

Answer:

Yes. A fleet of cars consists of several cars.


Car Fleet

class Fleet
{
  // data
  Car town;         // the town car of the fleet
  Car suv;          // the sports utility vehicle of the fleet

  // constructor

  // method

}

class Car           // unchanged from above
{
  . . . 
}

class FleetTester
{
  Fleet myCars;

  . . .  
}

Consider a fleet consisting of two cars: a town car and a sports utility vehicle. Think of the fleet as a single object composed of two objects. Remember that an object has (i) identity, (ii) state, and (iii) behavior. Is this true for a fleet of cars?

  1. Identity: Yes — my fleet is different from your fleet.
  2. State: Yes — the odometer readings of each car is part of the state of the fleet.
  3. Behavior: Yes — the fleet can have a method to compute average MPG for the fleet (and can have other methods if we care to write them.)

Above is a skeleton of the Fleet class and a testing class.

The definition of class Car is the same as above. It does not change, even though Car objects are now used as part of a larger object. The (not yet finished) definition of Fleet says that a fleet consists of two cars.


QUESTION 4:

Does the definition of class Fleet include the definition of class Car?