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 14: linked lists

SECTION(14, Tutorial 14) m5_question(Dr Seuss' story ``Yurtle the Turtle'' describes how a turtle called Yurtle sits at the top of a pile of other turtles. In this example, the pile of turtles is represented by a linked list of CLSS(Turtle) objects, with the TT(up) property serving to connect one CLSS(Turtle) object to another. If a CLSS(Turtle) object has a non-NULL TT(up) property, then this represents a turtle that is sitting on top of the current one. The last turtle in the linked list is the turtle that is at the top of the pile, which has a NULL value for its TT(up) property.) m5_study(14.1) m5_question( m4_begin_indent NU()m4_include(sjs-tutorials/TurtleTest.m4) m4_end_indent The code in the MAIN function after the TT(BO(***)) sets up the following relationships between the three CLSS(Turtle) objects (Bungle, Zippy and Yurtle). The following diagram shows the relationship between the different turtles. When you traverse the list of turtles you must always start at the bottom turtle (known as the EM(head of the linked list)). If you give a different value for the bottom turtle, your code will think that the given turtle is the one at the bottom of the pile and you will get the wrong result. PCLOSE m4_begin_center m4_image(Turtles) m4_end_center POPEN ) m5_question(BO(Question 14.2:) Move the code for calculating the total weight of the turtles from the MAIN function to a function called TT(FUNCTION VOID FUNC(printTotalWeight)(CLSS(Turtle) VARI(bottom))) in the CLSS(Turtle) class that prints out the total weight of the turtles. Then call that function from the MAIN function to get the same result as before. Note that that if TT(printTotalWeight) was a method then calling that method using NULL (representing an empty list) like so: TT(NULL.printTotalWeight()) would be an error, whereas TT(CLSS(Turtle).printTotalWeight(NULL)) wouldn't be and therefore is better. This is one example of how methods and functions differ.) m5_question(BO(Question 14.3: Revision question for getters.) By copying the pattern established by the TT(getName) method, add two getter methods to the CLSS(Turtle) class: TT(getAge) which returns the current turtle's age and TT(getWeight) which returns the current turtle's weight. Then call these methods on the Yurtle object in the MAIN function. Note that the TT(toString) method would be more appropriate as it handles nulls better but you known that the TT(yurtle) reference is not NULL so you know it is safe to call the TT(getAge) and TT(getWeight) methods on the TT(yurtle) reference.) m5_question(BO(Question 14.4:) Write a function TT(FUNC(findTopTurtle)(CLSS(Turtle) VARI(bottom))) that returns the CLSS(Turtle) object that is at the top of the pile, and returns NULL if there isn't one.) m5_question(BO(Question 14.5:) Then call this function from the MAIN function using SYSTEM_OUT_PRINTLN and the bottom turtle TT(bungle).) m5_question(BO(Question 14.6:) Write a function TT(FUNC(findOldestTurtle)(CLSS(Turtle) VARI(bottom))) that returns the oldest turtle or NULL if there isn't one.) m5_question(BO(Question 14.7:)Then call this function from the MAIN function using SYSTEM_OUT_PRINTLN and the bottom turtle TT(bungle).) m5_question(BO(Question 14.8:) Write a function TT(FUNC(findHeaviestTurtle)(CLSS(Turtle) VARI(bottom))) returns the heaviest turtle, or NULL if there isn't one.) m5_question(BO(Question 14.9:) Then call this function from the MAIN function using SYSTEM_OUT_PRINTLN and the bottom turtle TT(bungle).) m5_question(BO(Question 14.10:) Write a function TT(FUNC(sayPile)(CLSS(Turtle) VARI(bottom))) that prints the names of the turtles in the pile starting from the bottom turtle and finishing at the top turtle. Then call this function from the MAIN function.) m5_question(BO(Question 14.11:) Under what circumstances would it be okay to change the visibility of the TT(up) property to private, like the TT(name), TT(age) and TT(weight) properties?) m5_question(BO(Question 14.11:) Add an extra parameter to the constructor which is a reference the to the turtle on top of the current one. Then remove all occurences of the TT(up) property from the MAIN function. The advantage of this is that it enables you to change the visibility of the TT(up) property to private.)
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:01 NZST 2014
Best viewed at 800x600 or above resolution.
© Copyright 1999-2014 Davin Pearson.