AnimalTest.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
AnimalTest.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 AnimalTest
{
private static void chatter(Animal[] a)
{
for (int i=0; i<a.length; i++)
{
a[i].talk();
}
}
public static void main(String[] args)
{
Animal[] farm = { new Dog(), new Cow(), new Kiwi() };
Animal[] ark = { new Dog(), new Dog(), new Cow(), new Cow(),
new Kiwi(), new Kiwi() };
Cow[] herd = { new Cow(), new Cow(), new Cow() };
chatter(farm);
chatter(ark);
chatter(herd);
}
}
class Animal
{
public boolean hasWings()
{
return false;
}
public boolean canFly()
{
return false;
}
public void talk()
{
}
}
class Dog extends Animal
{
public void talk()
{
System.out.println("Woof woof!");
}
}