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 10: object arrays

SECTION(10, Tutorial 10) m5_question(This tutorial teaches you how to create single dimensional and multi-dimensional arrays of objects. The object types are all types execpt for EM(boolean), EM(char), EM(int), EM(float) and EM(double). A helpful convention in Java is that the Object types start with an uppercase letter, while non-object types start with a lowercase letter, such as for example the STRING class as an example of an object type. In addition to this, two different array initialisation syntaxes are presented.) SUBSECTION(10.1, Single dimensional arrays) m5_question(BO(Question 10.1:) Here is an example of a convenient one dimensional array initialisation syntax. Study, compile and run the following code. The code CLSS(Person)TT([[[]]]) should be read out loud as EM(person array) indicating the the variable TT(a) is a person array, also known as an array of persons. m4_begin_indent NU()CLASS CLSS(Person) EOL BEGIN EOL PD PRIVATE PROPERTY STRING VARI(name); EOL EOL PD PUBLIC CONSTRUCTOR CLSS(Person)(STRING VARI(aName)) EOL PD BEGIN EOL PD PD name = aName; EOL PD END EOL EOL PD PUBLIC STRING FUNC(toString)() EOL PD BEGIN EOL PD PD RETURN name; EOL PD END EOL END EOL EOL CLASS CLSS(PersonTest) EOL BEGIN EOL PD BEGIN_MAIN EOL PD PD VAR CLSS(Person)[] VARI(a) = SQU NEW CLSS(Person)(STRI("Person HASH 1")), NEW CLSS(Person)(STRI("Person HASH 2")), NEW CLSS(Person)(STRI("Person HASH 3")) GLY; EOL EOL PD PD FUR (VAR INT VARI(i)=NUMB(0); LT(i,NUMB(3)); i=i+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD SYSTEM_OUT_PRINTLN(STRI("a[") + i + STRI("]=") + a[i]); EOL PD PD END EOL PD END_MAIN EOL END EOL m4_end_indent NU()Due to a design oversight by the creators of Java you cannot use this syntax to re-initialise an array like so: m4_begin_indent NU()COMM(// Compilation error) EOL a = SQU NEW CLSS(Person)(STRI("Person HASH 4")), NEW CLSS(Person)(STRI("Person HASH 5")), NEW CLSS(Person)(STRI("Person HASH 6")), NEW CLSS(Person)(STRI("Person HASH 7")) GLY; EOL m4_end_indent NU()Luckily there is a way array around this oversight and that is to use a design pattern where you introduce a temporary variable like so: m4_begin_indent NU()COMM(// No error) EOL VAR CLSS(Person)[] VARI(temp) = SQU NEW CLSS(Person)(STRI("Person HASH 4")), NEW CLSS(Person)(STRI("Person HASH 5")), NEW CLSS(Person)(STRI("Person HASH 6")), NEW CLSS(Person)(STRI("Person HASH 7")) GLY; EOL a = temp; COMM(// Array "a" now holds Person HASH 4, Person HASH 5, Person HASH 6, Person HASH 7) EOL m4_end_indent NU()Later you will learn why this design pattern is useful for re-initialising multi-dimensional arrays. ) m5_question(BO(Question 10.2:) Write a function in the class CLSS(PersonTest) called TT(print) that takes a CLSS(Person) array argument and prints out the array. You will need to use the TT(length) property of the array parameter so your function works with arbitrary sized arrays. Change the MAIN function to what follows so that it contains a call to the TT(print) function. m4_begin_indent NU()VAR CLSS(Person)[] VARI(a) = SQU NEW CLSS(Person)(STRI("Person HASH 1")), NEW CLSS(Person)(STRI("Person HASH 2")), NEW CLSS(Person)(STRI("Person HASH 3"))GLY; EOL print(a); EOL m4_end_indent ) m5_question(BO(Question 10.3:) Write your own class called CLSS(Mine) similar to the CLSS(Person) class with a one int parameter constructor, a private int property TT(p) and a TT(toString) method that converts TT(p) to a string. Then write a function in the CLSS(PersonTest) class with same name as the previous TT(print) function, except that this one takes a CLSS(Mine)TT([[[]]]), also known as a CLSS(Mine) array. You might recall from TUTE_ARRAYS1 that this practice of having two functions with the same name is called EM(function name overloading). Change the MAIN function to what follows so that it initialises an array of CLSS(Mine) point variables and then calls the second TT(print) function. m4_begin_indent NU()VAR CLSS(Mine)[] VARI(b) = SQU NEW CLSS(Mine)(NUMB(1)), NEW CLSS(Mine)(NUMB(2)), NEW CLSS(Mine)(NUMB(3)) GLY; EOL print(b); EOL m4_end_indent ) m5_question(Here is an example of a second initialisation syntax. For this particular example it is better to use the simpler, earlier initialisation syntax, but when the size of the array to be created is to be determined at run-time, then the second syntax should used. The next question will show you an example of this. m4_begin_indent NU()BEGIN_MAIN EOL PD VAR CLSS(Person)[] VARI(a) = NEW CLSS(Person)[NUMB(3)]; EOL PD COMM(// at this point the array is all nulls) EOL PD FUR (VAR INT VARI(i)=NUMB(0); LT(i,NUMB(3)); i=i+NUMB(1)) EOL PD BEGIN EOL PD PD a[i] = NEW CLSS(Person)(STRI("Person HASH ") + (i+NUMB(1))); EOL PD END EOL PD print(a); EOL END_MAIN EOL m4_end_indent ) m5_question(BO(Question 10.4:) Write a function TT(create) takes one INT argument, the size of the array to create and returns a CLSS(Person) array of that size. Make it so the TT(i)SUP(th) element of the array is initialised to TT(STRI("Person HASH ") + i). Call this function from the MAIN function like so: m4_begin_indent NU()BEGIN_MAIN EOL PD VAR CLSS(Person)[] VARI(a) = create(NUMB(3)); EOL PD print(a); EOL END_MAIN EOL m4_end_indent ) m5_question(BO(Question 10.5:) Write a function TT(create2) takes one int argument, the size of the array to create and returns a CLSS(Mine) array of that size. Make it so the TT(i)SUP(th) element of the array's TT(toString) method prints out TT(STRI("Mine HASH ") + i). Why is it not possible to overload that TT(create) function? Try it and see what the compiler says. Call TT(create2) from the MAIN function like so: m4_begin_indent NU()BEGIN_MAIN EOL PD VAR CLSS(Mine)[] VARI(a) = create2(NUMB(3)); EOL PD print(a); EOL END_MAIN EOL m4_end_indent ) m5_question(BO(Question 10.6:) Write a function TT(doubler) that takes a CLSS(Person) array TT(x) and returns a new CLSS(Person) array called TT(result) twice as big as TT(x). Copy TT(x) into the TT(result) before you return it. The extra elements in TT(result) should all be NULL.) m5_question(BO(Question 10.7:) Change the TT(doubler) function so that every NULL in the array TT(result) is set to a new CLSS(Person) make it so that every new CLSS(Person) object has a different TT(name) property.) SUBSECTION(10.2, Two dimensional arrays) m5_question(BO(Question 10.8:) Here is an example of a convenient two dimensional array initialisation syntax. Study, compile and run the following code. The code CLSS(Person)TT([[[][]]]) should be read out loud as EM(person array array) indicating the the variable TT(a) is a person array array, also known as a two dimensional array of persons. m4_begin_indent NU()BEGIN_MAIN EOL PD VAR CLSS(Person)[][] a = SQU SQU NEW CLSS(Person)(STRI("Person HASH 1")), NEW CLSS(Person)(STRI("Person HASH 2")), NEW CLSS(Person)(STRI("Person HASH 3")) GLY, EOL PD PD PD PD PD PD PD SQU NEW CLSS(Person)(STRI("Person HASH 4")), NEW CLSS(Person)(STRI("Person HASH 5")) GLY, EOL PD PD PD PD PD PD PD SQU NEW CLSS(Person)(STRI("Person HASH 6")) GLY GLY; EOL EOL PD FUR (VAR INT VARI(y)=NUMB(0); LT(y,a.length); y=y+NUMB(1)) EOL PD BEGIN EOL PD PD FUR (VAR INT VARI(x)=NUMB(0); LT(x,a[y].length); x=x+NUMB(1)) EOL PD PD BEGIN EOL PD PD PD SYSTEM_OUT_PRINT(STRI(" ") + a[y][x]); EOL PD PD END EOL PD PD SYSTEM_OUT_PRINTLN(); EOL PD END EOL END_MAIN EOL m4_end_indent ) m5_question(BO(Question 10.9:) By copying the pattern of the code above, do some more overloading of the TT(print) function by writing two new TT(print) functions, one taking a two dimensional array of CLSS(Person), the other taken a two dimensional array of CLSS(Mine). The call both of these functions from the MAIN function.) m5_question([Since a[0] is a Person array, you would naively expect it to be able to be re-initialised like so:
NU()a[0] = SQU NEW Person("Person HASH 4"), EOL PD PD PD NEW Person("Person HASH 5"), EOL PD PD PD NEW Person("Person HASH 6") GLY; EOL
NU()so that after this code a0 holds the four element long array Person HASH 4,Person HASH 5 and Person HASH 6, but it does't work owing to a design oversight by the creators of Java. Luckily as mentioned above there is a way around this oversight and that is to use a temporary variable like so:
NU()VAR Person[] temp = SQU NEW Person("Person HASH 4"), EOL PD PD PD PD PD PD PD PD NEW Person("Person HASH 5"), EOL PD PD PD PD PD PD PD PD NEW Person("Person HASH 6") GLY; EOL a[0] = temp; // Array "a[0]" now holds Person HASH 4,Person HASH 5,Person HASH 6 EOL
NU()Like with one dimensional arrays, there is a second initialisation syntax for two dimensional arrays and here it is. Unlike the above code the sub-arrays a[0], a[1] and a[2] are all of equal size, namely three.
NU()VAR Person[][] a = NEW Person[3][3]; EOL a[0][0] = NEW Person("Person HASH 1"); EOL a[0][1] = NEW Person("Person HASH 2"); EOL a[0][2] = NEW Person("Person HASH 3"); EOL a[1][0] = NEW Person("Person HASH 4"); EOL a[1][1] = NEW Person("Person HASH 5"); EOL a[1][2] = NEW Person("Person HASH 6"); EOL a[2][0] = NEW Person("Person HASH 7"); EOL a[2][1] = NEW Person("Person HASH 8"); EOL a[2][2] = NEW Person("Person HASH 9"); EOL
]) m5_question(BO(Question 10.10:) Write a function TT(create3) and TT(create4) that takes an INT argument TT(size) and returns a two dimensional array of CLSS(Person) or CLSS(Mine), respectively. Make is so that each CLSS(Person) or CLSS(Mine) object has its own number, using a separate counter variable TT(INT VARI(count)).) SUBSECTION(10.3, Three dimensional arrays) m5_question(BO(Question 10.11:) Using the knowledge you have gained so far about arrays, create, initialise and print a three dimensional array of CLSS(Person). Make it so that each CLSS(Person) object is given its own number using a separate counter variable TT(INT VARI(count)).)
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:27:03 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.