The complete program is below. Copy it to a file and run it.
// File Car.java
//
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 (endMiles - startMiles)/gallons ;
}
}
// File MilesPerGallon.java
//
import java.util.Scanner ;
class MilesPerGallon
{
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
double startMiles, endMiles, gallons;
System.out.print("Enter first reading: " );
startMiles = scan.nextDouble();
System.out.print("Enter second reading: " );
endMiles = scan.nextDouble();
System.out.print("Enter gallons: " );
gallons = scan.nextDouble();
Car car = new Car( startMiles, endMiles, gallons );
System.out.println( "Miles per gallon is " + car.calculateMPG() );
}
}
Each class is contained in a separate file.
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.
When you compile this program how many class files will there be?