Animal Speak Variety


Submit solution

Points: 50 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

題目說明

請設計一個動物家族程式,其中包含三種動物類別:Dog、Cat 與 Bird,皆繼承自父類別 Animal。 每種動物擁有屬性 name,並且會使用自己特有的叫聲說話。

請依照下列要求完成主程式:

  1. 讓使用者依序輸入三個字串,分別代表狗、貓、鳥的名字(輸入順序固定)。

  2. 建立對應物件並設定名稱。

  3. 分別呼叫 showName() 方法顯示動物的名稱。

  4. 呼叫 speak() 方法顯示動物的叫聲。

程式模板

import java.util.Scanner;

class Animal {
    String name;

    void speak() {
        System.out.println("The animal makes a sound.");
    }

    void showName() {
        System.out.println("The animal's name is " + name);
    }
}

class Dog extends Animal {
    // TODO: 補齊缺失程式碼
}

class Cat extends Animal {
    // TODO: 補齊缺失程式碼
}

class Bird extends Animal {
    // TODO: 補齊缺失程式碼
}

public class MyProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // TODO: 輸入三種動物的名字


        // TODO: 建立三個動物並設定 name


        // TODO: 顯示名稱與叫聲


        scanner.close();
    }
}

輸入

<狗的名字>
<貓的名字>
<鳥的名字>

輸出

The animal's name is <狗的名字>
<狗的名字> says: Woof!
The animal's name is <貓的名字>
<貓的名字> says: Meow!
The animal's name is <鳥的名字>
<鳥的名字> says: Tweet!

範例輸入 #1

Buddy
Kitty
Tweety

範例輸出 #1

The animal's name is Buddy
Buddy says: Woof!
The animal's name is Kitty
Kitty says: Meow!
The animal's name is Tweety
Tweety says: Tweet!

Comments

There are no comments at the moment.