SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Structures And Classes
Exercise:
Programming Projects
Question:4 | ISBN:9780132846813 | Edition: 5

Question

Write the definition for a class named GasPump to be used to model a pump at an automobile service station. Before you go further with this programming exercise, write down the behavior you expect from a gas pump from the point of view of the purchaser.

The following are listed things a gas pump might be expected to do. If your list differs, and you think your list is as good or better than these, then consult your instructor. You and your instructor should jointly decide what behavior you are to implement. Then implement and test the agreed upon design for a gas pump class.

a. A display of the amount dispensed

b. A display of the amount charged for the amount dispensed

c. A display of the cost per gallon, liter, or other unit of volume that is used

where you reside

d. Before use, the gas pump must reset the amount dispensed and amount

charged to zero.

e. Once started, a gas pump continues to dispense fuel, keep track of the

amount dispensed, and compute the charge for the amount dispensed until

stopped.

f. A stop dispensing control of some kind is needed.

Implement the behavior of the gas pump as declarations of member functions of the gas pump class, then write implementations of these member functions. You will have to decide if there is data the gas pump has to keep track of that the user of the pump should not have access to. If so, make these private member variables.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

#include <iostream>
using namespace std;
//Create GasPump class is defined, which encapsulates 
//the behavior and data of a gas pump.
class GasPump 
{
private:
    //Declare variables
    double amountDispensed;
    double amountCharged;
    double costPerUnit;

public:
   //Constructor with parameter
    GasPump(double cost) 
    {
        amountDispensed = 0.0;
        amountCharged = 0.0;
        costPerUnit = cost;
    }
    //The reset member function resets the 
    //amountDispensed and amountCharged to zero.
    void reset() {
        amountDispensed = 0.0;
        amountCharged = 0.0;
    }
    //The startDispensing member function takes a volume parameter
    //and updates the amountDispensed and amountCharged based on the provided volume and the cost per unit.
    void startDispensing(double volume) {
        amountDispensed += volume;
        amountCharged += volume * costPerUnit;
    }
    //The stopDispensing member function simply outputs 
    //a message indicating that dispensing has stopped
    void stopDispensing() {
        cout << "Dispensing stopped." << endl;
    }
    //The displayAmountDispensed, displayAmountCharged, and 
    //displayCostPerUnit member functions output the corresponding information to the console.
    void displayAmountDispensed() {
        cout << "Amount dispensed: " << amountDispensed << endl;
    }

    void displayAmountCharged() {
        cout << "Amount charged: " << amountCharged << endl;
    }

    void displayCostPerUnit() {
        cout << "Cost per unit: " << costPerUnit << endl;
    }
};
//Program begins with a main method
int main() {
    GasPump pump(2.5);  // Create a gas pump with a cost of 2.5 per unit

    pump.reset();  // Reset the pump before use

    pump.startDispensing(10.0);  // Start dispensing 10 units
    pump.startDispensing(5.0);   // Continue dispensing 5 units

    pump.displayAmountDispensed();  // Display the amount dispensed
    pump.displayAmountCharged();    // Display the amount charged
    pump.displayCostPerUnit();      // Display the cost per unit

    pump.stopDispensing();  // Stop the dispensing

    return 0;
}

Output of the program code:

Amount dispensed: 15
Amount charged: 37.5
Cost per unit: 2.5
Dispensing stopped.

 

0 0

Discussions

Post the discussion to improve the above solution.