TurtleTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called TurtleTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// LINKED LIST TUTORIAL
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class Turtle {

  /*
   * Properties of the class...
   */
  private String name;
  private int    age;
  private int    weight;
  private Turtle up;

  /*
   * Constructor of the class...
   */
  public Turtle(String aName, int anAge, int aWeight) {
    name   = aName;
    age    = anAge;
    weight = aWeight;
  }

  /*
   * A method for one turtle to sit on top of another:
   *
   */
  public void carry(Turtle onTop) {

    // body goes here:

  }

  /*
   *
   * This method lists all the Turtles that the current Turtle
   * is carrying on his or her back.
   *
   */
  public void tellStory() {

    System.out.println("Turtle " + name +
                       " is carrying the following turtles: ");

    // for-loop goes here:

  }
}
^L

class TurtleTest {

  /*
   *
   * The main method is the point of entry into the program...
   *
   */
  public static void main(String[] args) {

    Turtle yertle = new Turtle("Yertle", 100, 20);
    Turtle zippy  = new Turtle("Zippy",  100, 20);
    Turtle bungle = new Turtle("Bungle", 100, 20);

    bungle.carry(zippy);
    zippy.carry(yertle);

    System.out.println("The arrangement of the turtles is as follows:");

    bungle.tellStory();       // => "Bungle is carrying: Zippy, Yurtle"
    zippy.tellStory();        // => "Zippy is carrying: Bungle"
    yertle.tellStory();       // => "Yurtle is carrying: no-one"

    System.out.println("Bungle is carrying the following weight:");

    System.out.println(bungle.calculateLoad()); // => 60
  }

}

// QUESTIONS:
//
// (1) Finish the method carry()
// 
// (2) Finish the method tellStory()
// 
// (3) Write a method findTopTurtle() that returns a reference to the
// Turtle at the top of the pile.
// 
// (4) Write a method isAtTop() which returns whether or not the
// current Turtle is at the top of the pile.
// 
// (5) Write the method calculateLoad() that returns the total weight of
// all the Turtles sitting on the current one.
// 
// (6) Write the method findOldestTurtle() that returns a
// reference to the oldest Turtle in the pile, starting from
// the current one.
// 
// (7) What is the advantage in having the fields name, age and weight
// private?
// 
// (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?
// 
// (9) What is the advantage in having the property "up" private?
// 
// (10) Write a method called insert(Turtle aTurtle) that inserts
// aTurtle after the current Turtle in the list.
// 
// (11) HARDER: Rewrite the tellStory method using recursion.  Note that
// you will need two functions instead of one.
//