Write the implementation (.cpp file) of the GasTank class 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. The constructor also sets amount to zero. A function 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 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. 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).

.
.
Click on the title for the solution
.
.

This is the answer:

:

GasTank::GasTank(double a)
{
capacity=a;
amount=0;
}

void GasTank::addGas(double b)
{
amount +=b;
if( amount >capacity) amount=capacity;
}
void GasTank::useGas (double c)
{
amount-=c;
if ( amount<0) amount=0;
}
bool GasTank::isEmpty()
{
if(amount<0.1) return true;
else return false;
}
bool GasTank::isFull()
{
if(amount>(capacity-0.1)) return true;
else  return false;
}
double GasTank::getGasLevel()
{
return amount;
}
double GasTank::fillUp()
{
double temp = amount;
amount=capacity;
return capacity- temp;
}

Comments

Popular posts from this blog

411: You are given a file named phonedir that consists of many lines containing three strings: lastname firstname emailaddress. Write a utility program that reads commands (from stdin). Each command has one of two possible forms: lookup string add lastname firstname emailaddress In the case of the first command (lookup), the program looks for a line in the phonedir file where either the firstname or lastname matches the string given. If it finds such a line it prints out all three parts of the line, separated by spaces: lastname firstname emailaddress If it does not find a match in the file it prints out the string it was looking for, followed by a colon followed by a space followed by the message "not found": string: not found In the case of the second command (add), the program appends the three strings given to the file phonedir. Hints and suggestions: (1) Define and use two functions named lookup and add. When your program reads the string lookup, your lookup function is called; when the program reads the string add, your add function is called. When either of these two functions are called, they then read whatever else is necessary for their command (one string for lookup and three strings -- lastname, firstname, and emailaddress-- for add). And then these functions do... whatever it takes to carry out the command. (2) When doing a lookup or an add, open up the file phonedir ... carry out the operation .. and then close up the file. EXAMPLE: suppose the phonedir file looked like this: arnow david arnow@panix.com bush george president@whitehouse.gov gates bill bill@microsoft.com here then is a sample session with the program (program output is in bold): lookup david arnow david arnow@panix.com lookup joe joe: not found add theplumber joe joetheplumber@nowhere.com lookup arnow arnow david arnow@panix.com lookup joe theplumber joe joetheplumber@nowhere.com and the phonedir file would now be: arnow david arnow@panix.com bush george president@whitehouse.gov gates bill bill@microsoft.com theplumber joe joetheplumber@nowhere.com

Assume a class Window with accessor method getWidth that accepts no parameters and returns an integer. Assume further an array of 3 Window elements named winarr, has been declared and initialized. Write a sequence of statements that prints out the width of the widest window in the array.