/** Here's a sample solution to Tutorial 5, by head TA Adam Foster */ public class Cupboard { // instance variables: private int amountOfFruit = 0; // in pieces // Class variables private static int amountOfBread = 0; // in slices private static int amountOfPB = 0; // in grams // purchase x pieces of fruit public void littleShop(int x) { amountOfFruit += x; } // buy some groceries public void bigShop() { amountOfBread += 23; amountOfPB += 1000; amountOfFruit += 5; } // eat a meal public void eatMeal() { amountOfBread -= 2; amountOfPB -= 10; amountOfFruit--; } // returns whether or not there is enough food // for a meal in the cupboard public boolean enoughForMeal() { return(amountOfBread>=2 && amountOfFruit>=1 && amountOfPB>=10); } // throw out all shared items (Class items) public static void cleanCommunalCupboard() { amountOfBread = 0; amountOfPB = 0; } // clean everything out of the cupboard public void clean() { cleanCommunalCupboard(); amountOfFruit = 0; } } ========================================================== public class T5Driver { public static void main(String [] args) { // create one cupboard for a student Cupboard student1 = new Cupboard(); // it will be empty, so there shouln't be enough for a meal System.out.println(student1.enoughForMeal()); // do some shopping student1.bigShop(); // we should have for student1: // Bread = 23, PB = 1000, Fruit = 5 -> enough! System.out.println(student1.enoughForMeal()); student1.eatMeal(); // we should have for student1: // Bread = 21, PB = 990, Fruit = 4 -> enough! System.out.println(student1.enoughForMeal()); Cupboard.cleanCommunalCupboard(); // we should have for student1: // Bread = 0, PB = 0, Fruit = 4 -> not enough! System.out.println(student1.enoughForMeal()); // create a second cupboard for a second student. // remember, static items are SHARED among all cupboards Cupboard student2 = new Cupboard(); // we should have for student2: // Bread = 0, PB = 0, Fruit = 0 -> not enough! System.out.println(student2.enoughForMeal()); student2.bigShop(); // we should have for student2: // Bread = 23, PB = 1000, Fruit = 5 -> enough! System.out.println(student2.enoughForMeal()); student2.eatMeal(); // we should have for student2: // Bread = 21, PB = 990, Fruit = 4 -> enough! System.out.println(student2.enoughForMeal()); student2.eatMeal(); // we should have for student2: // Bread = 19, PB = 980, Fruit = 3 -> enough! System.out.println(student2.enoughForMeal()); student2.eatMeal(); // we should have for student2: // Bread = 17, PB = 970, Fruit = 2 -> enough! System.out.println(student2.enoughForMeal()); student2.eatMeal(); // we should have for student2: // Bread = 15, PB = 960, Fruit = 1 -> enough! System.out.println(student2.enoughForMeal()); student2.eatMeal(); // we should have for student2: // Bread = 13, PB = 950, Fruit = 0 -> not enough! System.out.println(student2.enoughForMeal()); // we should have for student1: // Bread = 13, PB = 950, Fruit = 4 -> enough! System.out.println(student1.enoughForMeal()); } } __________________________________________________ Do You Yahoo!? Great stuff seeking new owners in Yahoo! Auctions! http://auctions.yahoo.com