Next: , Previous: , Up: J.T.W. Tutorials   [Contents][Index]


4.8 Tutorial 8 Accessing functions and class variables from another class

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.

class Box
begin
    function void square(int n)
    begin
        superfor (var int y=0 to n-1)
        begin
            superfor (var int x=0 to n-1)
            begin
                if ((x == 0) or (x == n-1) or (y == 0) or (y == n-1))
                then System.out.print("#");
                else System.out.print(" ");
            end
            System.out.println();
        end
    end
    beginMain
        square(5);
    endMain
end

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:

class Box
begin
    function void x(int n)
    begin
        superfor (var int y=0 t0 n-1)
        begin
            superfor (var int x=0 to n-1)
            begin
                if ((x == y) or (x == n-1-y)) then System.out.print("#");
                else System.out.print(" ");
            end
            System.out.println();
        end
    end

    beginMain
        x(5);
    endMain
end

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:

class Box
begin
    function void triangle(int n)
    begin
        superfor (var int y=0 to n-1)
        begin
            superfor (var int x=0 to n-1)
            begin
                if (x < y)
                then System.out.print("#");
                else System.out.print(" ");
            end
            System.out.println();
        end
    end
    beginMain
        triangle(5);
    endMain
end

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:

class Grid
begin
    /* NOTE: the use of "final" below to denote a value whose value cannot be changed. */
    final classVar int SIZE = 20;

    /* NOTE: the array below is a two-dimensional array */
    classVar boolean[][] array = new boolean[SIZE][SIZE];

    function void set(int x, int y, boolean v)
    begin
        if ((x>=0) and (x<SIZE) and (y>=0) and (y<SIZE)) then
        begin
            array[x][y] = v;
        end
    end

    function void print(int size)
    begin
        superfor (var int y=0 to size-1)
        begin
            superfor (var int x=0 to size-1)
            begin
                if (array[x][y])
                then System.out.print("#");
                else System.out.print(" ");
            end
            System.out.println();
        end
        System.out.println();) // prints an empty line between shapes
    end
end

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.


Next: , Previous: , Up: J.T.W. Tutorials   [Contents][Index]