I have to make a program that has a zoo with three types of animals (giraffes, tigers, and penguins)
I have to make a method that assigns the animals to the zoo (there can be multiple zoos so it has to be specific) and then a method that can print all the animals in that zoo.
How do you add multiple animals to a zoo?
I can assign multiple animals to a zoo (where you can print details of the animal and it shows which zoo, but I need to print details of the zoo and it shows animals using:
public class Animal extends Zoo {
private Zoo zoo;
public void setZoo(Zoo zoo){
this.zoo = zoo;
}
)
but then I can’t print out all the animals inside the zoo, just the most recent animal assigned to the zoo.
This is what I have come up with that prints the most recent:
public class Zoo {
private Animal animal;
public void addAnimal(Animal animal){
this.animal = animal;
}
}
Thanks so much!!
I’ll try to help, but I doubt I understood you correctly.
First, you’ll need an abstract class Animal; makes no sense to inherit from Zoo (what in common they have?), and your Zoo class to hold those 3 types of animals.
All three of them should inherit from an abstract Animal because, well…they’re animals and things get easier when it’s time to handle array of 3 of those concrete animals.
You see, now we don’t care about what type of animal Zoo keeps, it’s just an Animal and it has a name and it depends of it’s concrete one (Tiger, Giraffe, Penguin).
And to test it…
I just hope I helped.