Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- 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 ;
- }
- boolean gasHog() {
- if (calculateMPG() < 15) {
- return true;
- } else {
- return false;
- }
- }
- }
- 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() );
- if (car.gasHog() = true)
- System.out.println( "Gas hog!" ) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement