Java Reference
In-Depth Information
This code produces the following output:
====================
> <
>> <<
>>> <<<
>>>> <<<<
>>>>> <<<<<
>>>>>> <<<<<<
>>>>>>> <<<<<<<
>>>>>>>> <<<<<<<<
>>>>>>>>> <<<<<<<<<
>>>>>>>>>><<<<<<<<<<
Using the writeChars method we can write an even better version of the drawTop
method from the DrawFigure2 program of Chapter 2. We saw earlier that using
writeChars we could eliminate two of the inner loops, but this left us with the inner
loop to print dots. Now we can eliminate all three of the inner loops and produce a much
more readable version of the method:
public static void drawTop() {
for (int line = 1; line <= SUB_HEIGHT; line++) {
System.out.print("|");
writeChars(' ', line - 1);
System.out.print("\\");
writeChars('.', 2 * SUB_HEIGHT - 2 * line);
System.out.print("/");
writeChars(' ', line - 1);
System.out.println("|");
}
}
You can include as many parameters as you want when you define a method. Each
method call must provide exactly that number of parameters, in the same order. For
example, consider the first call on writeChars in the preceding code fragment and
the header for writeChars . Java lines up the parameters in sequential order (with the
first actual parameter going into the first formal parameter and the second actual
parameter going into the second formal parameter):
writeChars('=', 20);
public static void writeChars(char ch, int number) {
...
}
Search WWH ::




Custom Search