Start learning free

LESSON 600 OF 639 · OBJECT-ORIENTED PROGRAMMING

Animal Sounds in Scala

Practice

You build two classes, Animal and Dog.

A dog is a kind of animal. So Dog inherits from Animal.

  • Animal(name) makes an animal with that name.
  • describe() returns "<name> is an animal".
  • Dog(name) makes a dog. It gets name and describe() from Animal.
  • Don't write describe() again in Dog. The inherited one already works.
  • A dog adds one method. fetch() returns "<name> fetches the ball".

Each input line is animal or dog, then a name.

For an animal, the test code prints its describe(). For a dog, it prints describe(), then fetch(). Each text goes on its own line.

The starter code spells the methods your language's way. C# has Describe and Fetch.

 

Example 1:

Input:
    dog Rex
    animal Tom
Output:
    Rex is an animal
    Rex fetches the ball
    Tom is an animal
Explanation: Rex is a dog. It describes itself and fetches.
Tom is a plain animal. It only describes itself.

Example 2:

Input:
    dog Max
    dog Bella
Output:
    Max is an animal
    Max fetches the ball
    Bella is an animal
    Bella fetches the ball
Explanation: Both dogs describe themselves with the method they inherited.

Example 3:

Input:
    animal Rex
    dog Rex
Output:
    Rex is an animal
    Rex is an animal
    Rex fetches the ball
Explanation: The two have the same name.
Only the dog fetches.

 

Constraints:

  • A name is one word of 1 to 20 English letters.
  • 1 to 100 lines per test case.
STEP 1 OF 4
    Ctrl + Enter to check

    Animal Sounds in Scala

    Keep going with Scala500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free