Java Reference
In-Depth Information
Using the writeSpaces method, you can rewrite this as follows:
public static void drawTop() {
for (int line = 1; line <= SUB_HEIGHT; line++) {
System.out.print("|");
writeSpaces(line - 1);
System.out.print("\\");
int dots = 2 * SUB_HEIGHT - 2 * line;
for (int i = 1; i <= dots; i++) {
System.out.print(".");
}
System.out.print("/");
writeSpaces(line - 1);
System.out.println("|");
}
}
Notice that writeSpaces is called two different times, specifying how many
spaces are required in each case. You could modify the drawBottom method from the
DrawFigure2 program similarly to simplify it.
The Mechanics of Parameters
When Java executes a call on a method, it initializes the method's parameters. For
each parameter, it first evaluates the expression passed as the actual parameter and
then uses the result to initialize the local variable whose name is given by the formal
parameter. Let's use an example to clarify this process:
1 public class ParameterExample {
2 public static void main(String[] args) {
3 int spaces1 = 3;
4 int spaces2 = 5;
5
6 System.out.print("*");
7 writeSpaces(spaces1);
8 System.out.println("*");
9
10 System.out.print("!");
11 writeSpaces(spaces2);
12 System.out.println("!");
13
14 System.out.print("'");
15 writeSpaces(8);
16 System.out.println("'");
 
Search WWH ::




Custom Search