Answer to Question #224852 in Java | JSP | JSF for hajjk

Question #224852

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50

cent toll. Mostly they do, but sometimes a car goes by without paying. T Model this tollbooth with a class called tollbooth. The two data items are a

type unsigned int to hold the total number of cars, and a type double to hold the total

amount of money collected. A constructor initializes both of these to 0. A member

function called payingcar() increments the car total and adds 0.50 to the cash total.

Another function, called nopaycar(), increments the car total but adds nothing to the

cash total. Finally, a member function called display() displays the two totals. Make

appropriate member functions const. Include a program to test this class. This program should allow the user to push one key to count a paying car, and another to count a nonpaying car. Pushing the ESC key should cause the program to print out the total cars and total cash and then exit.


1
Expert's answer
2021-08-10T17:41:22-0400
#include <iostream>
using ​namespace std;

class tollBooth {
private:
    unsigned int carTotal;
    double cashTotal;

public:
    tollBooth();
    ~tollBooth();

    void payingCar();
    void nopayCar();
    void display() const;
};

tollBooth::tollBooth() {
    this->carTotal = 0;
    this->cashTotal = 0;
}

tollBooth::~tollBooth() {}

void tollBooth::payingCar() {
    carTotal += 1;
    cashTotal += 0.5;
}

void tollBooth::nopayCar() {
    carTotal += 1;
}

void tollBooth::display() const {
    cout << "Number of cars passed by: " << carTotal << "\nCash collected: $" << cashTotal << endl;
}

int main() {
    tollBooth mainBridge;

    mainBridge.payingCar();
    mainBridge.payingCar();
    mainBridge.payingCar();
    mainBridge.nopayCar();
    mainBridge.nopayCar();

    mainBridge.display();

    return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS