You would expect to use:
Car class
Think about
the classes you need
before you start writing.
This makes programming easier
and
your programs will have fewer bugs.
Object oriented design means deciding what classes you need,
what data the objects hold,
and how the objects will behave.
Let us do that with the Car class.
CarA class that calculates miles per gallon.
Variables
- double startMiles; // Starting odometer reading
- double endMiles; // Ending odometer reading
- double gallons; // Gallons of gas used between the readings
Constructors
- Car( double startOdo, double endingOdo, double gallons )
Creates a new instance of a Car object with the starting and ending odometer readings and the number of gallons of gas consumed.Methods
- double calculateMPG()
calculates and returns the miles per gallon for the car.
Look at the parameter list for the constructor:
Car( double startOdo, double endingOdo, double gallons );
This says that the constructor must be called with three items
of data: three double precision values.
Could a main() method create a Car object?