In: Computer Science
Java program
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
public 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; } public static void main(String[] args) { Animal animal1 = new Animal(); System.out.println("Number of animals created is " + Animal.getCount()); Animal animal2 = new Animal(); System.out.println("Number of animals created is " + Animal.getCount()); Animal animal3 = new Animal(); System.out.println("Number of animals created is " + Animal.getCount()); System.out.println("Count of first animal: " + animal1.getMyCount()); System.out.println("Count of second animal: " + animal2.getMyCount()); System.out.println("Count of third animal: " + animal3.getMyCount()); } }