In: Computer Science
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
class Animal { private static int count = 0; private int myCount; public Animal() { myCount = ++count; } public static int getCount() { return count; } public static void setCount(int count) { Animal.count = count; } public int getMyCount() { return myCount; } } class AnimalTest { public static void main(String[] args) { System.out.println("Number of animals created: " + Animal.getCount()); Animal animal1 = new Animal(); System.out.println("Number of animals created: " + Animal.getCount()); Animal animal2 = new Animal(); System.out.println("Number of animals created: " + Animal.getCount()); Animal animal3 = new Animal(); System.out.println("Number of animals created: " + Animal.getCount()); System.out.println("Count of animal1: " + animal1.getMyCount()); System.out.println("Count of animal2: " + animal2.getMyCount()); System.out.println("Count of animal3: " + animal3.getMyCount()); } }