Objectives
- Accessing data in an
ArrayList
ArrayList
Animal
, where each instance represents an individual animal that is characterized by a name
and a type
. Each Animal
that is created is added to a static
ArrayList
that you will instantiate named allAnimals
.public class Animal {
//**TO-DO: Initialize a static ArrayList here called allAnimals**
String name;
String type;
public Animal(String name, String type){
this.name = name;
this.type = type;
allAnimals.add(this);
}
public String toString(){
return name + " (" + type + ")";
}
//**TO-DO: Write method that checks if an animal in the list**
public static boolean isDuplicate(String nameToCheck){
}
}
Animal
with the same name as another Animal
that is already in the list.Animal
with a particular name already exists in allAnimals
, any subsequent Animal
with the exact same name is considered a duplicate.DUPLICATE: There is already an animal with the name "Alice" in the list: Alice (aardvark)
UNIQUE: The name George is unique.
UNIQUE: The name Timothy is unique.
DUPLICATE: There is already an animal with the name "Paul" in the list: Paul (piranha)
DUPLICATE: There is already an animal with the name "Orville" in the list: Orville (ostrich)
UNIQUE: The name Oliver is unique.