Next: Tutorial 9 Mapping class variables to instance variables (also known as properties) and functions to methods, Previous: Tutorial 7 Non-Object arrays, Up: J.T.W. Tutorials [Contents][Index]
Question 4.8.1: Study, compile and run the following code which resides
in a file called Box.jtw. Notice the use of System.out.print()
to print without a trailing newline and System.out.println() to
print with a trailing newline. The ln part tells you this.
classBoxbeginfunctionvoid square(int n)beginsuperfor(var int y=0ton-1)beginsuperfor(var int x=0ton-1)beginif((x == 0)or(x == n-1)or(y == 0)or(y == n-1))thenSystem.out.print("#");elseSystem.out.print(" ");endSystem.out.println();endendbeginMainsquare(5);endMainend
Notice that here is the output of the above code for different values of the n parameter:
| n=1 | # |
| n=2 | ## ## |
| n=3 | ### # # ### |
| n=4 | #### # # # # #### |
| n=5 | ##### # # # # # # ##### |
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:
| n=1 | # |
| n=2 | ## ## |
| n=3 | ### ### |
| n=4 | #### #### |
| n=5 | ##### ##### |
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
| n=1 | # |
| n=2 | ## ## |
| n=3 | # # # # # # |
| n=4 | # # # # # # # # |
| n=5 | # # # # # # # # # # |
Question 4.8.4: Study, compile and run the following code which resides in a file called Box.java:
classBoxbeginfunctionvoid x(int n)beginsuperfor(var int y=0 t0 n-1)beginsuperfor(var int x=0ton-1)beginif((x == y)or(x == n-1-y)) then System.out.print("#");elseSystem.out.print(" ");endSystem.out.println();endendbeginMainx(5);endMainend
Notice that here is the output of the above code for different values of the n parameter:
| n=1 | # |
| n=2 | ## ## |
| n=3 | # # # # # |
| n=4 | # # ## ## # # |
| n=5 | # # # # # # # # # |
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 x function
above to get the following output:
| n=1 | # |
| n=2 | # # |
| n=3 | # # # |
| n=4 | # # # # |
| n=5 | # # # # # |
Question 4.8.6: By copying the pattern established in the above code,
write a now function 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:
| n=1 | # |
| n=2 | # # |
| n=3 | # # # |
| n=4 | # # # # |
| n=5 | # # # # # |
Question 4.8.7: Study, compile and run the following code which resides in a file called Box.java:
classBoxbeginfunctionvoid triangle(int n)beginsuperfor(varint y=0ton-1)beginsuperfor(varint x=0ton-1)beginif(x < y)thenSystem.out.print("#");elseSystem.out.print(" ");endSystem.out.println();endendbeginMaintriangle(5);endMainend
Notice that here is the output of the above code for different values of the n parameter: n=1
| n=1 | # |
| n=2 | # ## |
| n=3 | # ## ### |
| n=4 | # ## ### #### |
| n=5 | # ## ### #### ##### |
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
| n=1 | # |
| n=2 | ## # |
| n=3 | ### ## # |
| n=4 | #### ### ## # |
| n=5 | ##### #### ### ## # |
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:
| n=1 | # |
| n=2 | ## ## |
| n=3 | ### ### ### |
| n=4 | #### #### #### #### |
| n=5 | ##### ##### ##### ##### ##### |
Question 4.8.10: Add the following code to Box.java:
classGridbegin/* NOTE: the use of "final" below to denote a value whose value cannot be changed. */finalclassVarint SIZE = 20; /* NOTE: the array below is a two-dimensional array */classVarboolean[][] array = new boolean[SIZE][SIZE];functionvoid set(int x, int y, boolean v)beginif((x>=0)and(x<SIZE)and(y>=0)and(y<SIZE))thenbeginarray[x][y] = v;endendfunctionvoid print(int size)beginsuperfor(var int y=0tosize-1)beginsuperfor(var int x=0tosize-1)beginif(array[x][y])thenSystem.out.print("#");elseSystem.out.print(" ");endSystem.out.println();endSystem.out.println();) // prints an empty line between shapesendend
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 Grid class above. For every call to
System.out.println() in Box.java, replace it with a call to
the set method of the Grid class. Note that the third
parameter in the 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: Grid.set(/* argument
values */). Finally at the end of all of the functions in the Box
class except for the main function you will need to call the
Grid.print method of the Grid class to actually print out the
array.
Question 4.8.12: Re-initialize the boolean array array named
array from the main function of the Box class. 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 Grid.
Re-initialize the array variable to a two-dimensional array of
dimensions 100 x 100. Also set the size variable to 100 so that
the functions of the Grid class still work.