015-002-006

Polymorphism: Array Management

Medium

Problem Description

Polymorphism: Array Management

In this problem, you will create a program that reads the number and types of animals from standard input, stores Dog and Cat objects in an Animal-type array, utilizes polymorphism to call each object's makeSound() method, and displays the result to standard output.

Learning Objective: Understand how to dynamically manage different subclass objects using a parent class array

Overview

You have Dog and Cat classes that inherit from Animal class. Read animal types from input, store them in an Animal-type array, and output each animal's sound.

Specifications

  • Animal class: makeSound() method (empty implementation)
  • Dog class: makeSound() outputs "Woof!"
  • Cat class: makeSound() outputs "Meow!"
  • Read number N and each animal type ("Dog" or "Cat") from input
  • Store corresponding objects in Animal-type array
  • Call makeSound() for all animals in loop

Input Format

N
type1
type2
...
typeN
  • Line 1: Number of animals N
  • Subsequent lines: Animal type for each ("Dog" or "Cat")

Output Format

Output each animal's sound on a separate line.

Sample Input/Output

Input:
2
Dog
Cat

Output:
Woof!
Meow!

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up