class Calculator { /* * Notice these variables are private -- we wouldn't * want some instance of the gremlin class out there * to change them * * * Notice also the absence of the "static" specifier. * In Java, they are dynamic (instance) variables, unless * specified as static (class) variables * * Remember, these are one per instance (object) */ private double input; /* Notice I used double. Why use float? */ private double total; /* Would only make sense if super many of them */ private char op; /* * This is the constructor. It works just as it did in C++ * I prefer to think of it as the Initializer, since * the new operator really builds the class. This * just initializes it, after it already exists. * * Notice that it has the same name of the class. * Notice also that it lacks a return type. This is because * the new operator returns the reference, not this * method. * * Notice also that it is public. It could be private. The * "classic" use of this is the Singleton pattern, which * uses a public method to call a private constructor. This * public methods sets a static variable (class variable) the * first time it is called, then checks it after that. If it * has been set, it doesn't call the constructor. This ensures * that only one instance fo the class is created. * * But, for almost, if not all, of our applications, the * constructor will be public. */ public Calculator() { /* Notice that public methods can be called from within the same class */ reset(); } void enterNumber(double input) { /* * Notice the use of "this" to distinguish the * instance variable form the parameter (local variable). * "this" is a reference to the class upon which the * method has been invoked. * * In Java, unlike C++, references are not addressess or * pointers -- they are truely "names". "this" is * one name for the active class -- but there may be others. * Just like those guys on "America's Most Wanted". */ this.input = input; /* * "println" is much like printf() in C, print "" in Perl, * or "cout" in C++. It is used to send output to the console. * "System.out" is a reference, or name, for the console. * Specifically, "out" is a name for an instance of the PrintStream * class contained within the "System". In other words, the * System object contains the "out" object. * * In Java, the "." is the "scope operator". To the left of the * "." is the name of an object (a "reference"). To the right is * something within that method. * * In this case, we want toe out object within system, and the * println() method within that out object. */ System.out.println (input); } void enterOperation (char op) { /* Notice the use fo the helper method -- this time a private one */ doCurrentOperation(); if (op != '=') this.op = op; else this.op = '+'; System.out.println (total); } public void reset() { input = total = 0.0; op = '+'; } /* * Notice the specifier "private". This is not a part of the * interface. It is private. It is a helper method */ private void doCurrentOperation() { switch (op) { case '+': total += input; break; case '-': total -= input; break; case '*': total *= input; break; case '/': total /= input; break; default: // We'll learn a better way of handling this soon. System.out.println ("Illegal operation"); } input = 0.0; } /* * Notice the main is "static". There is only one for all * classes. In this case, it is a test driver. It isn't very good * at all, but it shows the point. * * It creates an instance of the Calculator with which to play */ public static void main (String []args) { double a = 1.23; double b = 4.56; /* Notice the use of "new" to create the object * Notice also that the constructor is called without any * paramters. */ Calculator c = new Calculator(); c.enterNumber (a); c.enterOperation ('+'); c.enterNumber (b); c.enterOperation ('='); c.enterOperation ('/'); c.enterNumber (2.0); c.enterOperation ('='); c.enterOperation ('-'); c.enterNumber (2.0); c.enterOperation ('-'); c.enterNumber (1.0); c.enterOperation ('='); } }