Systematic and Structured Programming, Part 2
Step One - Simple Action Statements (Reviewed)
Once More, for Extra Support
This page is almost identical to the Step One page in Chapter 2. There are a few changes but Step One does not change whether you create your own methods or not. You may want to scan through this chapter and move on to the next one. However, it never hurts to review something this important to watch for things you might have missed in your first pass through it.
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 any I/O interface systems, such as the formatted I/O tools
- 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 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 or memory management if any special ones are used
- ending the program with a printed statement
The main method code for step one would look like the following:
public static void main( String[] args )
{
// initialize program
// get coefficients from user
// process quadratic roots
// display roots
// end program
}
As you may notice, and as you have up to now experienced, it doesn'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 should have already set up a new project and package to manage your program code
- The comments should be placed into your basic program file, and saved as QuadClass_StepOne_v1.java. Note that the program name is used, then the step number, and finally a version number. Your versions do not commonly change for steps one and two, but as your programs evolve, you will find that you have more versions of steps three through six. Also note that for this tutorial, version numbers are not always 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
- Note also that a couple of lines will be indented as the program progresses. These are actions that represent smaller parts of a primary or major action. The indenting makes your design process clearer, and helps you separate the important steps of the program from less-important details so that you are clear on the overall flow of the program
- 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 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, like the Four Step Programming Process chapter, 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.