Requirements

Let’s practice creating objects and classes.

You need to create a class AnimalType with the following members:

  • name (type String)

Instance variables need to have private access. You need to define getters and setters methods to retrieve and assign values to the variables.

Create a constructor that will receive all values ​​for the attributes through parameters and initialize them (set their values).

Create an Animal class with the following instance variables:

  • name (String)
  • type (AnimalType)

Instance variables need to have private access. You need to define getters and setters methods to retrieve and assign values to the variables.

Create a constructor that will receive all values ​​for the variables through parameters and initialize them (set their values).

Create a Zoo class with the following members:

  • name (String)
  • size (double)
  • animals (Animal[])
  • capacity (int)
  • boolean addAnimal(Animal animal)
  • boolean setAnimal(Animal animal, int index)

Instance variables need to have private access. You need to define getters and setters methods to retrieve and assign values to the variables.

Create a constructor that will initialize all instance variables (except animals). 

The animals variable needs to be initialized so that the capacity variable defines the number of maximum elements.

You need to ensure that a value less than or equal to 0 cannot be entered into the size variable. If such a value is attempted to be entered, the message “Size must be greater than 0.” needs to be printed to the standard output. Additionally, the size needs to be set to the value 10. The same functionality needs to be applied in both setters and constructors.

Create a method public boolean addAnimal(Animal animal) that will add a new animal to the animals array. It is necessary to ensure that no more animals can be entered than defined by the capacity attribute. In case of attempting such an entry, the message “Zoo is full.” needs to be printed to the standard output, and the method must return the value false. Otherwise, add the animal to the array and return the value true.

It is necessary to create a method public boolean setAnimal(Animal animal, int index) that will set the passed animal element type to the field at the position defined by the index argument. Additionally, it is necessary to ensure that the passed value of the index variable indeed exists. If it does not exist, the message “Invalid argument index.” needs to be printed, and the method should return the value false. Otherwise, update the existing element and return the value true.

Hint

A private instance variable can be used to simplify tracking the index at which to add a new animal.

Too Easy?

Try adding a method public boolean remove(int index) that removes the animal at the specified index.

Keep in mind that if the zoo is full, you can add a new animal after removing an animal.

Testing

To test the functionality, you can use the following class: Main.java.

Send Us Your Solution

If you’re interested in our feedback, please send us your solution here. We’ll review it and let you know.

    By Ana Peterlić

    Ana is a Java Software Engineer with over 6 years of experience in designing and developing software solutions. She loves tutoring and helping others understand Java ecosystem.