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 9: true OOP classes

SECTION(9, Tutorial 9) SUBSECTION(9.1, Elementary classes: using a single class for everything) m5_question(For the purpose of the text that follows, O.O.P. stands for EM(Object Oriented Programming).) m5_study(9.1) m5_question(
NU()CLASS PersonDriver1 EOL BEGIN EOL PD CLASS_VAR STRING homersName = "Homer Simpson"; EOL PD CLASS_VAR INT homersAge = 40; // Homer's age in years EOL EOL PD CLASS_VAR STRING fredsName = "Fred Flintstone"; EOL PD CLASS_VAR INT fredsAge = 45; // Fred's age in years EOL EOL PD CLASS_VAR STRING darthsName = "Darth Vader"; EOL PD CLASS_VAR INT darthsAge = 55; // Darth's age in years EOL EOL PD FUNCTION VOID growHomer() EOL PD BEGIN EOL PD PD homersAge = homersAge + 1; EOL PD END EOL PD FUNCTION VOID growFred() EOL PD BEGIN EOL PD PD fredsAge = fredsAge + 1; EOL PD END EOL PD FUNCTION VOID growDarth() EOL PD BEGIN EOL PD PD darthsAge = darthsAge + 1; EOL PD END EOL EOL PD FUNCTION VOID knightHomer() EOL PD BEGIN EOL PD PD homersName = "Sir " + homersName; EOL PD END EOL PD FUNCTION VOID knightFred() EOL PD BEGIN EOL PD PD fredsName = "Sir " + fredsName; EOL PD END EOL PD FUNCTION VOID knightDarth() EOL PD BEGIN EOL PD PD darthsName = "Sir " + darthsName; EOL PD END EOL EOL PD FUNCTION VOID printHomer() EOL PD BEGIN EOL PD PD SYSTEM_OUT_PRINTLN("I am " + homersName + ", my age is " + homersAge); EOL PD END EOL PD FUNCTION VOID printFred() EOL PD BEGIN EOL PD PD SYSTEM_OUT_PRINTLN("I am " + fredsName + ", my age is " + fredsAge); EOL PD END EOL PD FUNCTION VOID printDarth() EOL PD BEGIN EOL PD PD SYSTEM_OUT_PRINTLN("I am " + darthsName + ", my age is " + darthsAge); EOL PD END EOL EOL PD BEGIN_MAIN EOL PD PD growHomer(); EOL PD PD knightHomer(); EOL PD PD printHomer(); EOL PD PD printFred(); EOL PD PD printDarth(); EOL PD END_MAIN EOL END EOL
) m5_question(BO(Question 9.2:) By copying the pattern established in the existing code write a some new class variables to represent a new person called Winston Peters. Note that he was born in 1945 so he is 67 years old.) m5_question(BO(Question 9.3:) Then write some functions to work with this new person.) m5_question(BO(Question 9.4:) Finally call those functions from the MAIN function.) SUBSECTION(9.2, Improved classes: one object per class) m5_question(As your program gets large (say over 1000 lines) then it becomes no longer practical to put all of your code in the same class. So it is natural to put each piece of related code in its own class. ) m5_study(9.5, Each of these classes can be put in their own file. For each class CLSS(X), this class can be put into a file called TT(X.sjs). However for the purposes of this tutorial you will probably find it easier to merge all of the classes into the same file into a file called TT(PersonDriver2.sjs) m4_begin_indent NU()m4_include(sjs-tutorials/Person-2.m4) m4_end_indent ) m5_question(BO(Question 9.6:) By copying the pattern established in the existing code write a new class to represent Winston Peters.) m5_question(BO(Question 9.7:) Call the functions from the MAIN function of the driver class.) SUBSECTION(9.3, True OOP: more than one object per class) m5_question(To allow for more than one object per class, most if not all class variables needs to be made into what are called EM(instance variables) (or more simply and more commonly known as EM(properties)) and most if not all functions need to be made into what are called EM(methods). ) m5_study(9.8) m5_question( m4_begin_indent NU()m4_include(sjs-tutorials/Person-3.m4) m4_end_indent In the above code, note the use of three references TT(h), TT(f) and TT(d). Each reference above points to a different object. In S.J.S. and Java, objects only exist for at least the duration of time that there is a reference pointing to them. Therefore you need to keep references pointing to the objects that you are interested in keeping around for computation, storage of data or any other purpose. ) m5_question(BO(Question 9.9:) By copying the pattern established in the existing code add some code to the MAIN function add some code to create a new person for Winston Peters.) SUBSECTION(9.4, A common design pattern: private properties, public constructor and public getters) m5_question(A common design pattern in Java and one that I present for you in the following code is to make all of the properties of a class effectively read-only to all client classes by making all of the properties EM(private) and providing EM(non-private) EM(getter) methods for getting the values of the properties. It is possible for the original class to change the values of the properties but other classes (such as CLSS(PersonTest) below) are not capable of doing this, without calling a method of the original class such the TT(grow) and TT(knight) methods of the CLSS(Person) class. Finally an additional thing known as a EM(constructor) is used to ensure that objects are intialised with meaningful values for their properties. ) m5_study(9.10) m5_question( m4_begin_indent NU()m4_include(sjs-tutorials/Person-4.m4) m4_end_indent ) m5_question(BO(Question 9.11:) By copying the pattern established in the existing code add some code to the MAIN function add some code to create a new person called Winston Peters.) SUBSECTION(9.5, Comparing strings) m5_question(BO(Question 9.12:) Add a method TT(unknight()) which removes the TT(STRI("Sir ")) title if he has one. One trick for young players in S.J.S. or Java is to use the operator TT(==) to compare strings like so: m4_begin_indent NU()FUNCTION BOOLEAN FUNC(myCompare)(STRING VARI(a), STRING VARI(b)) EOL BEGIN EOL PD RETURN a == b; COMM(// Works but not as expected!) EOL END EOL m4_end_indent It compiles without error, but doesn't give you the result you were expecting. Instead you need to use the TT(equals) method of the STRING class like so: m4_begin_indent NU()FUNCTION BOOLEAN FUNC(myCompare)(STRING VARI(a), STRING VARI(b)) EOL BEGIN EOL PD RETURN a.equals(b); EOL END EOL m4_end_indent More generally, if TT(x) and TT(y) are a references to objects, then TT(x == y) returns whether or not x and y are pointing to the same object, whereas TT(x.equals(y)) returns whether or not the EM(contents) of the objects referred to by TT(x) and TT(y) are equal. The meaning of the word EM(contents) varies from class to class, but in the case of strings it means that the strings contain the same data. PP You will also find the STRING class' TT(substring) and (TT(toUpperCase) or TT(toLowerCase)) methods useful here too. See the Java API at m4_begin_center TT(http://java.sun.com/j2se/1.5.0/docs/api/) m4_end_center NU()for more details of these two methods. ) SUBSECTION(9.6, The NULL value for references) m5_question(As soon as you learn how to use references you need to know that all reference variables could conceivably hold the value EM(null), meaning EM(no value). In particular when properties are themselves references as you will discover in TUTE_LINKS, then those properties are initialised to NULL by default. Object arrays that you will learn about in TUTE_ARRAYS2 using the second of two initialisation syntaxes are also initialised to NULL by default. SUBSECTION(9.7, Why the TT(toString) method is better than any other method or property for debugging) If TT(x) is a reference to a class CLSS(X) and if TT(m) is a method of CLSS(X) and TT(p) is a property of CLSS(X), and if TT(x) is currently NULL, then the following lines result in a TT(NullPointerException) being thrown when executed: m4_begin_indent NU()x.p; EOL x.m(); EOL m4_end_indent NU()whereas if TT(x) is NULL then m4_begin_itemize m4_item TT(SYSTEM_OUT_PRINTLN(x);) and m4_item TT(SYSTEM_OUT_PRINTLN(STRI("x=") + x);) m4_end_itemize NU()prints out, respectively: m4_begin_itemize m4_item TT(NULL), and m4_item TT(x=NULL). m4_end_itemize NU()If TT(x) is not NULL, it calls m4_begin_itemize m4_item TT(SYSTEM_OUT_PRINTLN(x.toString());) m4_item TT(SYSTEM_OUT_PRINTLN(STRI("x=") + x.toString());) m4_end_itemize NU()so these expressions are safer to use than any other method or property in situations where TT(x) might be NULL. The syntax of the TT(toString) method is as follows: m4_begin_indent NU()PUBLIC METHOD STRING FUNC(toString)() EOL BEGIN EOL PD COMM(// Code goes here...) EOL END EOL m4_end_indent Importantly for reasons which will be explained later the TT(toString) method must be declared with TT(public) visibility. For other properties and methods to be used safely with NULL references you need to wrap a conditional EM(if) construct around the calling of the method or property like so for properties: m4_begin_indent NU()IF (x != NULL) EOL THEN BEGIN EOL PD SYSTEM_OUT_PRINTLN(x.p); EOL END EOL m4_end_indent NU()or like so for methods: m4_begin_indent NU()IF (x != NULL) EOL THEN BEGIN EOL PD SYSTEM_OUT_PRINTLN(x.m()); EOL END EOL m4_end_indent NU()Therefore the TT(toString) method is more convenient than any other method or property. ) m5_question(BO(Question 9.13:) Change to TT(print) method above from a method that prints out to the screen to a method called TT(toString) that returns a string.) m5_question(BO(Question 9.14:) Call the TT(toString) method instead of the TT(print) methods in the MAIN function.)
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:48 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.