Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GasTank
- {
- // Gas tank has two instance variables (fields, attributes etc)
- // that keeps track of the amount of gasoline
- // in the tank and it's total capacity. They can be decimals.
- private double capacity = 0;
- private double gas = 0;
- /**
- * Default constructor that sets the amount of gas to zero.
- */
- public GasTank(double capacity)
- {
- capacity = 0;
- }
- /**
- * bumps up the amount of gas in the gas tank by amount, but will not exceed
- * the capacity of the gas tank.
- *
- * @param amount
- */
- public void addGas(double amount)
- {
- gas += amount;
- }
- /**
- * Removes the gas from the gas tank by amountUsed. You can't have negative
- * gas!! so if the client uses more than what's in the tank then keep it zero.
- *
- * @param amountUsed
- */
- public void useGas(double amountUsed)
- {
- if (amountUsed <= gas)
- {
- gas -= amountUsed;
- }
- else
- {
- gas = 0;
- }
- }
- /**
- * Tank is empty if we have less than 0.1 gallons of gas. So person has chance
- * to run to the gas station before they are really out.
- *
- * @return
- */
- public boolean isEmpty()
- {
- if (gas < 0.1)
- {
- return true;
- }
- return false;
- }
- /**
- * The gas tank is full if the amount is within 0.1 gallons of the total
- * capacity of the tank.
- *
- * @return
- */
- public boolean isFull()
- {
- return gas >= 0.1 + capacity;
- }
- /**
- * Fills up the tank but ALSO returns the amount of gas it took to fill up the
- * tank to capacity. Remember there might already be some gas in the tank.
- *
- * @return
- */
- public double fillUp()
- {
- // TODO: Add code here.
- double gasDifference = capacity - gas;
- gas = capacity;
- return gasDifference;
- }
- /**
- * Just a simple getter that returns the amount of gas in the gas tank.
- *
- * @return
- */
- public double getGasLevel()
- {
- // TODO: Add code here.
- return gas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement