1

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!!

2
  • Firstly, it's best to tag homework as, well, Homework. Secondly, it makes no sense to extend Zoo with an Animal as an Animal is not a Zoo. In your second snippet, you are only allowing for one animal. You've used the array tag here, so presumably this is the hint you're looking for. Your Zoo needs an array of Animal, and then the addAnimal method needs to add the animal to that array. Commented Nov 25, 2011 at 19:22
  • how would i make it so that when I add a new animal, it does not save to the same array index number? I tried doing an static int number_of_animals; then have this 'number_of_animals+=1' in the Animal constructor. but when i use that reference to the number in the array i created in the Zoo class, i get a null pointer Commented Nov 25, 2011 at 19:55

2 Answers 2

3

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.

class Zoo {
    private Animal[] animals;
    private String zooName;
    private int zooSize;

    public String getName() {
        return zooName;
    }

    public Zoo(String name, int capacity) {
        zooSize = 0;
        animals = new Animal[capacity];
        this.zooName = name;
    }
    public void addAnimal(Animal an) {
        animals[zooSize] = an;
        zooSize++;
    }

    public String toString() {
        String tempStr = "In " + zooName + ", we keep these " + zooSize + " animals:\n";
        for(int i=0; i < zooSize; ++i) {
            tempStr += animals[i].getName() + "\n";
        }
        return tempStr;
    }
}

abstract class Animal {
    private Zoo zoo;
    private String name = "";

    public Animal(String name) {
        this.name = "Animal " + name;
    }

    public String getName() {
        return name;
    }

    public void setZoo(Zoo zoo)
    {
        this.zoo = zoo;
        this.zoo.addAnimal(this);
    }

    public String belongsWhere() {
        return  name + " belongs to " +
                zoo.getName() + " zoo";
    }
}

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).

class Giraffe extends Animal {

    public Giraffe(String surname) {
        super("Giraffe " + surname);
    }
}

class Tiger extends Animal {

    public Tiger(String surname) {
        super("Tiger " + surname);
    }
}

class Penguin extends Animal {

    public Penguin(String surname) {
        super("Penguin " + surname);
    }
}

And to test it...

public class Main {

        public static void main(String[] args) {
            Zoo z1 = new Zoo("Zoo1");
            Zoo z2 = new Zoo("Zoo2");

            Animal pen1 = new Penguin("Luke");
            Animal gir1 = new Giraffe("Duke");
            Animal pen2 = new Penguin("Fluke");
            Animal tig1 = new Tiger("Brooke");

            pen1.setZoo(z1);
            pen2.setZoo(z2);
            gir1.setZoo(z2);
            tig1.setZoo(z2);

            System.out.println(pen1.belongsWhere());
            System.out.println(tig1.belongsWhere());

            System.out.println(z1); System.out.println(z2);
        }
}

I just hope I helped.

Sign up to request clarification or add additional context in comments.

1 Comment

yes that's really great, i'm just trying to find a way using ur method without the List functions as I have not learned about them and am supposed to stick to arrays. Quite difficult..
0

Zoo will need to consist of a List<Animal> of animals, not just a single Animal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.