RunAnimal

PHOTO EMBED

Mon Oct 20 2025 07:19:46 GMT+0000 (Coordinated Universal Time)

Saved by @jerzey002

import java.util.Scanner;

public class RunAnimal {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an animal (bird, cat, dog): ");
        String choice = input.nextLine();

        Animal animal;

        if (choice.equalsIgnoreCase("bird")) {
            animal = new Bird();
        } else if (choice.equalsIgnoreCase("cat")) {
            animal = new Cat();
        } else if (choice.equalsIgnoreCase("dog")) {
            animal = new Dog();
        } else {
            System.out.println("Unknown animal!");
            input.close();
            return;
        }

        System.out.println();
        animal.eat();
        animal.sleep();
        animal.makeSound();

        input.close();
    }
}

abstract class Animal {
    abstract void eat();
    abstract void sleep();
    abstract void makeSound();
}

class Bird extends Animal {
    void eat() {
        System.out.println("The bird pecks on some grains.");
    }

    void sleep() {
        System.out.println("The bird sleeps on a tree branch.");
    }

    void makeSound() {
        System.out.println("The bird chirps cheerfully.");
    }
}

class Cat extends Animal {
    void eat() {
        System.out.println("The cat eats some fish.");
    }

    void sleep() {
        System.out.println("The cat sleeps soundly on the couch.");
    }

    void makeSound() {
        System.out.println("The cat meows softly.");
    }
}

class Dog extends Animal {
    void eat() {
        System.out.println("The dog eats its favorite bone.");
    }

    void sleep() {
        System.out.println("The dog sleeps beside its owner.");
    }

    void makeSound() {
        System.out.println("The dog barks loudly.");
    }
}
content_copyCOPY