Advertisement
MnMWizard

Untitled

Oct 24th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. //By Mason Marnell
  2. public class Automobile {
  3. private double mpg;
  4. private double gas;
  5.  
  6. public Automobile(double mpgInput){
  7. mpg = mpgInput;
  8. gas = 0;
  9. }
  10.  
  11. public void fillup(double gasAdded){
  12. gas += gasAdded;
  13. }
  14.  
  15. public double reportFuel(){
  16. return gas;
  17. }
  18.  
  19. public void takeTrip(double miles){
  20. double gasgone = miles / mpg;
  21. gas -= gasgone;
  22. }
  23. public static void main(String[] args) {
  24.  
  25.  
  26. //Create a new object called myBmw. Pass the constructor an
  27. //argument of 24 miles per gallon
  28.  
  29. Automobile myBmw = new Automobile(24);
  30.  
  31. //Use the myBmw object to call the fillup method. Pass it an argument
  32. //of 20 gallons.
  33.  
  34. myBmw.fillup(20);
  35.  
  36. //Use the myBmw object to call the takeTrip method. Pass it an
  37. //argument of 100 miles. Driving 100 miles of course uses fuel and we
  38. //would now find less fuel in the tank.
  39.  
  40. myBmw.takeTrip(100);
  41.  
  42. //Use the myBmw object to call the reportFuel method. It returns a
  43. //double value of the amount of gas left in the tank and this is assigned
  44. // to the variable fuel_left
  45.  
  46. double fuel_left = myBmw.reportFuel( );
  47.  
  48. //Print the fuel_left variable
  49.  
  50. System.out.println(fuel_left);
  51.  
  52. //prints gallons left, 15.833333333333332
  53.  
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement