Systematic and Structured Programming
Step One - Simple Action Statements
The Problem to Be Solved
The beginning of the process is to identify the problem and try to elicit all the important actions that your program is required to do. Here is an example program specification:
- The program solves for the roots of a quadratic equation
- For the following equation, it prompts the user for, and acquires, the x2 coefficient (A), the x coefficient (B), and the constant coefficient (C), for the following equation:
- Finally, the program provides the roots of the equation to the user as a displayed output. The first step is to identify the main parts of the program and write them in as comments. This program will only require a few simple statements. The following statements are provided with explanations, and then the code itself is provided.
Ax2 + Bx + C = 0
Initialize the program
- there will always be program initialization actions. Examples are:
- declaring and/or defining variables
- initializing or seeding a random number generator that is used elsewhere in the program
- setting the title for the program
Get the coefficients from the user
- this is pretty straight forward; many programs, although not all, will need some kind of user input to get started
Process the quadratic roots
- also very common, although in some programs calculations may be made at different points
Display the roots
- when the calculations are complete, the user input and the resulting data can be displayed in some formatted or easily understandable way
End the program
- there will always be some kind of program shut down process. Examples are:
- holding the program until the user closes it
- shutting down I/O interfaces if any special ones are used
- returning memory to the operating system (advanced programs in other languages)
The main method code for step 1 would look like the following. Note that the class information is set aside to focus on the process:
public static void main( String[] args )
{
// initialize program
// get coefficients from user
// process quadratic roots
// display results
// end program
}
The program with the Java class information would be set up (in Project_1 and p1_package) as follows:
package p1_package;
public class QuadClass_StepOne
{
public static void main( String[] args )
{
// initialize program
// get coefficients from user
// process quadratic roots
// display results
// end program
}
}
As you may notice, it wouldn't take more than a few minutes to write this, and while it may seem trivial, it is very important because you now have a feeling for how the program will flow. Even more importantly, you have broken the program down into smaller, more manageable problems.
Some notes:
- Before you start programming anything, you will need to create a class structure in your IDE. This is shown above and will be demonstrated in the video below
- The comments should be placed into your basic program file, and saved in QuadClass_StepOne. Note that the class name is provided, then the step number, and optionally a version number (e.g., QuadClass_StepOne_v1). Your versions do not commonly change for steps one and two, but as your programs evolve, you may find that you have more versions of steps three and four. For purposes of this tutorial, version numbers are not provided in order to reduce complexity in the learning activities
- Note that there are fewer than ten (10) primary actions in the main method. If any method, main or otherwise, has too many primary or major actions (e.g., more than 8 to 10), the method must be broken down into smaller steps
- It is also important that your program compiles without any errors or warnings at this point. This may sound trivial, and it is, because your program will only be a shell of its future self. It will have the fundamental program components, which include initialization, input, processing, output, and program completion, it will have the main method, and it will otherwise mostly be comments. However, you need to make sure that these fundamental program components are all working now so they don't pile up as a longer list of problems later
- Finally, this chapter is written like a tutorial so that you can follow the process of developing a program from beginning to end. Make sure you follow the process and practice writing the code as you go through it
To see the step one process in action, watch this video; then develop your step one code with or without the video as needed in your own IDE.