You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.1 KiB

public class AnimalTest {
public static void main(String[] args) {
// Create various animal objects
Animal[] animals = {
new Fish("Nemo", 1, 2.5),
new Dog("Rex", 3),
new Cat("Whiskers", 2),
new Bird("Tweety", 1)
};
// Test makeSound() method for all animals
System.out.println("=== Animal Sounds ===");
for (Animal animal : animals) {
animal.makeSound();
}
System.out.println();
// Test play() method of Pet interface
System.out.println("=== Pet Play Behavior ===");
for (Animal animal : animals) {
if (animal instanceof Pet) {
Pet pet = (Pet) animal;
pet.play();
} else {
System.out.println(animal.getName() + " is not a pet and cannot play.");
}
}
System.out.println();
// Test Fish's special swim() method
System.out.println("=== Fish Special Behavior ===");
Fish fish = new Fish("Dory", 2, 3.8);
fish.swim();
}
}