Advertisement
Guest User

Untitled

a guest
Oct 21st, 2011
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Car
  4. {
  5. // instance variables
  6. double startMiles; // Stating odometer reading
  7. double endMiles; // Ending odometer reading
  8. double gallons; // Gallons of gas used between the readings
  9.  
  10. // constructor
  11. Car( double first, double last, double gals )
  12. {
  13. startMiles = first ;
  14. endMiles = last ;
  15. gallons = gals ;
  16. }
  17.  
  18. // methods
  19. double calculateMPG()
  20. {
  21. return (endMiles - startMiles)/gallons ;
  22. }
  23.  
  24. boolean gasHog() {
  25. if (calculateMPG() < 15) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31. }
  32.  
  33. class MilesPerGallon
  34. {
  35. public static void main( String[] args )
  36. {
  37. Car car = new Car( 32456, 32810, 10.6 );
  38. System.out.println( "Miles per gallon is " + car.calculateMPG() );
  39. if (car.gasHog() = true)
  40. System.out.println( "Gas hog!" ) ;
  41. }
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement