473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to define a class GasPump?

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
5 3772
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
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
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
Thank you very much for all the replies. I particularly like accu.org
web page.

Dec 13 '05 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
6
by: David Young | last post by:
Hello all, I'm quite new to C# (< 6 months) but really love it and is my language of choice ..... but I have one question I've not been able to find out ..... In C++ a #define label in one...
18
by: **Developer** | last post by:
I always define events with the parameters ByVal sender As Object, ByVal e As EventArgs Even if they are not used. Seems I read someplace that's the thing to do. So I then do:
5
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from...
5
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
7
by: Nobody | last post by:
Anyone have a clean way of solving this define issue? In Windows, there are sometimes unicode functions and multibyte functions... the naming convention used is FunctionA for multibyte and...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.