Fern   davin.50webs.com
New Zealanders making it harder to hate computers

       Main Menu        Research Projects       Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address     Computer Games        Web Design           Java Programming       The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      


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

SECTION(8, Tutorial 8) m5_question(BO(Question 8.1:) Study, compile and run the following code which resides in a file called TT(Box.sjs). Notice the use of SYSTEM_OUT_PRINT to print without a trailing newline and SYSTEM_OUT_PRINTLN to print with a trailing newline. The TT(BO(ln)) part tells you this. m4_begin_indent NU()CLASS CLSS(Box) EOL BEGIN EOL PD FUNCTION VOID FUNC(square)(INT VARI(n)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,n); y=y+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,n); x=x+NUMB(1)) EOL PD PD PD BEGIN EOL PD PD PD PD IF ((x == NUMB(0)) OR (x == n-NUMB(1)) OR (y == NUMB(0)) OR (y == n-NUMB(1))) EOL PD PD PD PD THEN SYSTEM_OUT_PRINT(STRI("HASH")); EOL PD PD PD PD ELSE SYSTEM_OUT_PRINT(STRI(" ")); EOL PD PD PD END EOL PD PD PD SYSTEM_OUT_PRINTLN(); EOL PD PD END EOL PD END EOL PD BEGIN_MAIN EOL PD PD square(NUMB(5)); EOL PD END_MAIN EOL END EOL m4_end_indent Notice that here is the output of the above code for different values of the TT(n) parameter: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
##
        
n=3
###
# #
###
        
n=4
####
#  #
#  #
####
        
n=5
#####
#   #
#   #
#   #
#####
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \#\# \\ \hline n = 3 & \#\#\# \\ & \#\phantom{\#}\# \\ & \#\#\# \\ \hline n = 4 & \#\#\#\# \\ & \#\phantom{\#\#}\# \\ & \#\phantom{\#\#}\# \\ & \#\#\#\# \\ \hline n = 5 & \#\#\#\#\# \\ & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ & \#\#\#\#\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.2:) By copying the pattern established in the above code, write a now function TT(square2) that generates the following output. Note that you will need to remove some of the EM(or) clauses in the TT(square) method above to get the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
##
        
n=3
###

###
        
n=4
####


####
        
n=5
#####



#####
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \#\# \\ \hline n = 3 & \#\#\# \\ & \\ & \#\#\# \\ \hline n = 4 & \#\#\#\# \\ & \\ & \\ & \#\#\#\# \\ \hline n = 5 & \#\#\#\#\# \\ & \\ & \\ & \\ & \#\#\#\#\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.3:) By copying the pattern established in the above code, write a now function TT(square3) that generates the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
##
        
n=3
# #
# #
# #
        
n=4
#  #
#  #
#  #
#  #
        
n=5
#   #
#   #
#   #
#   #
#   #
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \#\# \\ \hline n = 3 & \#\phantom{\#}\# \\ & \#\phantom{\#}\# \\ & \#\phantom{\#}\# \\ \hline n = 4 & \#\phantom{\#\#}\# \\ & \#\phantom{\#\#}\# \\ & \#\phantom{\#\#}\# \\ & \#\phantom{\#\#}\# \\ \hline n = 5 & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ & \#\phantom{\#\#\#}\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.4:) Study, compile and run the following code which resides in a file called TT(Box.java): m4_begin_indent NU()CLASS CLSS(Box) EOL BEGIN EOL PD FUNCTION VOID FUNC(x)(INT VARI(n)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,n); y=y+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,n); x=x+NUMB(1)) EOL PD PD PD BEGIN EOL PD PD PD PD IF ((x == y) OR (x == n-NUMB(1)-y)) THEN SYSTEM_OUT_PRINT(STRI("HASH")); EOL PD PD PD PD ELSE SYSTEM_OUT_PRINT(STRI(" ")); EOL PD PD PD END EOL PD PD PD SYSTEM_OUT_PRINTLN(); EOL PD PD END EOL PD END EOL PD BEGIN_MAIN EOL PD PD x(5); EOL PD END EOL END EOL m4_end_indent Notice that here is the output of the above code for different values of the TT(n) parameter: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
##
        
n=3
# #
 #
# #
        
n=4
#  #
 ##
 ##
#  #
        
n=5
#   #
 # #
  #
 # #
#   #
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \#\# \\ \hline n = 3 & \#\phantom{\#}\# \\ & \phantom{\#}\#\phantom{\#}\\ & \#\phantom{\#}\# \\ \hline n = 4 & \#\phantom{\#\#}\# \\ & \phantom{\#}\#\#\phantom{\#} \\ & \phantom{\#}\#\#\phantom{\#} \\ & \#\phantom{\#\#}\# \\ \hline n = 5 & \#\phantom{\#\#\#}\# \\ & \phantom{\#}\#\phantom{\#}\#\phantom{\#} \\ & \phantom{\#\#}\#\phantom{\#\#} \\ & \phantom{\#}\#\phantom{\#}\#\phantom{\#} \\ & \#\phantom{\#\#\#}\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.5:) By copying the pattern established in the above code, write a now function TT(x2) that generates the following output. Note that you will need to remove one of the EM(or) clauses in the TT(x) method above to get the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
#
 #
        
n=3
#
 #
  #
        
n=4
#
 #
  #
   #
        
n=5
#
 #
  #
   #
    #
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \# \\ & \phantom{a}\# \\ \hline n = 3 & \#\phantom{ab} \\ & \phantom{\#}\#\phantom{\#} \\ & \phantom{\#\#}\# \\ \hline n = 4 & \#\phantom{\#\#\#}\\ & \phantom{\#}\#\phantom{\#\#} \\ & \phantom{\#\#}\#\phantom{\#} \\ & \phantom{\#\#\#}\# \\ \hline n = 5 & \#\phantom{\#\#\#\#} \\ & \phantom{\#}\#\phantom{\#\#\#} \\ & \phantom{\#\#}\#\phantom{\#\#} \\ & \phantom{\#\#\#}\#\phantom{\#} \\ & \phantom{\#\#\#\#}\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 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 EM(or) clauses in the TT(x) method above to get the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
 #
#
        
n=3
  #
 #
#
        
n=4
   #
  #
 #
#
        
n=5
    #
   #
  #
 #
#
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \phantom{\#}\# \\ & \#\phantom{\#} \\ \hline n = 3 & \phantom{\#\#}\# \\ & \phantom{\#}\#\phantom{\#}\\ & \#\phantom{\#\#} \\ \hline n = 4 & \phantom{\#\#\#}\#\\ & \phantom{\#\#}\#\phantom{\#} \\ & \phantom{\#}\#\phantom{\#\#} \\ & \#\phantom{\#\#\#} \\ \hline n = 5 & \phantom{\#\#\#\#}\# \\ & \phantom{\#\#\#}\#\phantom{\#} \\ & \phantom{\#\#}\#\phantom{\#\#} \\ & \phantom{\#}\#\phantom{\#\#\#} \\ & \#\phantom{abcd} \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.7:) Study, compile and run the following code which resides in a file called TT(Box.java): m4_begin_indent NU()CLASS CLSS(Box) EOL BEGIN EOL PD FUNCTION VOID FUNC(triangle)(INT VARI(n)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,n); y=y+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,n); x=x+NUMB(1)) EOL PD PD PD BEGIN EOL PD PD PD PD IF (LT(x,y)) EOL PD PD PD PD THEN SYSTEM_OUT_PRINT(STRI("HASH")); EOL PD PD PD PD ELSE SYSTEM_OUT_PRINT(STRI(" ")); EOL PD PD PD END EOL PD PD PD SYSTEM_OUT_PRINTLN(); EOL PD PD END EOL PD END EOL PD BEGIN_MAIN EOL PD PD triangle(5); EOL PD END_MAIN EOL END EOL m4_end_indent Notice that here is the output of the above code for different values of the TT(n) parameter: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
#
##
        
n=3
#
##
###
        
n=4
#
##
###
####
        
n=5
#
##
###
####
#####
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \# \\ & \#\# \\ \hline n = 3 & \# \\ & \#\# \\ & \#\#\# \\ \hline n = 4 & \# \\ & \#\# \\ & \#\#\# \\ & \#\#\#\# \\ \hline n = 5 & \# \\ & \#\# \\ & \#\#\# \\ & \#\#\#\# \\ & \#\#\#\#\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.8:) By copying the pattern established in the above code, write a now function TT(triangle2) that generates the following output. Note that you will need to change the if clause in the TT(triangle) method above to get the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
#
        
n=3
###
##
#
        
n=4
####
###
##
#
        
n=5
#####
####
###
##
#
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \# \\ \hline n = 3 & \#\#\# \\ & \#\# \\ & \# \\ \hline n = 4 & \#\#\#\# \\ & \#\#\# \\ & \#\# \\ & \# \\ \hline n = 5 & \#\#\#\#\# \\ & \#\#\#\# \\ & \#\#\# \\ & \#\# \\ & \# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Question 8.9:) Write a now function called TT(box) that generates the following output. Note that you will need to modify the TT(triangle) method above to get the following output: PCLOSE m4_ifdef([m4_html],
n=1
#
        
n=2
##
##
        
n=3
###
###
###
        
n=4
####
####
####
####
        
n=5
#####
#####
#####
#####
#####
        
) m4_ifdef([m4_latex], \begin{center} \begin{tabular}{|c|l|} \hline n = 1 & \# \\ \hline n = 2 & \#\# \\ & \#\# \\ \hline n = 3 & \#\#\# \\ & \#\#\# \\ & \#\#\# \\ \hline n = 4 & \#\#\#\# \\ & \#\#\#\# \\ & \#\#\#\# \\ & \#\#\#\# \\ \hline n = 5 & \#\#\#\#\# \\ & \#\#\#\#\# \\ & \#\#\#\#\# \\ & \#\#\#\#\# \\ & \#\#\#\#\# \\ \hline \end{tabular} \end{center} ) POPEN) m5_question(BO(Querstion 8.10:) Add the following code to TT(Box.java): m4_begin_indent NU()CLASS CLSS(Grid) EOL BEGIN EOL PD SCOM(/** NOTE: the use of "final" below to denote a value whose value cannot be changed. */) EOL PD FINAL CLASS_VAR INT VARI(SIZE) = NUMB(20); EOL EOL PD SCOM(/** NOTE: the array below is a two-dimensional array */) EOL PD CLASS_VAR BOOLEAN[][] VARI(array) = NEW BOOLEAN[SIZE][SIZE]; EOL EOL PD FUNCTION VOID FUNC(set)(INT VARI(x), INT VARI(y), BOOLEAN VARI(v)) EOL PD BEGIN EOL PD PD IF (GTEQ(x,NUMB(0))) AND (LT(x,SIZE)) AND (GTEQ(y,NUMB(0))) AND (LT(y,SIZE)) THEN EOL PD PD BEGIN EOL PD PD PD array[x][y] = v; EOL PD PD END EOL PD END EOL EOL PD FUNCTION VOID FUNC(print)(INT VARI(size)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,size); y=y+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,size); x=x+NUMB(1)) EOL PD PD PD BEGIN EOL PD PD PD PD IF (array[x][y]) EOL PD PD PD PD THEN SYSTEM_OUT_PRINT(STRI("HASH")); EOL PD PD PD PD ELSE SYSTEM_OUT_PRINT(STRI(" ")); EOL PD PD PD END EOL PD PD PD SYSTEM_OUT_PRINTLN(); EOL PD PD END EOL PD PD SYSTEM_OUT_PRINTLN(); COMM(// prints an empty line between shapes) EOL PD END EOL END EOL m4_end_indent ) m5_question(BO(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 CLSS(Grid) class above. For every call to SYSTEM_OUT_PRINTLN in TT(Box.java), replace it with a call to the set method of the CLSS(Grid) class. Note that the third parameter in the TT(set) method is of type EM(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: TT(CLSS(Grid).set(COMM(/* argument values */))). Finally at the end of all of the functions in the CLSS(Box) class except for the MAIN function you will need to call the TT(print(n)) method of the CLSS(Grid) class to actually print out the array.)
Back to Main Menu
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Programming | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |

Please report any broken links to the Web-person
Last modified: Tue Mar 25 12:26:54 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.