001    /*
002     * Calculator Main 
003     *
004     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
005     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
006     * CS101 homepage</a> or email <las@ai.mit.edu>.
007     *
008     * Copyright (C) 1996 Massachusetts Institute of Technology.
009     * Please do not redistribute without obtaining permission.
010     */
011    package calculator;
012    
013    import java.lang.reflect.Constructor;  // Nutshell 2nd ed, pp 483-484
014    import java.applet.*;
015    import java.awt.event.*;
016    import java.awt.*;                                                                               
017    /**
018     * This is a basic skeleton application which launches the necessary
019     * code for a four-function calculator.  It relies on a (Runnable)
020     * ButtonHandler to repeatedly call getButton() and consume the
021     * buttonIDs that the CalculatorGUI object produces.
022     *
023     * <P>Copyright (c) 1998 Massachusetts Institute of Technology
024     *
025     * @author  Emil Sit, sit@mit.edu
026     * @author  Lynn Andrea Stein, las@ai.mit.edu
027     * @author  Craig Henderson, craigh@ai.mit.edu
028     * @author  Natasha Olchanski, natashao@ai.mit.edu
029     * @version: $Id: Main.java,v 1.1.1.1 2002/06/05 21:56:34 root Exp $
030     *
031     * @see calculator.Calculator
032     * @see calculator.CalculatorGUI
033     * @see calculator.ButtonHandler
034     * @see cs101.util.semaphore.IntBuffer
035     *
036     */
037    public class Main extends Applet {
038        
039      /** CalculatorGUI for use when we're started as an Applet. */
040      /* private */ CalculatorGUI mygui;  // !private for bug in JDK
041      
042      /**
043       * Prevent instantiation.
044       */
045      private Main() {}
046    
047      /**
048       * Print the usage information to the standard error stream, and quit.
049       */
050      public static void printUsageAndQuit() {
051        System.err.println( "\n  Only one argument is expected:" +
052                            "\n\t  the name of your button handler.\n");
053        System.exit(1);
054      }
055    
056      /**
057       * Print the exception and any other debugging information to the
058       * standard error stream, and quit.
059       */
060      public static void printExceptionAndQuit(Exception e) {
061        e.printStackTrace( System.err );
062        System.exit(1);
063      }
064    
065      /**
066       * Expects a single argument on the command line, which is
067       * the name of the student's button handler class.
068       * The student's class should have a constructor that accepts
069       * either a Calculator, or nothing at all.
070       */
071      public static void main (String[] argv) {
072    
073        // Only one argument is expected.
074        if (argv.length != 1) {
075          printUsageAndQuit();
076        } else {
077    
078          // these must be declared outside the try-block.
079          Class myClass = null;
080          Constructor constructor = null;
081          Object instance = null;
082          try {
083    
084            // From the name on the command line, produce a Class.
085            // Obtain from this Class the Constructor that takes
086            // one argument, of type Calculator.
087                    
088            myClass = Class.forName(argv[0]);
089            constructor = myClass.getDeclaredConstructor(
090                            new Class[] { Calculator.class }  // anonymous array !
091                                                         );
092    
093            // Using this constructor, create an instance
094            // of the student's class.  This is akin to calling:
095            //
096            //    new ButtonHandler( new CalculatorGUI() );
097            //
098            // where students may call the "ButtonHandler" what they wish.
099                    
100            instance = constructor.newInstance(
101                         new Object[] { new CalculatorGUI() }  // anonymous array !
102                                               );
103            
104          } catch (NoSuchMethodException e) {
105            // If no such constructor could be found, then
106            // call their no-arg constructor.
107            try {               
108              instance = myClass.newInstance();                 
109            } catch (Exception e2) {
110              printExceptionAndQuit( e2 );
111            }
112            
113          } catch (Exception e) {           
114            // Consider any other exception to be fatal.
115            printExceptionAndQuit( e );
116          }
117        }
118      }
119    
120      /**
121       * Initilize the Calculator (as an Applet).  We don't have 
122       * the complications of main here because we're not worried about
123       * loading student's code. 
124       */
125      public void init () {
126        Button calcButton = new Button ("Show/Hide Calculator");
127        this.mygui = new CalculatorGUI();
128        ButtonHandler calc = new ButtonHandler (this.mygui);
129        calcButton.addActionListener(new ActionListener() {
130          public void actionPerformed(ActionEvent evt) {
131            if (Main.this.mygui.isVisible()) 
132              Main.this.mygui.setVisible(false);
133            else Main.this.mygui.show();
134          }
135          });
136        this.add (calcButton);
137        this.setSize(100, 100);
138      }
139    }
140    
141    
142    /* Comments:
143     *
144     * History:
145     *     $Log: Main.java,v $
146     *     Revision 1.1.1.1  2002/06/05 21:56:34  root
147     *     CS101 comes to Olin finally.
148     *
149     *     Revision 1.2  2000/05/01 06:25:19  mharder
150     *     Changed javadoc to agree with new cs101 packages.
151     *
152     *     Revision 1.1  1999/10/08 15:09:24  las
153     *     This pset replaces the old Calculator pset.  However, not everything
154     *     has been transferred.  At the moment, it's just java, doc, and index.
155     *     The rest are still in the repository under Calculator-Old.
156     *
157     *     Revision 1.6  1998/07/24 16:37:15  tparnell
158     *     Placate new javadoc behavior
159     *
160     *     Revision 1.5  1998/07/21 16:36:24  tparnell
161     *     added private Main() to prevent instantiation.
162     *     added workaround for inner class bug in JDK.
163     *
164     *     Revision 1.4  1998/07/20 18:04:19  natashao
165     *     Added init() method to allow Calculator to run as an Applet.
166     *
167     *     Revision 1.3  1998/07/06 19:06:56  tparnell
168     *     *** empty log message ***
169     *
170     *     Revision 1.2  1998/06/07 02:31:30  craigh
171     *     Significant changes to Main.  Namely, students no longer have to name
172     *     their class "ButtonHandler".  Students provide a class name on the
173     *     command line, permitting reflection to be used.  If their class has
174     *     a constructor that takes one argument of type Calculator, it is invoked
175     *     and given an instance of CalculatorGUI.  Otherwise, the no-arg constructor
176     *     of their class is called.  Two methods were added to this class as well,
177     *     for debugging purposes: printUsageAndQuit(), and printExceptionAndQuit().
178     *
179     *     Revision 1.1  1998/02/26 17:25:46  tparnell
180     *     Reconstruction from hard drive failure.  Everything appears intact.
181     *
182     *     Revision 1.2  1997/07/16 14:15:22  tparnell
183     *     *** empty log message ***
184     *
185     *     Revision 1.1  1996/10/04 16:20:23  las
186     *     Transformed Calculator into an application and made it a package.  See
187     *     STAFF_SETUP for which files are public.  To run, use Calculator.Main.
188     *
189     *     Specifics:
190     *         Added Main.java, which starts the calculator program (both
191     *     CalculatorGUI and ButtonHandler);
192     *         Made Calculator an interface;
193     *         Moved GUI implementation (previously in Calculator) to
194     *     CalculatorGUI.
195     *         Added clear button, which looks pretty gross right now.  (It can
196     *     be deleted in a single line, though.)
197     *
198     *
199     */
200    
201    
202    
203