Java Reference
In-Depth Information
One final problem-solving term has to do with the process of programming.
Professional programmers develop programs in stages. Instead of trying to produce a
complete working program all at once, they choose some piece of the problem to
implement first. Then they add another piece, and another, and another. The overall
program is built up slowly, piece by piece. This process is known as iterative
enhancement or stepwise refinement.
Iterative Enhancement
The process of producing a program in stages, adding new functionality at
each stage. A key feature of each iterative step is that you can test it to
make sure that piece works before moving on.
Now, let's look at a construct that will allow you to iteratively enhance your Java
programs to improve their structure and reduce their redundancy: static methods.
Static Methods
Java is designed for objects, and programming in Java usually involves decomposing
a problem into various objects, each with methods that perform particular tasks. You
will see how this works in later chapters, but for now, we are going to explore proce-
dural decomposition. We will postpone examining some of Java's details while we
discuss programming in general.
Consider the following program, which draws two text boxes on the console:
1 public class DrawBoxes {
2 public static void main(String[] args) {
3 System.out.println("+------+");
4 System.out.println("| |");
5 System.out.println("| |");
6 System.out.println("+------+");
7 System.out.println();
8 System.out.println("+------+");
9 System.out.println("| |");
10 System.out.println("| |");
11 System.out.println("+------+");
12 }
13 }
The program works correctly, but the four lines used to draw the box appear twice.
This redundancy is undesirable for several reasons. For example, you might wish to
change the appearance of the boxes, in which case you'll have to make all of the edits
twice. Also, you might wish to draw additional boxes, which would require you to
type additional copies of (or copy and paste) the redundant lines.
 
Search WWH ::




Custom Search