212k views
1 vote
Write a class named GasTank containing:

An instance variable named amount of type double, initialized to 0.
A method named addGas that accepts a parameter of type double.
The value of the amount instance variable is increased by the value of the parameter.
A method named useGas that accepts a parameter of type double.
The value of the amount instance variable is decreased by the value of the parameter.
A method named getGasLevel that accepts no parameters.
getGasLevel returns the value of the amount instance variable.

1 Answer

7 votes

Answer:

The answer to this question can be given as:

Class definition:

public class GasTank //define class GasTank

{

private double amount = 0; //define variable.

public void addGas(double x) //define function addGas.

{

amount=amount+x; //calculate value.

}

public void useGas(double x) //define function useGas.

{

amount=amount-x; //calculate value.

}

public double getGasLevel() //define function getGasLevel

{

return amount; //return value.

}

}

Step-by-step explanation:

The description of the above class definition can be given as:

  • In the class definition first we define a class that is "GasTank". In this class we define a variable that is amount the datatype of this variable is double that is used to store real or decimal values.
  • Then we define three function that is addGas(), useGas() and getGasLevel(). addGas() and useGas() function does not return any value and in both function we pass variable x as a parameter that calculate there value and the getGasLevel() function will return amount variable value.
User Kevin Owocki
by
5.3k points