GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Tutorial 12 Tutorial 13 Tutorial 14 Tutorial 15
Tutorial 16 Tutorial 17 Tutorial 18 Using Emacs Download Links


J.T.W. tutorial 8: loops and accessing functions and class variables from another class


§ 8 Tutorial 8

Question 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
      for (var int y=0; y<n; y=y+1)
      begin
         for (var int x=0; x<n; x=x+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 8.2: By copying the pattern established in the above code, write a new 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 8.3: By copying the pattern established in the above code, write a now function square3 that generates the following output:

n=1
#
        
n=2
##
##
        
n=3
# #
# #
# #
        
n=4
#  #
#  #
#  #
#  #
        
n=5
#   #
#   #
#   #
#   #
#   #
        

Question 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
      for (var int y=0; y<n; y=y+1)
      begin
         for (var int x=0; x<n; x=x+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);
   end
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 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 method above to get the following output:

n=1
#
        
n=2
#
 #
        
n=3
#
 #
  #
        
n=4
#
 #
  #
   #
        
n=5
#
 #
  #
   #
    #
        

Question 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 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
      for (var int y=0; y<n; y=y+1)
      begin
         for (var int x=0; x<n; x=x+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=2
#
##
        
n=3
#
##
###
        
n=4
#
##
###
####
        
n=5
#
##
###
####
#####
        

Question 8.8: By copying the pattern established in the above code, write a now 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=2
##
#
        
n=3
###
##
#
        
n=4
####
###
##
#
        
n=5
#####
####
###
##
#
        

Question 8.9: Write a now 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 8.10: Add the following code to Box.java:

class Grid
begin
   /** The dimensions of the array named: array. */
   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
      for (var int y=0; y<size; y=y+1)
      begin
         for (var int x=0; x<size; x=x+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 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 can 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 print(int) method of the Grid class to actually print out the array.

Question 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.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Fri 30 Jul 2021 19:01:05 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2021 Davin Pearson.
Please report any broken links to