0

Implement Zoo class (with its test class). Zoo have name and area in meter square. Zoo can have one or more Animals. An Animal has ID, name, Type, Age, gender. We should be able to add new Animals to the Zoo, remove Animals and determine how many animals currently in the zoo.

This is the Zoo class:

import java.util.ArrayList;

public class Zoo {
    String name;
    String area;
    ArrayList<Animal> animals;
    static int id;

    public Zoo(String name, String area) {
        this.name = name;
        this.area = area;
    }

    public void addanimal(animal ann) {
        animals.add(id, ann);
        id++;
    }
}

public class Animal {
    String name;
    String type;
    String age;
    String gender;

    public Animal(String name, String type, String age, String gender) {
        this.name = name;
        this.type = type;
        this.age = age;
        this.gender = gender;
    }
}
public class Test {

    public static void main(String[] args) {
        Zoo nozha = new Zoo("nozha", "100");
        Animal lion = new Animal("lion", "male", "20", "fine");
        nozha.addanimal(lion);
        Znimal tiger = new Animal("tiger", "male", "30", "ssc");
        nozha.addanimal(tiger);
        System.out.print(Zoo.id);
    }
}

First I need help with function (addanimal) because when I print (zoo.id) its not working and I didn't know how to remove animal please help me i am beginner in programming and this is my first time i used ArrayList and I never asked before

12
  • 7
    It would help you (and others) if you complied with Java naming conventions, in particular: class name start with a large cap (Zoo, Animal), method names use camel case (addAnimal). Commented Sep 19, 2012 at 11:13
  • 1
    What is not working? What did you expect to get and what did you get. I would expect this to print 2 assuming you changed List<Animal> animals = new ArrayList<Animal>(); Commented Sep 19, 2012 at 11:14
  • 2
    id should certainly not be static. Commented Sep 19, 2012 at 11:20
  • 2
    (The following is not meant to be mean) Stop posting duplicate questions. What you need to do is to take a step back and go through some introductions to java/programming. And it's worth mentioning that naming conventions are in fact a really huge deal. And you'll learn 10 times more if you figure things out for yourself. Commented Sep 19, 2012 at 11:21
  • 3
    @JBNizet The homework tag should not be used anymore: stackoverflow.com/tags/homework/info Commented Sep 19, 2012 at 11:28

3 Answers 3

8

You need to initialize the animals variable to something other than its default value, which is null:

private List<Animal> animals = new ArrayList<Animal>();

Then look at the javadoc of java.util.List, and you'll see that it contains methods to add and remove elements, as well as a method which returns its size, and makes thus the id variable completely unnecessary.

Also, notice in the javadoc how ALL the classes start with an uppercase letter, and ALL the methods are spelled in camelCase (like addAnimal() and not like addanimal()). Respect these conventions: they're a very important factor for the readability of your code.

Also, choose the appropriate type for your variables. An area, in meter square, should be an int or a float or a double, but not a String.

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

4 Comments

I would also add that it's preferable to initialize id as a 0, which is quite possible since it's static and preferable to avoid possible headaches if the class is ever to be extended/inherited from.
As I said in my answer, this ID variable is completely unnecessary, and shouldn't even be there.
That's true, but I suppose there's a reason it exists. If no then your answer is fully correct.
Given what's being asked, at the top of the question, there's absolutely no reason for this variable.
0

First i need help with function (addanimal) because when i print (zoo.id)

Basing from your code it seems that zoo.id would return the size of ArrayList<animals> why use the size of the said list instead animals.size() or by rule of encapsulation, getAnimals().size(). This may return a NullPointerException, so intialize ArrayList<animals> with a emptyarraylist`

In removing a said animal in the list, you better go for animals.remove(Animal ann).

In updating, check for if animals.contains(Animal ann) is true, via ID or hashCode then check what index is ann in the list then update ann by animals.set(<index>, ann)

Comments

0

Zoo.java:

import java.util.ArrayList;
  public class Zoo {

    String name; 
    double area; 
    int sizeOfZoo = 0;
    ArrayList<Animal> animals = new ArrayList<Animal>();


    public Zoo(String name, double area) { 
        this.name = name; 
        this.area = area;

    }   

        public void addAnimal(Animal ann) {
        animals.add(ann); 
    } 

        public int getSizeOfZoo() {
                return animals.size();
        }
    }

Animal.java

public class Animal {
    String name;
    String type;
    String age;
    String gender;

    public Animal(String name, String type, String age, String gender) {

            this.name = name;
            this.type = type;
            this.age = age;
            this.gender = gender;
        }    
    } 

Test.java

    public class Test {

        public static void main(String[] args) {

            Zoo nozha = new Zoo("nozha", 100);
            Animal lion = new Animal("lion","fine","20","male");
            nozha.addAnimal(lion); 

            Animal tiger = new Animal("tiger","ssc","30","female");
            nozha.addAnimal(tiger);

            System.out.println("Number of animals in zoo: " + nozha.getSizeOfZoo());
        }
} 

I threw in a getSizeOfZoo() method for you, to get rid of the Id variable. Changed String for area to a Double. Keep your classes seperate, keep the naming convention Class, variable, methodName() etc. It makes it much easier to read. The parameters when creating a lion and tiger were a little off, I've put it as name, type, age, gender now.

Check out the javadoc for ArrayList and you can figure out how to 'remove' an element from your ArrayList (I'm reluctant to do your project FOR you). The issues you have encountered are more around coding basics rather than anything Java specific, or OOP specific. Have a read up, try the mothod removeAnimal(animal an) and see how you get on. We can help if there is any issue, but look at getSizeOfZoo() and addAnimal() and the docs and you should be flying!

Hope that helps.

1 Comment

Maybe rename getSizeOfZoo() to a nicer name like zooPopulation() or numberOfAnimals() or whatever for your own sake and clarity.

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.