Let's count cats! Implement a class named Cat and a static method getNumberOfCats.
These are some features of the class:
- The class
Cathas two instance fields: a stringnameand an intage, and one static int fieldcounter. The staticcounterfield allows you to know how many cats have already been created. - The class should have a constructor for initializing the instance fields. During the initialization of a new cat, the value of the static field
countershould be incremented. Ifcounter > 5, the message "You have too many cats" must be printed toSystem.out.
Your static method getNumberOfCats returns the current number of created cats and prints no messages.
Take a look at the example:
Let's assume seven instances of the class Cat have already been created. Then the correctly implemented method Cat.getNumberOfCats() should return 7.
Note that the message "You have too many cats" should appear twice: the first time when creating the sixth cat and the second time when creating the seventh cat.
Use the provided template and do not make the class private.