Car Fleet

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes. A fleet of cars consists of several cars.

Car Fleet

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.)

Here is a skeleton of the Fleet class, along with the definition of Car and a testing class:

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;

  . . .  
}

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?