Instance Variables for 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:

No. However, the definition for Fleet says that a Fleet object has two variables that refer to Car objects.

Instance Variables for Fleet

The state of a Fleet object is held in its two instance variables. These variables refer to Car objects. The documentation for Fleet might look like this:


Fleet
A class that holds two Car objects.
Constructors
Fleet( int Car1StartOdo, int Car1EndingOdo, double Car1Gallons, 
       int Car2StartOdo, int Car2EndingOdo, double Car2Gallons )
Creates a new instance of a Fleet object with the starting and ending odometer readings and the number of gallons of gas consumed for each car.
Methods
double calculateMPG()
Calculates and returns the average miles per gallon for the fleet.

The constructor builds the two cars of the fleet. Each car needs three initial values. So the constructor for Fleet has a total of six initial values. (There are other, more elegant, ways to do this, but let us do it this way for now.) Here is a short   main()   that constructs a fleet:

class FleetTester
{
  public static void main ( String[] args)
  {
    Fleet myCars = new Fleet(  );
  }  

}

QUESTION 5:

Fill in the blanks so that the Fleet object looks like this:

  1. The first car in the fleet has odometer readings of 1000, 1234, and gallons of 10
  2. The second car in the fleet has odometer readings of 777, 999, and gallons of 20