Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Forked from anonymous/gist:5461837
Last active December 16, 2015 16:49
Show Gist options
  • Save tbuehlmann/5465767 to your computer and use it in GitHub Desktop.
Save tbuehlmann/5465767 to your computer and use it in GitHub Desktop.
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