-
-
Save tbuehlmann/5465866 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 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