Programmers normally decide what to put in their programs using the “Top-Down” method of design. This is often referred to as divide and conquer programming. Once you have your problem defined, you break it down into smaller parts. You then take the smaller parts and break them down into still smaller parts. You keep doing this until you have a very small program that is easy to solve.
You probably already do this regularly in
math and science class. For example, if you were given the problem “Find
the area of this shape”,
|
First you would divide the shape into smaller shapes. |
Then, you would find the area of each of the smaller shapes. When you had the smaller areas, you would add them together to find the area of the entire shape. Thus, you would have solved the problem by subdividing it like this:
It works exactly the same way in programming. To explore a more complex example, recall that digital clocks have displays for each number that look something like the picture shown on the side.
The bars are filled in according to what number is desired.

Suppose then that we are given the following problem: “Write a program that displays the numbers 4-8 using the digital clock format for numbers”. Admittedly, this is a rather useless program, but it is a simple one which will allow us to understand how top-down design works.
The first obvious division to this problem is to separate out the modules for 4 through 8.

To figure out the sub-modules of each number,
let us analyse the digital clock.
|
This means that there are only two regions:
The horizontal region can be coloured in or clear. The vertical region can be both coloured in, left coloured in, or right coloured in. That makes 5 subroutines. |

The chart would be very similar for 5, 6, 7 and 8.
At this point, we are ready to start coding. We know that we need these functions:
The code (uncommented) follows. Note how simple the main program is:
import java.io.*;
public class clock
{
public static void main (String args [])
{
four ();
five ();
six ();
seven ();
eight ();
}
public static void h_full ()
{
System.out.println( " *****" );
}
public static void h_empty ()
{
System.out.println();
}
public static void v_left ()
{
System.out.println( " *" );
System.out.println( " *" );
}
public static void v_right ()
{
System.out.println( " *" );
System.out.println( " *" );
}
public static void v_both ()
{
System.out.println( " * *" );
System.out.println( " * *" );
}
public static void space ()
{
System.out.println( );
}
public static void four ()
{
space ();
h_empty ();
v_both ();
h_full ();
v_right ();
h_empty ();
}
public static void five ()
{
space ();
h_full ();
v_left ();
h_full ();
v_right ();
h_full ();
}
public static void six ()
{
space ();
h_full ();
v_left ();
h_full ();
v_both ();
h_full ();
}
public static void seven ()
{
space ();
h_full ();
v_right ();
v_right ();
v_right ();
h_empty ();
}
public static void eight ()
{
space ();
h_full ();
v_both ();
h_full ();
v_both ();
h_full ();
}
}
Structure charts are used to represent the modules that you have designed using top-down design. They are of the form:

Obviously, the number of modules change based on the problem. There are examples of structure charts in the section on Top-Down Programming.