I need this written in C++. Your time is greatly appreciated =] Write the implem
ID: 3543570 • Letter: I
Question
I need this written in C++. Your time is greatly appreciated =]
Write the implementation (.cpp file) of the GasTankclass of the previous exercise. The full specification of the class is:
- A data member named amount of type double .
- An data member 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 function named addGas that accepts a parameter of type double . The value of the amountinstance 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 function named useGas that accepts a parameter of type double . The value of the amount data member is decreased by the value of the parameter . However, if the value of amount is decreased below 0 , amount is set to 0 .
- A function named isEmpty that accepts no parameters and returns a boolean value . isEmpty returns true if the value of amount is less than 0.1 , and false otherwise.
- A function named isFull that accepts no parameters and returns a boolean value .. isFull returns true if the value of amount is greater than capacity-0.1 , and false otherwise.
- A function named getGasLevel that accepts no parameters . getGasLevel returns the value of the amount data member.
- A function named fillUp that accepts no parameters and returns a double . fillUpincreases 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).
Explanation / Answer
#include<iostream>
using namespace std;
class GasTankclass {
public:
double amount, capacity;
GasTankclass(int c){
amount = 0;
capacity = c;
}
void addGas(double x){
amount += x;
if(amount >= capacity) amount = capacity;
}
void useGas(double x){
amount -= x;
if(amount <= 0) amount = 0;
}
bool isEmpty(){
if(amount < 0.1) return true;
else return false;
}
bool isFull(){
if(amount < capacity - 0.1) return false;
else return true;
}
double getGasLevel(){
return amount;
}
double fillUp(){
double x = capacity - amount;
amount = capacity;
return x;
}
};
int main(){
double x;
cout << "Enter the tank capacity: ";
cin >> x;
GasTankclass obj(x);
cout << "Capacity = " << obj.capacity << " Current Amount = " << obj.getGasLevel() << endl << endl;
cout << "How much gas to fill? ";
cin >> x;
obj.addGas(x);
if(obj.isFull()) cout << "Tank full ";
cout << "Capacity = " << obj.capacity << " Current Amount = " << obj.getGasLevel() << endl << endl;
cout << "How much gas to use? ";
cin >> x;
if(obj.isEmpty()) cout << "Tank empty ";
obj.useGas(x);
cout << "Capacity = " << obj.capacity << " Current Amount = " << obj.getGasLevel() << endl << endl;
cout << "Refilling the tank now";
obj.fillUp();
cout << "Capacity = " << obj.capacity << " Current Amount = " << obj.amount << endl << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.