m4_define([P],@tie{})
m4_changequote(,)

BO(Question 4.8.1:) Study, compile and run the following code which resides
in a file called TT(Box.jtw). Notice the use of TT(System.out.print())
to print without a trailing newline and TT(System.out.println()) to
print with a trailing newline. The EM(ln) part tells you this.

@example
CLASS Box
BEGIN
PD FUNCTION void square(int n)
PD BEGIN
PD PD SUPERFOR (var int y=0 TU n-1)
PD PD BEGIN
PD PD PD SUPERFOR (var int x=0 TU n-1)
PD PD PD BEGIN
PD PD PD PD IF ((x == 0) OR (x == n-1) OR (y == 0) OR (y == n-1))
PD PD PD PD THEN System.out.print("#");
PD PD PD PD ELSE System.out.print(" ");
PD PD PD END
PD PD PD System.out.println();
PD PD END
PD END
PD BEGINMAIN
PD PD square(5);
PD ENDMAIN
END
@end example

Notice that here is the output of the above code for different values
of the n parameter:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(###) EOL
     TT(#P#) EOL
     TT(###) EOL
EOL
@item n=4 EOL
@tab TT(####) EOL
     TT(#P #) EOL
     TT(#P #) EOL
     TT(####) EOL
EOL
@item n=5 EOL
@tab TT(#####) EOL
     TT(#P P#) EOL
     TT(#P P#) EOL
     TT(#P P#) EOL
     TT(#####) EOL
@end multitable

BO(Question 4.8.2:) By copying the pattern established in the above code,
write a now FUNCTION square2 that generates the following output. Note
that you will need to remove some of the or clauses in the square
method above to get the following output:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(###) EOL
     TT(   ) EOL
     TT(###) EOL
EOL
@item n=4 EOL
@tab TT(####) EOL
     TT(    ) EOL
     TT(    ) EOL
     TT(####) EOL
EOL
@item n=5 EOL
@tab TT(#####) EOL
     TT(     ) EOL
     TT(     ) EOL
     TT(     ) EOL
     TT(#####) EOL
EOL
@end multitable

BO(Question 4.8.3:) By copying the pattern established in the above code,
write a now FUNCTION square3 that generates the following output: n=1

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(#P#) EOL
     TT(#P#) EOL
     TT(#P#) EOL
EOL
@item n=4 EOL
@tab TT(#P #) EOL
     TT(#P #) EOL
     TT(#P #) EOL
     TT(#P #) EOL
EOL
@item n=5 EOL
@tab TT(#P P#) EOL
     TT(#P P#) EOL
     TT(#P P#) EOL
     TT(#P P#) EOL
     TT(#P P#) EOL
EOL
@end multitable

BO(Question 4.8.4:) Study, compile and run the following code which resides
in a file called Box.java:

@example
CLASS Box
BEGIN
PD FUNCTION void x(int n)
PD BEGIN
PD PD SUPERFOR (var int y=0 t0 n-1)
PD PD BEGIN
PD PD PD SUPERFOR (var int x=0 TU n-1)
PD PD PD BEGIN
PD PD PD PD IF ((x == y) OR (x == n-1-y)) then System.out.print("#");
PD PD PD PD ELSE System.out.print(" ");
PD PD PD END
PD PD PD System.out.println();
PD PD END
PD END

PD BEGINMAIN
PD PD x(5);
PD ENDMAIN
END
@end example

Notice that here is the output of the above code for different values
of the n parameter:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(# #) EOL
     TT(P#P) EOL
     TT(# #) EOL
EOL
@item n=4 EOL
@tab TT(#P #) EOL
     TT(P## ) EOL
     TT(P## ) EOL
     TT(#P #) EOL
EOL
@item n=5 EOL
@tab TT(# P #) EOL
     TT(P# # ) EOL
     TT(P #  ) EOL
     TT(P# # ) EOL
     TT(# P #) EOL
EOL
@end multitable

BO(Question 4.8.5:) By copying the pattern established in the above code,
write a now FUNCTION x2 that generates the following output. Note that
you will need to remove one of the or clauses in the TT(x) FUNCTION
above to get the following output:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(# ) EOL
     TT(P#) EOL
EOL
@item n=3 EOL
@tab TT(#  ) EOL
     TT(P# ) EOL
     TT(P #) EOL
EOL
@item n=4 EOL
@tab TT(#   ) EOL
     TT(P#  ) EOL
     TT(P # ) EOL
     TT(P P#) EOL
EOL
@item n=5 EOL
@tab TT(#    ) EOL
     TT(P#   ) EOL
     TT(P #  ) EOL
     TT(P P# ) EOL
     TT(P P #) EOL
EOL
@end multitable

BO(Question 4.8.6:) By copying the pattern established in the above code,
write a now FUNCTION TT(x3) that generates the following output. Note that
you will need to remove one of the or clauses in the x method above to
get the following output:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(P#) EOL
     TT(# ) EOL
EOL
@item n=3 EOL
@tab TT(P #) EOL
     TT(P# ) EOL
     TT(#  ) EOL
EOL
@item n=4 EOL
@tab TT(P P#) EOL
     TT(P # ) EOL
     TT(P#  ) EOL
     TT(#   ) EOL
EOL
@item n=5 EOL
@tab TT(P P #) EOL
     TT(P P# ) EOL
     TT(P #  ) EOL
     TT(P#   ) EOL
     TT(#    ) EOL
EOL
@end multitable

BO(Question 4.8.7:) Study, compile and run the following code which resides
in a file called Box.java:

@example
CLASS Box
BEGIN
PD FUNCTION void triangle(int n)
PD BEGIN
PD PD SUPERFOR (VAR int y=0 TU n-1)
PD PD BEGIN
PD PD PD SUPERFOR (VAR int x=0 TU n-1)
PD PD PD BEGIN
PD PD PD PD IF (x < y)
PD PD PD PD THEN System.out.print("#");
PD PD PD PD ELSE System.out.print(" ");
PD PD PD END
PD PD PD System.out.println();
PD PD END
PD END
PD BEGINMAIN
PD PD triangle(5);
PD ENDMAIN
END
@end example

Notice that here is the output of the above code for different values
of the n parameter: n=1

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(# ) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(#  ) EOL
     TT(## ) EOL
     TT(###) EOL
EOL
@item n=4 EOL
@tab TT(#   ) EOL
     TT(##  ) EOL
     TT(### ) EOL
     TT(####) EOL
EOL
@item n=5 EOL
@tab TT(#    ) EOL
     TT(##   ) EOL
     TT(###  ) EOL
     TT(#### ) EOL
     TT(#####) EOL
EOL
@end multitable

BO(Question 4.8.8:) By copying the pattern established in the above code,
write a new FUNCTION triangle2 that generates the following
output. Note that you will need to change the IF clause in the
triangle method above to get the following output: n=1

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(# ) EOL
EOL
@item n=3 EOL
@tab TT(###) EOL
     TT(## ) EOL
     TT(#  ) EOL
EOL
@item n=4 EOL
@tab TT(####) EOL
     TT(### ) EOL
     TT(##  ) EOL
     TT(#   ) EOL
EOL
@item n=5 EOL
@tab TT(#####) EOL
     TT(#### ) EOL
     TT(###  ) EOL
     TT(##   ) EOL
     TT(#    ) EOL
EOL
@end multitable

BO(Question 4.8.9:) Write a new FUNCTION called box that generates the
following output. Note that you will need to modify the triangle
method above to get the following output:

@multitable @columnfractions .25 .25
@item n=1 EOL
@tab TT(#) EOL
EOL
@item n=2 EOL
@tab TT(##) EOL
     TT(##) EOL
EOL
@item n=3 EOL
@tab TT(###) EOL
     TT(###) EOL
     TT(###) EOL
EOL
@item n=4 EOL
@tab TT(####) EOL
     TT(####) EOL
     TT(####) EOL
     TT(####) EOL
EOL
@item n=5 EOL
@tab TT(#####) EOL
     TT(#####) EOL
     TT(#####) EOL
     TT(#####) EOL
     TT(#####) EOL
EOL
@end multitable

BO(Question 4.8.10:) Add the following code to Box.java:

@cindex Introducing boolean arrays

@example
CLASS Grid
BEGIN
PD COMM(/* NOTE: the use of "final" below to denote a value whose value cannot be changed. */)
PD FINAL CLASSVAR int SIZE = 20;

PD COMM(/* NOTE: the array below is a two-dimensional array */)
PD CLASSVAR boolean[][] array = new boolean[SIZE][SIZE];

PD FUNCTION void set(int x, int y, boolean v)
PD BEGIN
PD PD IF ((x>=0) AND (x<SIZE) AND (y>=0) AND (y<SIZE)) THEN
PD PD BEGIN
PD PD PD array[x][y] = v;
PD PD END
PD END

PD FUNCTION void print(int size)
PD BEGIN
PD PD SUPERFOR (var int y=0 TU size-1)
PD PD BEGIN
PD PD PD SUPERFOR (var int x=0 TU size-1)
PD PD PD BEGIN
PD PD PD PD IF (array[x][y])
PD PD PD PD THEN System.out.print("#");
PD PD PD PD ELSE System.out.print(" ");
PD PD PD END
PD PD PD System.out.println();
PD PD END
PD PD System.out.println();) COMM(// prints an empty line between shapes)
PD END
END
@end example

@cindex How to access class variables and functions from another class
@cindex Accessing class variables and functions from another class
@cindex Class variables from another class, accessing

BO(Question 4.8.11:) The following question will guide you through the
process of making the drawing algorithm more powerful. Instead of
printing the shapes directly to the screen, they will be stored in an
array to be printed out only when the array has been completely
set. You don't need to know a great deal about arrays to answer the
remaining questions of this section as the array code has been written
for you in the TT(Grid) CLASS above. For every call to
TT(System.out.println()) in TT(Box.java), replace it with a call to
the TT(set) METHOD of the TT(Grid) CLASS. Note that the third
parameter in the TT(set) METHOD is of type BOOLEAN, that is to say it
must be either TRUE or FALSE. To call a FUNCTION of another CLASS you
need to prefix the name of the CLASS like so: TT(Grid.set(/* argument
values */)). Finally at the end of all of the functions in the TT(Box)
CLASS except for the MAIN FUNCTION you will need to call the
TT(Grid.print) method of the TT(Grid) CLASS to actually print out the
array.

BO(Question 4.8.12:) Re-initialize the boolean array array named
TT(array) from the MAIN FUNCTION of the TT(Box) CLASS.  BO(HINT:) to
access a CLASS variable from another CLASS, you need to prefix it with
the name of its CLASS name, in this case it is TT(Grid).
Re-initialize the TT(array) variable to a two-dimensional array of
dimensions 100 x 100.  Also set the TT(size) variable to 100 so that
the functions of the TT(Grid) class still work.

m4_changequote([,])

m4_undefine([P])

