A good answer might be:

The data of a Car object should be:

  1. Stating odometer reading,
  2. Ending odometer reading, and
  3. Gallons of gas used between the readings.

The names of the variables are up to the programer.


Filling in the Definition

Here is the program with some of the Car definition filled in:

import java.io.* ;

class Car
{
  // instance variables
  int startMiles;   // Stating odometer reading
  int endMiles;     // Ending odometer reading
  double gallons;   // Gallons of gas used between the readings

  // constructor


  // methods

}

class MilesPerGallon
{
  public static void main( String[] args ) 
  {
    Car car = new Car( 32456, 32810, 10.6 );
    System.out.println( "Miles per gallon is " + car.calculateMPG() );
  }
}

Instance variables are the variables that contain the state of an object. Instance variables hold on to their values as long as the object exists. They can be changed (see the next chapter), but otherwise will store their data for the lifetime of the object. The previous chapter just called them "variables," but it will be useful later on to have a more exact term.

QUESTION 7:

What must the constructor be named?