import Car ; 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() ); } }
With the documentation for Car
already done, writing the program is easy.
(However you can't run this program yet because class Car
has not yet been defined.)
Adding user interaction is done just as in previous programs.
import java.io.* ;
import Car ;
class MilesPerGallon
{
public static void main( String[] args )
throws IOException
{
BufferedReader userIn =
new BufferedReader(
new InputStreamReader( System.in ) );
String line;
int startMiles, endMiles;
double gallons;
System.out.println("Enter first reading:" );
line = userIn.readLine();
startMiles = Integer.parseInt( line );
System.out.println("Enter second reading:" );
line = userIn.readLine();
endMiles = Integer.parseInt( line );
System.out.println("Enter gallons:" );
line = userIn.readLine();
gallons = Integer.parseInt( line );
Car car = new Car(
__________, __________, __________ );
System.out.println( "Miles per gallon is "
+ car.calculateMPG() );
}
}
To keep things simple all input values are integers.
A better version of the program would use floating point input for
the gallons.
The statement
automatically converts the int
(on the right) to
a double
(on the left.)