Steps for Developing Programs
The following is a discussion of steps you can take in developing a program.
The method described below should work well for most programs you write for the
introductory programming courses.
First, read the specifications for the program very carefully. Make sure you
know what each sentence is telling you. If you encounter anything you don't
understand, be sure to ask your instructor about it.
Most elementary programs follow a simple pattern: Input-Process-Output, or I-P-O.
This means that during execution, the program will ask for some data to work with
(the input), will then perform some calculations (the processing), and then will
print out the results (the output). Recognizing this pattern in any program's
specifications is a good way to start.
- Most program specifications given in introductory classes include a sample run of the expected
program, i.e.
you are shown what could appear on your screen when the program executes. This is
a good first place to start studying the specifications after reading through them
a few times. You should identify the following things: (1) input values (i.e. those data
typed by a user of the program in response to some prompt); (2) output values (i.e.
those that would have been calculated by the program); and (3) string literals used
either as prompt messages or output labels. Once you have identified these components,
you have already begun part of the work of creating the program. Why? Because each
input value and each output value can be thought of as a variable you need to declare
within your program somewhere. Of course, there are usually sections in the specifications
for both input and output, so match what you are figuring out from the sample run to the
input and output specification sections.
- For each input value, decide on a name for a variable to represent it (make sure the name reflects
what the variable holds), and its
type (is it a string, a single character, an integer, or floating point number...).
Frequently the lab specifications will tell you somewhere in the text just what types
the inputs must be (probably in the Input Specifications section). Write down a
declaration for each of these values (which you will fit into your program code eventually).
- Do the same thing as in (2) with the output values. You may not be explicitly told
what type these values are in the lab specifications, but you should be able to deduce
them from the sample output at least (e.g. do numbers that are output have decimal points?).
Read the Output Specifications section for details about how many output values there are,
what type they are, and what format they should appear in when printed (e.g. how many decimal places).
- What calculations are needed? You need to think about what formulas you will need at
some point. Do you need to convert values from one unit of measure to another? How are
these conversions done? Is there a mathematical formula involved? What is it? These
formulas undoubtedly take the values that were input and transform them into the values
you will want to output. In other words, it is likely that most formulas will use the
input variables in some way, with the result of the calculation stored into (i.e. assigned
to) an output variable. You may need some additional variables or constants for these
formulas, so provide declarations for those as needed. For instance, if your formula
involves p
, you will want to declare a global constant for its value (called, for example, pi),
then use the identifier (pi) in the formula.
With your notes from these preliminary steps, you are now ready to begin designing
the actual program. Where to start? Well, the simplest program you could write is one
that will declare your input variables, input values into them, and then just print them
back out to the user. You can do this without getting involved in methods immediately.
If you can't figure out how else to start, just make a program that contains just the
import statements you need, and a main method. Within the main method, write code
to do the following, in this order:
- Declare all of your input variables.
- For each input variable, output a prompt message to the user, then read in a value.
- For each input variable, output a label to the screen, followed by the value of the variable.
In other words, the program should just read values in, then print them right back out
again without doing any processing. A very primitive program, to be sure, but a first
step that will hopefully get you thinking in the right directions. Get this much to
compile and execute correctly.
The earliest labs during the semester will often include a section on Approaching the
Problem, which provides incremental suggestions on how to develop the program. Try
to get each suggested step to work, in the order they are given. Each step should lead
you closer to the final solution. Realize, of course, that some of the code you write
for a particular suggested step may have to be changed - or even eliminated - in later steps.
For more complex problems, the structure of the solution will need to be broken down into functional
methods that each solve a smaller part of the problem. A discussion of this process is beyond the scope
of this simple programming tip...