@cindex Yertle the Turtle
@cindex Linked lists

Dr Seuss' story @url{https://en.wikipedia.org/wiki/Yertle_the_Turtle,
EM(``Yertle the Turtle'')} describes how a turtle called Yertle sits
at the top of a pile of other turtles. In this example, the pile of
turtles is represented by a linked list of Turtle objects, with the
TT(down) PROPERTY serving to connect one Turtle object to another. If
a Turtle object has a non-null TT(down) PROPERTY, then this represents
the fact that it is sitting on top of another turtle. The last turtle
in the linked list is the turtle that is at the bottom of the pile,
which has a NULL value for its down PROPERTY.  Note that you cannot
use the SUPERFOR construct for iterating through a linked list.  In
this case the FUR construct is the most sensible.

BO(Question 4.14.1:) Study, compile and run the following code:

@example
CLASS Turtle
BEGIN

PD PRIVATE PROPERTY String     name;   COMM(// Turtle's name)
PD PRIVATE PROPERTY int        age;    COMM(// Turtle's age in years)
PD PRIVATE PROPERTY double     weight; COMM(// Turtle's weight in kg)

PD COMM(// NOTE: this property allows for linked lists)
PD PROPERTY Turtle down;

PD CONSTRUCTOR Turtle(String aName, int anAge, double aWeight)
PD BEGIN
PD PD name   = aName;
PD PD age    = anAge;
PD PD weight = aWeight;
PD END

PD COMM(/** Getter method for name property */)
PD METHOD String getName()
PD BEGIN
PD PD RETURN name;
PD END

PD COMM(/** Useful method for debugging */)
PD POBLIC METHOD String toString()
PD BEGIN
PD PD return name;
PD END
END

CLASS TurtleTest
BEGIN
PD BEGINMAIN

PD PD VAR Turtle yertle = new Turtle("Yertle", 103, 20);
PD PD VAR Turtle zippy  = new Turtle("Zippy",  102, 30);
PD PD VAR Turtle bungle = new Turtle("Bungle", 101, 40);

PD PD COMM(// *** see later)
PD PD yertle.down = zippy;
PD PD zippy.down = bungle;
PD PD bungle.down  = NULL; COMM(// NOTE: not needed as bungle.down is NULL by default)

PD PD VAR int totalWeight = 0;
PD PD COMM(// NOTE: demonstrates how to iterate through a linked list:)
PD PD FUR (VAR Turtle current = yertle; current != null; current=current.down)
PD PD BEGIN
PD PD PD totalWeight = totalWeight + current.getWeight();
PD PD END
PD PD System.out.println("The total weight is " + totalWeight);
PD ENDMAIN
END
@end example

The code in the MAIN FUNCTION after the TT(***) sets down the
following relationships between the three Turtle objects (Yertle,
Bungle and Zippy). The following diagram shows the relationship
between the different turtles. When you traverse the list of turtles
you must always start at the top turtle (known as the head of the
linked list). If you give a different value for the TT(top) turtle,
your code will think that the given turtle is the one at the top of
the pile and you will get the wrong result.

@verbatiminclude Turtles.txt

Question 4.14.2: Move the code for calculating the total weight of the
turtles from the MAIN FUNCTION to a FUNCTION called TT(FUNCTION void
printTotalWeight(Turtle bottom)) in the 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(Turtle.printTotalWeight(null)) wouldn't
be and therefore is better. This is one example of how methods and
functions differ.

BO(Question 4.14.3:) Revision question for getters. By copying the pattern
established by the getName method, add two getter methods to the
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 Yertle object in the MAIN FUNCTION. Note that the
TT(toString) method would be more appropriate as it handles nulls
better but you know that the yertle reference is not null so you know
it is safe to call the TT(getAge) and TT(getWeight) methods on the
yertle reference.

BO(Question 4.14.4:) Write a FUNCTION TT(Turtle findBottomTurtle(Turtle top)) that
returns the Turtle object that is at the top of the pile, and returns
null if there isn't one.

BO(Question 4.14.5:) Then call this FUNCTION from the MAIN FUNCTION using
TT(System.out.println()) and the top turtle Yertle.

BO(Question 4.14.6:) Write a FUNCTION TT(Turtle findOldestTurtle(Turtle top))
that returns the oldest turtle or null if there isn't one.

BO(Question 4.14.7:)Then call this FUNCTION from the MAIN FUNCTION using
TT(System.out.println()) and the top turtle Yertle.

BO(Question 4.14.8:) Write a FUNCTION TT(Turtle
findHeaviestTurtle(Turtle top)) returns the heaviest turtle, or NULL
if there isn't one.

BO(Question 4.14.9:) Then call this FUNCTION from the MAIN
FUNCTION using TT(System.out.println()) and the top turtle Yertle.

BO(Question 4.14.10:) Write a FUNCTION TT(sayPile(Turtle top)) that prints
the names of the turtles in the pile starting from the top turtle and
finishing at the bottom turtle. Then call this FUNCTION from the MAIN
FUNCTION.

BO(Question 4.14.11:) Under what circumstances would it be okay to change
the visibility of the TT(down) PROPERTY to private, like the name, age
and weight properties?

BO(Question 4.14.12:) Add an extra parameter to the CONSTRUCTOR which is a
reference the to the turtle on below the current one. Then remove all
occurrences of the TT(down) PROPERTY from the MAIN FUNCTION.  Note that
you will need to reverse the order that the turtles are created so the
bottom turtle is constructed first and so on.  The advantage of this
is that it enables you to change the visibility of the TT(down)
PROPERTY to PRIVATE.

