/* Author: James Tam Version: Feb. 1, 2021 Learning objectives: understanding the keywords static (class memory or method) and final (unchanging constants). Reviewing the difference between a reference and an object (hobbes appears to have changed inside of the main method). */ public class Cat { //Class constants that specify the mood of a cat. public static final int SLEEPY = 1; public static final int GRUMPY = 2; public static final int PLAYFUL = 3; //Class constant to specify the sensible maximum number of cats. public static final int MAX_POPULATION = 3; //Class variable that trackes the total population. private static int population = 0; //Instance attribute: each cat has it's own mood. private int mood; public Cat () { setMood(GRUMPY); increasePopulation(); } public Cat (int aMood) { setMood(aMood); increasePopulation(); } public String whatMood() { String moodDescription = null; switch (mood) { case GRUMPY: moodDescription = "Grumpy"; break; case SLEEPY: moodDescription = "Sleepy"; break; case PLAYFUL: moodDescription = "Wanting to play :P"; } return(moodDescription); } //Needs to be class specific and not instance specific otherwise there would //be no way to check the population if there were no cat.s public static int getPopulation() { return(population); } //Private because the population should only be changed when a new //cat created (constructor called). private void increasePopulation() { population++; if (population > MAX_POPULATION) { System.out.println("<< Error: Max. cat population of " + MAX_POPULATION + " has been exceeded >>"); } } public void setMood(int newMood) { if ((newMood < SLEEPY) || (newMood > PLAYFUL)) { System.out.println("Mood is not one of the valid values (" + SLEEPY + "-" + PLAYFUL + ")"); mood = SLEEPY; } else { mood = newMood; } } }