package Classes; public class Dog { public String name; public int sticksFetched; public Dog myFriend; public Cat enemy; // behavior method public void fetch() { // methods can access instance variables sticksFetched++; } public void bark(){ // methods can access instance variables System.out.println(name + " says woof"); } public void chaseCat(){ // methods can access instance variables // if instance variables have their own instance variables // we can access those too System.out.println(name + " chases " + enemy.catname); } public void loseSticks() { // local variable, only exists in this method int old = sticksFetched; sticksFetched /= 2; System.out.println(name + " had " + old + " sticks but now only has " + sticksFetched); } public void gainSticks() { // local variable, happens to have same name as other local variable int old = sticksFetched; sticksFetched *= 2; System.out.println(name + " had " + old + " sticks but now has " + sticksFetched); } public String toString() { // simple version that ignores friend and enemy return "a dog called " + name + " with " + sticksFetched + " sticks"; // fancier version that includes friend and enemy // but if two dogs are each others friend, this goes on forever /* return "a dog called " + name + " with " + sticksFetched + " sticks" +" whose friend is " + myFriend +" and whose enemy is " + enemy; */ // this version doesn't blow up for mutual friend dogs // but does blow up if dog doesn't have friend and enemy /* return "a dog called " + name + " with " + sticksFetched + " sticks" +" whose friend is " + myFriend.name +" and whose enemy is " + enemy.catname; */ // we need conditionals so we can have a fancier version without blowing up! } }