10.3k views
3 votes
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the 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 is increased by the value of the parameter. However, if the value of 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 is decreased by the value of the parameter. However, if the value of amount is decreased below 0, amount is set to 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 false otherwise. A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable. A method named fillUp that accepts no parameters. fillUp increases amount to capacity and returns the difference between the value of capacity and the original value of amount (that is, the amount of gas that is needed to fill the tank to capacity).

User Yofee
by
5.5k points

2 Answers

2 votes

Below is a Python implementation of the GasTank class based on the description:

class GasTank:

def __init__(self, capacity):

self.amount = 0.0

self.capacity = capacity

def addGas(self, added_amount):

self.amount += added_amount

if self.amount > self.capacity:

self.amount = self.capacity

def useGas(self, used_amount):

self.amount -= used_amount

if self.amount < 0:

self.amount = 0

def isEmpty(self):

return self.amount < 0.1

def isFull(self):

return self.amount > self.capacity - 0.1

def getGasLevel(self):

return self.amount

def fillUp(self):

needed_gas = self.capacity - self.amount

self.amount = self.capacity

return needed_gas

This example demonstrates how to create a GasTank object, add and use gas, check if it's empty or full, get the gas level, and fill up the tank.

User Herat Patel
by
5.4k points
1 vote

Answer:

The class GasTank is defined below

All the steps are briefed in comments

public class GasTank {

// instance variable initialization

private double amount = 0;

//declaring instance variable capacitance

private double capacity;

//constructor having parameter of type double

public GasTank(double i)

{

capacity = i;

}

// addGas method for increasing gas quantity.

public void addGas(double i)

//quantity of gas increased is added to the existing amount. If it becomes more than total capacity, amount is set to capacity

{ amount += i; if(amount > capacity) amount = capacity; / amount = amount < capacity ? amount+i : capacity;/ }

//useGas method having parameter of type double

public void useGas(double i)

//the parameter given is deducted from 0 and if results less than 0, remains equal to 0

{ amount = amount < 0 ? 0 : amount - i; }

//method isEmpty

public boolean isEmpty()

//Returns true if volume is less than 0.1 else false

{ return amount < 0.1 ? true : false; }

//method isFull

public boolean isFull()

//returns true if the value of amount is greater than 0.1 else false.

{ return amount > (capacity-0.1) ? true : false; }

//method getGasLeve

public double getGasLevel()

//Returns the value of amount instance variable

{ return amount; }

//method fillUp

public double fillUp()

//returns the difference between the capacity and the amount

{ double blah = capacity - amount; amount = capacity; return blah; }

}

User Dylan Tack
by
5.2k points