cs231 Lecture Notes Week 3, Monday Homework hints -------------- isStraight: forgot to handle wraparound! Suggestions? A little Java ------------- Global variables: not local to any method; visible from all methods (in a class) Two kinds: instance variables (associated with objects) static variables (associated with class) Syntax: public class Banana { double weight; // pertains to individual bananas static Color color; // pertains to all bananas static int number; // how many bananas objects are there? } You can initialize static variables in situ: public class Banana { static Color color = Color.yellow; static int number = 0; public Banana (double w) { weight = w; number++; } } You can access static variables from both class and object methods. Changing an instance variable affects only a single object. Changing a static variable changes EVERYONE's state. Use static variables sparingly, because they make it very difficult to reason about programs. And that's my next topic... Precomputing Histograms ----------------------- Last time we talked about using histograms to make it easier to classify hands. We need those histograms to be precomputed and then used by many different methods. Instance variables are "visible" from any method, so they are a reasonable mechanism. We could compute them at the time the Deck gets created, but that might be wasteful. Card[] cards; int[] suitHist; int[] rankHist; Then we can write a method that initializes the instance variable (and does the corresponding computation) later... public void makeSuitHist () { suitHist = new int[4]; for (int i=0; i