Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Forked from anonymous/gist:5465858
Last active December 16, 2015 16:49
Show Gist options
  • Select an option

  • Save tbuehlmann/5465866 to your computer and use it in GitHub Desktop.

Select an option

Save tbuehlmann/5465866 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class NewCar {
// instance variables
double start, end, galls;
// constructor
NewCar(double start) {
this.start = start;
}
// methods
void fillUp(double miles, double galls) {
if (end > 0)
start = end;
end = miles;
this.galls = galls;
}
double mpg() {
return (end-start)/galls;
}
public static void main (String[] args) {
// get data, construct car
Scanner keyboard = new Scanner(System.in);
System.out.println("New car odometer reading:");
double start = keyboard.nextDouble();
NewCar car = new NewCar(start);
// 2 gas station visits
for(int i=1; i<=2; i++) {
System.out.println();
System.out.println("Filling Station Visit");
System.out.println("odometer reading:");
double miles = keyboard.nextDouble();
System.out.println("Gallons to fill tank:");
double galls = keyboard.nextDouble();
car.fillUp(miles ,galls);
System.out.println("Miles per gallons: " + car.mpg());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment