Java Reference
In-Depth Information
/\
/ \
/ \
+------+
| |
| |
+------+
|United|
|States|
+------+
| |
| |
+------+
/\
/ \
/ \
It did so with a long sequence of println statements in the main method. In this
section you'll improve the program by using static methods for procedural decompo-
sition to capture structure and eliminate redundancy. The redundancy might be more
obvious, but let's start by improving the way the program captures the structure of
the overall task.
Structured Version
If you look closely at the output, you'll see that it has a structure that would be desir-
able to capture in the program structure. The output is divided into three subfigures:
the diamond, the X, and the rocket.
You can better indicate the structure of the program by dividing it into static meth-
ods. Since there are three subfigures, you can create three methods, one for each sub-
figure. The following program produces the same output as DrawFigures1 :
1 public class DrawFigures2 {
2 public static void main(String[] args) {
3 drawDiamond();
4 drawX();
5 drawRocket();
6 }
7
8 public static void drawDiamond() {
9 System.out.println(" /\\");
10 System.out.println(" / \\");
11 System.out.println(" / \\");
12 System.out.println(" \\ /");
13 System.out.println(" \\ /");
14 System.out.println(" \\ /");
15 System.out.println();
16 }
17
 
Search WWH ::




Custom Search