/* * Calculator Default ButtonHandler * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1999 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package calculator; import cs101.io.Console; import cs101.lang.Animate; import cs101.lang.AnimatorThread; /** * This class provides the smarts for a basic four-function * calculator. It repeatedly calls its Calculator(GUI)'s getButton() * to consume the buttonIDs that the Calculator object produces.

* *

Copyright (c) 1999 Massachusetts Institute of Technology * * @author Todd C. Parnell, tparnell@ai.mit.edu * @author Emil Sit, sit@mit.edu * @author Lynn Andrea Stein, las@ai.mit.edu * @version $Id: ButtonHandler.java,v 1.1.1.1 2002/06/05 21:56:28 root Exp $ * * @see Calculator * @see cs101.util.IntBuffer */ public class ButtonHandler implements Animate { protected Calculator gui; protected CalculatorState brains; protected AnimatorThread spirit; /** * Build a ButtonHandler object from a Calculator gui passed in as * an argument. */ protected ButtonHandler( CalculatorGUI myGUI ) { this.gui = myGUI; this.brains = new CalculatorState( this.gui ); this.brains.resetCalc(); this.spirit = new AnimatorThread(this); this.spirit.start(); } protected ButtonHandler( ) { this( new CalculatorGUI() ); } /** * Provides the interactive control loop of this animate object. * @see java.lang.Animate */ public void act () { int buttonID = this.gui.getButton(); // Deal with number buttons -- add to display text if ( buttonID < 10 && buttonID >= 0) { this.brains.handleNumKey( buttonID ); return; } // deal with everything else switch ( buttonID ) { case Calculator.DOT: this.brains.handleDecimal(); break; case Calculator.OP_MUL: case Calculator.OP_DIV: case Calculator.OP_ADD: case Calculator.OP_SUB: this.brains.handleOperation( buttonID ); break; case Calculator.EQUALS: this.brains.handleEquals(); break; case Calculator.CLEAR: this.brains.resetCalc(); break; default: Console.println("ERROR: unmatched buttonID."); Console.println( this.brains.toString() ); } } } /* Comments: * * History: * $Log: ButtonHandler.java,v $ * Revision 1.1.1.1 2002/06/05 21:56:28 root * CS101 comes to Olin finally. * * Revision 1.2 1999/10/17 05:40:59 jsmthng * Created constructor which took in CalculatorGUI (because it was * complaining that there wasn't one, before) -- no-parameters * constructor still exists. * * Revision 1.1 1999/10/08 15:09:23 las * This pset replaces the old Calculator pset. However, not everything * has been transferred. At the moment, it's just java, doc, and index. * The rest are still in the repository under Calculator-Old. * * Revision 1.5 1998/07/24 16:37:13 tparnell * Placate new javadoc behavior * * Revision 1.4 1998/07/23 14:28:26 tparnell * made class public * * Revision 1.3 1998/07/06 20:22:56 tparnell * changed from while (true)... to while (!this.stopped)... in support of * JDK1.2 deprecation of Thread.stop() * * Revision 1.2 1998/06/05 05:19:26 craigh * added getButtonLabel() to Calculator interface. Implemented the * method in CalculatorGUI, and made use of it in ButtonHandler. * * Revision 1.1 1998/02/26 17:25:43 tparnell * Reconstruction from hard drive failure. Everything appears intact. * * Revision 1.3 1997/10/05 21:11:18 shong * Updated for fall97, to Java 1.1 * changed GUI, using 1.1 Event Model * * Revision 1.2 1997/07/16 14:15:19 tparnell * *** empty log message *** * * Revision 1.2 1996/10/04 16:20:17 las * Transformed Calculator into an application and made it a package. See * STAFF_SETUP for which files are public. To run, use Calculator.Main. * * Specifics: * Added Main.java, which starts the calculator program (both * CalculatorGUI and ButtonHandler); * Made Calculator an interface; * Moved GUI implementation (previously in Calculator) to * CalculatorGUI. * Added clear button, which looks pretty gross right now. (It can * be deleted in a single line, though.) * * */