By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,367 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,367 IT Pros & Developers. It's quick & easy.

How to define a class GasPump?

P: n/a
Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
....
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed above. In
my class GasPump, there is a method start() and a method stop(). But:

GasPump myGasPump;

myGasPump.start(); //start pumping gas

//now, how can I stop pumping?

I am thinking using a boolean bHold. start() will set bHold = true and
stop() will set bHold = false. Inside start() method, once bHold is
true, the amount dispensed will keep increasing
void GasPump::start()
{
bHold = true;
double dAmount = 0.0;
while (bHold) //while handle is being pressed
{
dAmount += 0.1
} //now the handle has been released
//now the dAmount is the amount dispensed,
}
void GasPump::stop()
{
bHold = false;
}

But this seems getting into inter-processe communication(too complicated
to me) and I don't believe this is author's intention for this
programming project. I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.
Dec 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Xiaoshen Li <xl**@gmu.edu> wrote:
Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.
[snip] I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.


Have you learned how to get any input from the user yet? I would
suggest doing the same thing, but just ignoring anything they type and
instead incrementing the amount of gas dispensed.

--
Marcus Kwok
Dec 12 '05 #2

P: n/a
Xiaoshen Li wrote:
I am reading a textbook "Absolute C++" by Walter Savitch.
A side note: this book has not been reviewed by ACCU's reviewers, but
if you look at accu.org , then name "Walter Savitch" does appear once and
his book is _NOT_ recommended. Of course this does not necessarily mean
all his books are of low quality.
After a
chapter talking about classes, a programming project puzzled me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed above. In
my class GasPump, there is a method start() and a method stop(). But:

GasPump myGasPump;

myGasPump.start(); //start pumping gas

//now, how can I stop pumping?

I am thinking using a boolean bHold. start() will set bHold = true and
stop() will set bHold = false. Inside start() method, once bHold is
true, the amount dispensed will keep increasing
void GasPump::start()
{
bHold = true;
double dAmount = 0.0;
while (bHold) //while handle is being pressed
{
dAmount += 0.1
} //now the handle has been released
//now the dAmount is the amount dispensed,
}
void GasPump::stop()
{
bHold = false;
}

But this seems getting into inter-processe communication(too complicated
to me) and I don't believe this is author's intention for this
programming project. I don't know how to deal with pressing a return
key, 0.1 gallons gas is added, either.

Could anybod kindly help me out? Thank you very much.


I think the author suggests to define the behaviour of the GasPump to pump
a small fixed amount of fuel at a request:

myGasPump.dispense_a_little_bit();

and then use this in a loop controlled by the input from the user, but
outside of the pump itself (IOW, the pump is not supposed to have any
user interface with console I/O).

You might (later) experiment with timing (using, say, 'time' function)
between pressing 'Enter' (or accepting user's input). Example (the #
character designates the cursor, --- designates thinking):

Pump Menu:
B<Enter> to begin pumping
X<Enter> to exit .......... #
---------------- <user enters 'B' and presses the 'Enter' key -------
----- The program takes the time reading and goes into new state -----
....pumping...
Pump Menu:
E<Enter> to stop pumping
P<Enter> to pause pumping ... #
----------------- <user enters 'E' and presses the 'Enter' key -------
------ The program takes the second time reading and calculates ------
---- the amount of fuel dispenced based on the time in seconds by ----
--- multiplying it by some amount of fuel-per-second (flow when the --
-- pump is opened) ---------------------------------------------------
.... done pumping: dispensed XXX.XXX litres ($XXXXXX.XX)
Pump Menu
$<Enter> to pay for gas
X<Enter> to try to escape without paying ... #

V
Dec 12 '05 #3

P: n/a
On 2005-12-12, Xiaoshen Li <xl**@gmu.edu> wrote:
Dear All,

I am reading a textbook "Absolute C++" by Walter Savitch. After
a chapter talking about classes, a programming project puzzled
me:

Write the difinition for a class named GasPump to be used to model a
pump at an automobile service station. .... Below are listed things a
gas pump might be expected to do.
a. A display of the amount dispensed.
b. A display of the amount charged for the amount dispensed.
...
f. Actual behavior of the gas pump is, once started, it dispenses as
long as you hold the
nozzle lever. Peculiarities of console I/O make it difficult to continue
to dispense while
waiting for a signal to stop. One solution is to model this behavior by
having the user
repeatedly press the return (enter) key, dispensing a quantum of fuel
and recomputing the
amount charged, say 0.1 gallons at each press.

I don't know how to let my object of class GasPump do f listed
above. In my class GasPump, there is a method start() and a
method stop().
I would implement a different interface. Timing strategies will
be tough without implementation specific libraries.

Your tank will accept commands to pump X number of units of
petrol, or X units of currency worth of gas.
PUMP 24 GALLONS Ding... ding... ding... ding... ding... ding... DONE.
You pumped 24 gallons and owe $60.72.
PUMP 5 DOLLARS

Ding... DONE.
You pumped 1.97 gallons and owe $5.

--
Neil Cerutti
Dec 13 '05 #4

P: n/a
Thank you very much for all the replies. I particularly like accu.org
web page.

Dec 13 '05 #5

P: n/a
what does this class apply to?
"Xiaoshen Li" <xl**@gmu.edu> ??????:dn***********@osf1.gmu.edu...
Thank you very much for all the replies. I particularly like accu.org web
page.

Dec 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.