Answer to Question #281642 in Java | JSP | JSF for Jad

Question #281642

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.


1
Expert's answer
2021-12-20T19:59:01-0500
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog