ADD file via upload

main
p5wft6kpg 6 months ago
parent b7745a5036
commit a442c3f84d

@ -0,0 +1,35 @@
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();
}
}
Loading…
Cancel
Save