CS 150 Introduction to C++
Programming - Spring 1998
|
[ Home | Lecture Notes| WebTutor | WebTutor Site Map]
Commentary on Incremental Design
This frame contains commentary on the
developing incremental design shown in the right frame.
It is also used to move through the
developing design stages. You can always start over by clicking here.
When you are ready, click here to show the initial skeletal
design.
WATER Step 1:
Basically this is the skeletal C++ program
with comments indicating where certain parts of the C++
program should go.
We assume that (like most programs), there is an input,
process, output structure.
This program will compile and execute, but of course does
nothing.
Now let's give the specification for this case study: the
Water Usage Problem
For planning purposes The Water Company
wants to classify the water usage of their customers.
The water company has two kinds of customers: Home
users and Industrial Users.
INPUT STEP: This program takes as
input
1) the category of the customer ('H' or 'h' for home
user and 'I' or 'i' for industrial user)
2) the number of gallons consumed last billing period
PROCESS STEP:It classifies home
users as "efficient" is they use less than
3500 gallons, or "abnormal" otherwise.
It classifies industrial users as "small"
if they use less than 15000 gallons,
"medium" if they use between 15000 and
30000 gallons and "large" otherwise
OUTPUT STEP: It then prints out the
appropriate classification. |
Let's start by refining the INPUT STEP.
Click here for
WATER step 2:
(for this and subsequent versions, we will show the new
part of the design in BOLD red
letters and the old part in italics)
Since there are two input values, we will need two variables
to store them.
Since the category of the customer is a single character, it
is natural to choose the C++ char data type
to remember this input. We decide to call this variable, cat
(short for category).
Since the numbers of gallons is a whole number, we decide to
store it in a C++ int data type called gallon.
Now we design the input part of the program. We decide to
get the input from the keyboard, so we need to prompt the
user so they know which input is being requested when.
Notice for this refinement, we added only two C++
statements (the declarations of the variables cat
and gallon. The rest of the refinement is still
commentary. WHY? because at this stage we don't want to worry
about getting the syntax of the C++ correct. We want to focus
on solving the problem in English first, then we can worry
about making it into legal C++ (in a later refinement).
However, we did feel confident enough about declarations to
put them directly into C++. And they should compile correctly
even at this stage.
Click here for
WATER step 3:
We decide to protect the program from typing mistakes by
validating the input value and terminating the program if the
user makes a mistake.
Notice how we use a selection statement ("if") to
distinguish between valid and invalid input values. Again
notice that this design is expressed as English comments
statements with some C++ control stuff (The "if"
part of the comment). Some programmers call this "Structured
English" or pseudoCode.
Since it is a comment, we don't care how to
compute"// if(gallon is ridiculous)", for instance.
At this point, we should feel the design will get the job
done and that we will eventually be able to translate the
design into C++.
Copyright chris wild 1998.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 10, 1998.