I just started learning Java and have a very basic question. I came across this code in a book where the Animal-specific list is built, I read the first part of the code with the MyAnimalList class and thought that was it, but then saw the AnimalTestDrive class below.
Why do we need this Test class, to test our code? Does that mean if the MyAnimalList class is part of some wider code, we wouldn't include the AnimalTestDrive class in there?
Also, would these two classes have to be in different Java files?
public class MyAnimalList {
private Animal[] animals = new Animal[5];
private int nextIndex = 0;
public void add(animal a) {
if (nextIndex < animals.length) {
animals[nextIndex] = a;
System.out.println("Animal added at " + nextIndex);
nextIndex++;
}
}
}
public class AnimalTestDrive {
public static void main (String[] args) {
MyAnimalList list = new MyAnimalList();
Dog a = new Dog();
Cat c = new Cat();
list.add(a);
list.add(c);
}
}