The constructor must be named Car
, the same as the name of the class.
The constructor is used when an object is constructed.
Most of the work in making an object (out of main memory) happens behind the
scenes.
Usually the constructors that you write initialize a few variables.
The constructor for Car
will initialize the three variables
of the object.
Here is the program, with the constructor partially finished:
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
Car( _____ _____, ____ ______, _____ _____ )
{
}
// methods
}
The parameter list of a constructor looks like this:
dataType parameterName , dataType parameterName , and so on
Each dataType is the type of one item of data that will be handed to the constructor, and each parameterName is a name that is used for that item of data.