The complete constructor is seen below.
class Car { // instance variables double startMiles; // Stating odometer reading double endMiles; // Ending odometer reading double gallons; // Gallons of gas used between the readings // constructor Car( double first, double last, double gals ) { startMiles = first; endMiles = last; gallons = gals; } // methods double calculateMPG() { return ; } }
The constructor copies values from its parameters to the instance variables of the new object. You might wonder why you need to do this. Why not just leave the data in the parameters? There are two reasons:
calculateMPG()
cannot see the parameters of the
constructor.
Think of a parameter as a scrap of paper containing information handed to the constructor. The constructor has to copy the information to someplace permanent that can be seen by the other methods.