In: Computer Science
Write a class named GasTank containing:
class GasTank {
private double amount = 0;
private double capacity;
public GasTank(double aCapacity) {
super();
capacity = aCapacity;
}
public void addGas(double d) {
amount += d;
if (amount > capacity)
amount = capacity;
}
public void useGas(double d) {
amount -= d;
if (amount < 0)
amount = 0;
}
public boolean isEmpty() {
return amount == 0;
}
public boolean isFull() {
return amount == capacity;
}
public double getGasLevel() {
return amount;
}
}
public class TestGasTank {
public static void main(String[] args) {
GasTank gt = new GasTank(100);
gt.addGas(50);
System.out.println("Gas : " + gt.getGasLevel());
gt.addGas(50);
System.out.println("Gas : " + gt.getGasLevel());
System.out.println("Is Full: " + gt.isFull());
gt.useGas(100);
System.out.println("Gas : " + gt.getGasLevel());
System.out.println("Is Empty: " + gt.isEmpty());
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME