Post

Created by @nathanedwards
 at November 2nd 2023, 5:19:41 am.

Question

Suppose you are building a simple application to model a zoological park. You have defined three classes as follows:

class Animal {
    protected String name;
    protected int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String makeSound() {
        return "Unspecified sound";
    }
}

class Lion extends Animal {
    private boolean isKing;

    public Lion(String name, int age, boolean isKing) {
        super(name, age);
        this.isKing = isKing;
    }

    public boolean isKing() {
        return isKing;
    }

    @Override
    public String makeSound() {
        return "Roar";
    }
}

class Snake extends Animal {
    private boolean isVenomous;

    public Snake(String name, int age, boolean isVenomous) {
        super(name, age);
        this.isVenomous = isVenomous;
    }

    public boolean isVenomous() {
        return isVenomous;
    }

    @Override
    public String makeSound() {
        return "Hiss";
    }
}

You are tasked with creating an ArrayList that can store instances of both Lion and Snake objects. Implement the Zoo class with the following specifications:

  1. Declare a private instance variable named animals of type ArrayList that can store objects of type Animal.
  2. Write a constructor that initializes the animals ArrayList.
  3. Write a method named addAnimal that takes an Animal object as a parameter and adds it to the animals ArrayList.
  4. Write a method named printAnimalDetails that prints the name, age, and the sound made by each animal in the animals list.

Your implementation should go in the code cell below:

import java.util.ArrayList;

class Zoo {
    // TODO: implement the required specifications
}

Create an instance of the Zoo class and add two Lion objects with the following details:

  • Lion 1: name="Simba", age=5, king=true
  • Lion 2: name="Nala", age=4, king=false

Call the printAnimalDetails method to display the details of the lions.

Answer

The implementation of the Zoo class is as follows:

import java.util.ArrayList;

class Zoo {
    private ArrayList<Animal> animals;

    public Zoo() {
        animals = new ArrayList<>();
    }

    public void addAnimal(Animal animal) {
        animals.add(animal);
    }

    public void printAnimalDetails() {
        for (Animal animal : animals) {
            System.out.println("Name: " + animal.getName());
            System.out.println("Age: " + animal.getAge());
            System.out.println("Sound: " + animal.makeSound());
            System.out.println();
        }
    }
}

To create an instance of the Zoo class and add two Lion objects, we can use the following code:

Zoo zoo = new Zoo();

Lion lion1 = new Lion("Simba", 5, true);
Lion lion2 = new Lion("Nala", 4, false);

zoo.addAnimal(lion1);
zoo.addAnimal(lion2);

zoo.printAnimalDetails();

The output will be:

Name: Simba
Age: 5
Sound: Roar

Name: Nala
Age: 4
Sound: Roar

Explanation:

  • First, we create an instance of the Zoo class.
  • Then, we create two Lion objects with the given details.
  • We add these Lion objects to the Zoo instance using the addAnimal method.
  • Finally, we call the printAnimalDetails method to display the details of the animals stored in the Zoo instance. The method iterates through each animal in the animals ArrayList and prints their name, age, and the sound they make. In this case, both lions will output "Roar" as their sound.