/** * A simple model of a dog * * @author CSC 142 */ public class Dog { // a dog is defined by its name and whether it is hungry // or not private String name; private boolean isHungry = true; // could assign true // to isHungry in the constructor // what the dog at previously private String prevFood; private int countFetches; /** * Creates a hungry dog given its name * * @param name * the name of the dog */ public Dog(String name) { this.name = name; // isHungry = true; } /** * Feeds the dog if the dog wants to eat * * @param food * the food given to the dog * @return true if the dog has eaten, and false if not. */ public boolean eat(String food) { if (isHungry) { // the dog might eat // ! means not if (!food.equals(prevFood)) { System.out.println("Yummy!"); prevFood = food; countFetches = 0; isHungry = false; return true; } else { System.out.println("Oh no, not the same thing again!"); return false; } } else { // the dog doesn't eat System.out.println("Thank you, but I am not hungry."); return false; } } /** * Plays fetch with the dog */ public boolean fetch() { if (!isHungry) { // the dog plays fetch System.out.println("I love to play fetch."); countFetches++; // same as countFetches = countFetches + 1 if (countFetches >= 5) { isHungry = true; } return true; } else { System.out.println( "I am too hungry to play fetch."); return false; } } }