The data of a Car object should be:
The names of the variables are up to the programmer.
Here is Car
with some parts filled in
filled in:
class Car { // instance variables double startMiles; // Stating odometer reading double endMiles; // Ending odometer reading double gallons; // Gallons of gas used between the readings // constructor // methods }
An instance variable is a variable that holds part of the state of an object. Each object (each "instance" of the class) contains its own instance variables. Instance variables hold on to their values as long as the object exists. An assignment statements can change the value in an instance variable (see the next chapter), but otherwise it holds its value for the lifetime of the object.
What must the constructor of Car
be named?