I have to develop a program to assist Zoo staff to record required information of animals.Help with defining the methods! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have to develop a program to assist Zoo staff to record required information of animals.Help with defining the methods!

So I have 4 classes.A 'Animal' class which is the parent.And 2 subclasses 'Dog' and 'Cat'.So I'm good with that part.But my Other Class The 'Zoo' class.I need to define actions to be done on adding and removing animal details as well as getting statistics of the animals.For example one method: public void addAnimal(Animal animal){ ..add logic } The Output should be: One Lion Added to the zoo... Help with defining this method please Also a method to remove animals from the zoo. public void removeAnimal(String animalID){...add logic} If I were to use an ArrayList how would I go about defining these methods? PS:The Animal class's constructor is Animal(String animalCategory, String animalType)..e.g(2 main categories_1.Cat=Lion,Cheetah,Tiger etc..2.Dog=Wolf,Domestic dog,fox etc... Animal Type=Lion,Cheetah,Dog etc...) So the 'Cat' class& 'Dog' class are subclasses of the Animal class and have respective constructors. Cat(String Id,String Colour,String name) & Dog has the same constructor parameters.

10th May 2020, 1:21 PM
Nica STRAIT
Nica STRAIT - avatar
4 Answers
+ 1
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 That's the thing I'm lost...I was tryna use an ArrayList but Then The assignment question got a bit confusing because we had to have a menu.And since I'm a beginner I needed to know if anyone as Tips or something.
10th May 2020, 1:30 PM
Nica STRAIT
Nica STRAIT - avatar
+ 1
The list of zoo animals should be stored in the Zoo class as an ArrayList attribute. You can then add new animals in the addAnimal(animalID) method of the Zoo class
10th May 2020, 1:31 PM
Ore
Ore - avatar
+ 1
Ore Adeleye THANKS for the tip.Can you give an example please. Say I was to add a New animal using the method addAnimal(Animal animal)... So is it possible to get whatever that is passed into the addAnimal () parameter referenced by 'animal' and use that with the add()method from the ArrayList class. Eg: ArrayList<Animal> zooAnimal=New ArrayList<>() public void addAnimal (Animal animal) { zooAnimal.add(animal); System.out.println(" One "+ animal + "added to the zoo) } 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 That's what I think I should do...My Attempt I guess. Do you think it's possible to have a method defined like that? I'm not sure if this way of mine is legit?
10th May 2020, 1:49 PM
Nica STRAIT
Nica STRAIT - avatar
+ 1
Nica STRAIT It's been long I coded in Java so I may not be up to date but I think zooAnimal should be private and it should be referenced in addAnimal as this.zooAnimal. That is called encapsulation
10th May 2020, 1:58 PM
Ore
Ore - avatar