The complete program, suitable for Copy-Paste-Save-and-Run, is seen below:
Here is the complete program.
The calculateMPG()
method returns a
double
value to the caller.
Since it returns a value,
there must be a return
statement
within its body that returns a value of the
correct type.
In the complete program, the calculateMPG()
method uses
the instance variables of its object to calculate miles per gallon.
import java.io.* ; class Car { // instance variables int startMiles; // Stating odometer reading int endMiles; // Ending odometer reading double gallons; // Gallons of gas used // constructor Car( int first, int last, double gals ) { startMiles = first ; endMiles = last ; gallons = gals ; } // methods double calculateMPG() { return (endMiles - startMiles)/gallons ; } } 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() ); } }