Created
April 25, 2013 18:13
-
-
Save anonymous/5461837 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() | |
{ | |
if(new Car(start,end,galls).mpg()<15) | |
{ | |
return true; | |
} | |
return false; | |
} | |
boolean economyCar() | |
{ | |
if(new Car(start,end,galls).mpg()>30) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
class thirtyone1 | |
{ | |
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()==true) | |
{ | |
System.out.println("Gas Hog!"); | |
} | |
if(car1.economyCar()==true) | |
{ | |
System.out.println("Economy Car!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment