TurtleTest.java Questions
Instructions
- 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.
- 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.
- Save the file to disk and begin answering the
questions that are shown below.
- When you need to look at the model answer, click here.
- Click here to go back to the
tutorials menu.
Program listing
|
class Turtle {
private String name;
private int age;
private int weight;
private Turtle up;
public Turtle(String aName, int anAge, int aWeight) {
name = aName;
age = anAge;
weight = aWeight;
}
public void carry(Turtle onTop) {
}
public void tellStory() {
System.out.println("Turtle " + name +
" is carrying the following turtles: ");
}
}
^L
class TurtleTest {
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(); zippy.tellStory(); yertle.tellStory();
System.out.println("Bungle is carrying the following weight:");
System.out.println(bungle.calculateLoad()); }
}