Some animals can scream, others are mute. We will represent the fact of shouting by means of a method displaying on the screen the shout of the animal (Miaou, Wouf ...).
- Write an interface containing the method allowing to scream.
- Write the classes for cats, dogs and rabbits (which are mute)
- Write a program with an array of animals that can scream, fill it with dogs and cats and then make all these animals scream. Describe what is displayed on the screen when this program is run.
public interface Scream {
void scream();
}
public class Cat implements Scream {
@Override
public void scream() {
System.out.println("Miaou");
}
}
public class Dog implements Scream{
@Override
public void scream() {
System.out.println("Wouf");
}
}
public class Rabbit{
}
public class Main {
public static void main(String[] args) {
Scream[] animals = {new Dog(), new Dog(), new Cat(), new Cat(), new Dog(), new Cat()};
for (int i = 0; i < animals.length; i++) {
animals[i].scream();
}
}
}
Comments
Leave a comment