// ANSWERS: // // (1) Finish the method carry() // public void carry(Turtle onTop) { up = onTop; } // // (2) Finish the method tellStory() // public void tellStory() { System.out.println("Turtle " + name + " is carrying the following turtles: "); // for-loop goes here: for (Turtle tempTurtle = up; tempTurtle != null; tempTurtle = tempTurtle.up) { System.out.println(tempTurtle.name + " "); } } // // (3) Write a method findTopTurtle() that returns a reference to the // Turtle at the top of the pile. // public Turtle findTopTurtle() { Turtle tempTurtle = this; while (tempTurtle.up != null) { tempTurtle = tempTurtle.up; } return tempTurtle; } // // (4) Write a method isAtTop() which returns whether or not the // current Turtle is at the top of the pile. // public boolean isAtTop() { if (up == null) { return true; } else { return false; } } // // (5) Write the method calculateLoad() that returns the total weight of // all the Turtles sitting on the current one. // public int calculateLoad() { int load = 0; for (Turtle tempTurtle = up; tempTurtle != null; tempTurtle = tempTurtle.up) { load = load + tempTurtle.weight; } return load; } // // (6) Write the method findOldestTurtle() that returns a // reference to the oldest Turtle in the pile, starting from // the current one. // public Turtle findOldestTurtle() { Turtle oldestTurtle = this; for (Turtle tempTurtle = up; tempTurtle != null; tempTurtle = tempTurtle.up) { if (tempTurtle.age > oldestTurtle.age) { oldestTurtle = tempTurtle; } } return oldestTurtle; } // // (7) What is the advantage in having the fields name, age and weight // private? // // So the other classes cannot read from or write to these fields. // // (8) How could we have it so that other classes could read the name, age, // and weight of the Turtles, but not be allowed to change the name, age // and weights? // // By introducing three new methods: getName(), getAge() and getWeight() // defined as follows: public String getName() { return name; } public int getAge() { return age; } public int getWeight() { return weight; } // If you are in another class then you can read the name, age and weight // by calling these methods. Note that other classes still cannot // alter the three properties name, age and weight so the properties // are said to be "read-only" from other classes. // // (9) What is the advantage in having the property "up" private? // // So that other classes cannot adjust the order of the turtles in // the pile. If other classes need to scan through the lists, it // might be necessary to add a "getUp()" method like for the // previous question. // // (10) Write a method called insert(Turtle aTurtle) that inserts // aTurtle after the current Turtle in the list. // public void insert(Turtle aTurtle) { aTurtle.up = up; up = aTurtle; } // // (11) HARDER: Rewrite the tellStory method using recursion. Note that // you will need two functions instead of one. // // NOTE: There is more than one way to solve this question. public void tellStory() { System.out.println("Turtle " + name + " is carrying the following turtles: "); if (up != null) { tellStoryWorker(up); } } // NOTE: The method tellStoryWorker could be declared as // static. If you don't understand why this is true, then // ignore this remark because the method works as it // stands without being static. private void tellStoryWorker(Turtle current) { System.out.println(current.name + " "); if (current.up != null) { tellStoryWorker(current.up); } } //