Guest User

Untitled

a guest
Jan 27th, 2020
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class GasTank
  2. {
  3. // Gas tank has two instance variables (fields, attributes etc)
  4. // that keeps track of the amount of gasoline
  5. // in the tank and it's total capacity. They can be decimals.
  6.  
  7. private double capacity = 0;
  8. private double gas = 0;
  9.  
  10. /**
  11. * Default constructor that sets the amount of gas to zero.
  12. */
  13. public GasTank(double capacity)
  14. {
  15.  
  16. capacity = 0;
  17.  
  18. }
  19.  
  20. /**
  21. * bumps up the amount of gas in the gas tank by amount, but will not exceed
  22. * the capacity of the gas tank.
  23. *
  24. * @param amount
  25. */
  26. public void addGas(double amount)
  27. {
  28. gas += amount;
  29.  
  30. }
  31.  
  32. /**
  33. * Removes the gas from the gas tank by amountUsed. You can't have negative
  34. * gas!! so if the client uses more than what's in the tank then keep it zero.
  35. *
  36. * @param amountUsed
  37. */
  38. public void useGas(double amountUsed)
  39. {
  40.  
  41. if (amountUsed <= gas)
  42. {
  43. gas -= amountUsed;
  44. }
  45. else
  46. {
  47. gas = 0;
  48. }
  49.  
  50. }
  51.  
  52. /**
  53. * Tank is empty if we have less than 0.1 gallons of gas. So person has chance
  54. * to run to the gas station before they are really out.
  55. *
  56. * @return
  57. */
  58. public boolean isEmpty()
  59. {
  60.  
  61. if (gas < 0.1)
  62. {
  63. return true;
  64. }
  65.  
  66. return false;
  67. }
  68.  
  69. /**
  70. * The gas tank is full if the amount is within 0.1 gallons of the total
  71. * capacity of the tank.
  72. *
  73. * @return
  74. */
  75. public boolean isFull()
  76. {
  77. return gas >= 0.1 + capacity;
  78. }
  79.  
  80. /**
  81. * Fills up the tank but ALSO returns the amount of gas it took to fill up the
  82. * tank to capacity. Remember there might already be some gas in the tank.
  83. *
  84. * @return
  85. */
  86. public double fillUp()
  87. {
  88. // TODO: Add code here.
  89. double gasDifference = capacity - gas;
  90. gas = capacity;
  91. return gasDifference;
  92. }
  93.  
  94. /**
  95. * Just a simple getter that returns the amount of gas in the gas tank.
  96. *
  97. * @return
  98. */
  99. public double getGasLevel()
  100. {
  101. // TODO: Add code here.
  102. return gas;
  103. }
  104.  
  105. }
RAW Paste Data