-
-
Save tbuehlmann/5465767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Car { | |
//instance variables | |
double start, end, galls; | |
//constructor | |
Car(double start, double end, double galls) { | |
this.start = start; | |
this.end = end; | |
this.galls = galls; | |
} | |
//methods | |
double mpg() { | |
return (end-start)/galls; | |
} | |
boolean gasHog() { | |
return mpg() < 15; | |
} | |
boolean economyCar() { | |
return mpg() > 30; | |
} | |
public static void main (String[] args) { | |
// get data | |
Scanner keyboard = new Scanner(System.in); | |
System.out.println("Enter first reading!"); | |
double start = keyboard.nextDouble(); | |
System.out.println("Enter second reading!"); | |
double end = keyboard.nextDouble(); | |
System.out.println("Enter gallons!"); | |
double galls = keyboard.nextDouble(); | |
// create car object and call methods | |
Car car1 = new Car(start, end, galls); | |
System.out.println("\nMiles per gallon: " + car1.mpg()); | |
if(car1.gasHog()) { | |
System.out.println("Gas Hog!"); | |
} | |
if(car1.economyCar()) { | |
System.out.println("Economy Car!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment