Java Reference
In-Depth Information
This instruction can be made more specific by introducing the idea of repeatedly
writing a single character on the output line and then moving to a new line of output:
for (each of 50 lines) {
for (each of 30 columns) {
write one asterisk on the output line.
}
go to a new output line.
}
Using pseudocode, you can gradually convert an English description into some-
thing that is easily translated into a Java program. The simple examples we've looked
at so far are hardly worth the application of pseudocode, so we will now examine the
problem of generating a more complex figure:
*********
*******
*****
***
*
This figure must also be generated line by line:
for (each of 5 lines) {
draw one line of the triangle.
}
Unfortunately, each line is different. Therefore, you must come up with a general
rule that fits all the lines. The first line of this figure has a series of asterisks on it
with no leading spaces. Each of the subsequent lines has a series of spaces followed
by a series of asterisks. Using your imagination a bit, you can say that the first line
has 0 spaces on it followed by a series of asterisks. This allows you to write a general
rule for making this figure:
for (each of 5 lines) {
write some spaces (possibly 0) on the output line.
write some asterisks on the output line.
go to a new output line.
}
In order to proceed, you must determine a rule for the number of spaces and a rule
for the number of asterisks. Assuming that the lines are numbered 1 through 5, look-
ing at the figure, you can fill in Table 2.8.
You want to find a relationship between line number and the other two columns.
This is simple algebra, because these columns are related in a linear way. The second
Search WWH ::




Custom Search