/* Cat.java example code AC Chapin */ package cis119.pkg11.methods; // a cat -- example of a class, using this. public class Cat { // INSTANCE VARIABLES public String name; public boolean hungry; // METHODS // make a noise public void miaow() { /* here we use the name and hungry variables for whatever Cat we called the method for so if we called mrWhiskers.miaow() then we will use mrWhiskers.name and mrWhiskers.hungry */ System.out.print("Miaow! I'm " + this.name); // only print this part if the hungry // boolean is currently true if (this.hungry) { System.out.print(" and I'm hungry."); } System.out.println(); // newline } // eat a Bunny public void eat(Bunny food) { System.out.println(this.name + " is eating " + food.name + ". YUM!"); this.hungry = false; food.name = "Ex-Bunny"; } // toString method creates String representing the Cat @Override public String toString() { String str = this.name + " the "; if (this.hungry) { str += "hungry "; } return str + "cat"; } // say hi to another cat public void sayHi(Cat othercat) { System.out.println("Cat " + this.name + " says hi to Cat " + othercat.name); } // force another cat to say hi to me public void shyHi(Cat othercat) { // pass pointer to the current cat to the other cat's sayHi method othercat.sayHi(this); } }