Reputation: 1
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
Upvotes: 0
Views: 9507
Reputation: 1230
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.
Upvotes: 0
Reputation: 3571
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 empty
arraylist`
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)
Upvotes: 0
Reputation: 691735
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.
Upvotes: 8