Write a class named GasTank containing: An instance variable named amount of typ
ID: 3614091 • Letter: W
Question
Write a class named GasTank containing:
An instance variable named amount of type double, initializedto 0.
An instance variable named capacity of type double.
A constructor that accepts a parameter of type double. The value ofthe parameter is used to initialize the value of capacity.
A method named addGas that accepts a parameter of type double . The value of the amount instance variable isincreased by the value of the parameter. However, if the valueof amount is increased beyond the value of capacity, amount is set to capacity .
A method named useGas that accepts a parameter of type double . The value of the amount instance variable isdecreased by the value of the parameter. However, if the valueof amount is decreased below 0 , amount is setto 0 .
A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1 , and false otherwise.
A method named isFull that accepts no parameters. isFull returns a boolean value: true if the value of amount is greater than capacity-0.1 , and falseotherwise.
A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instancevariable.
this what i have so far:
public class GasTank {
double amount=0;
double capacity=0;
GasTank( double n1){capacity=n1;}
public void addGas( double n2){if (amount<capacity){amount=amount + n2;} else{amount=capacity;}}
public void useGas(double n3){ if( amount<0){ amount=0;}else
{amount=amount-n3;}}
public boolean isEmpty(){ if ( amount<.1){ return true;} else {return false;}}
public boolean isFull(){if(amount>.1){ return true;} else{return false;}}
public double getGasLevel(){return amount;}
}
whenever i submit the answer i get an error saying, Remember thataddGas behaves differently when the
value of amount is increased beyond the value of capacity
Explanation / Answer
please rate - thanks I made it readable and corrected a few things public class GasTank {double amount=0; double capacity=0; GasTank( double n1) {capacity=n1;} public void addGas( double n2) {amount= amount + n2; if(amount>capacity) amount=capacity; } public void useGas(double n3) {amount=amount-n3; if( amountRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.