Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:28:37+00:00 2026-05-27T04:28:37+00:00

I have to make a program that has a zoo with three types of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:28:38+00:00Added an answer on May 27, 2026 at 4:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to make a program that only has 2 methods. One method
I have a little program that I want to make open automatically when my
I have an existing program that has its own main loop, and does computations
I have a Makefile for a C program that has the declaration CC?=gcc Changing
I have encountered a problem in my program that has me somewhat stumped. I
I have a program that has a ton of sensors producing data at a
I tried to make a program that has a correct Divide function. My code
This is my problem. I have a program that has to run in a
I am doing this assignment, make a program that solves sudoku. I have a
What do I have to do to make my program use a file that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.