home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Bluette / Examples / Calculator.java < prev    next >
Encoding:
Java Source  |  1996-08-25  |  10.6 KB  |  329 lines

  1. // {$R Calculator.JFM}
  2.  
  3. //          I partially copied some of codes from following reference.
  4. //          
  5. //
  6. //          - Copyright notice for Example reference -
  7. //
  8. //            Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
  9. //            Published By SunSoft Press/Prentice-Hall
  10. //            Copyright (C) 1996 Sun Microsystems Inc.
  11. //            All Rights Reserved. ISBN 0-13-565755-5
  12.  
  13. import java.awt.*;
  14. import java.applet.Applet;
  15.  
  16. // Class Calculator
  17. public class Calculator extends Applet
  18. {
  19.     final int MenuBarHeight = 0;
  20.  
  21.     public double arg = 0;
  22.     public String op = "=";
  23.     public boolean start = true;
  24.  
  25.     // Component Declaration
  26.     public TextField display;
  27.     public Panel Panel1;
  28.     public Button Button1;
  29.     public Button Button2;
  30.     public Button Button3;
  31.     public Button Button4;
  32.     public Button Button5;
  33.     public Button Button6;
  34.     public Button Button7;
  35.     public Button Button8;
  36.     public Button Button9;
  37.     public Button Button10;
  38.     public Button Button11;
  39.     public Button Button12;
  40.     public Button Button13;
  41.     public Button Button14;
  42.     public Button Button15;
  43.     public Button Button16;
  44.     // End of Component Declaration
  45.  
  46.     // init()
  47.     public void init()
  48.     {
  49.         // Frame Initialization
  50.         setForeground(Color.black);
  51.         setBackground(Color.lightGray);
  52.         setFont(new Font("Dialog",Font.BOLD,12));
  53.         setLayout(null);
  54.         // End of Frame Initialization
  55.  
  56.         // Component Initialization
  57.         display = new TextField("0");
  58.         display.setForeground(Color.black);
  59.         display.setBackground(Color.lightGray);
  60.         display.setFont(new Font("Dialog",Font.BOLD,12));
  61.         display.setEditable(false);
  62.         Panel1 = new Panel();
  63.         Panel1.setLayout(null);
  64.         Panel1.setForeground(Color.black);
  65.         Panel1.setBackground(Color.lightGray);
  66.         Panel1.setFont(new Font("Dialog",Font.BOLD,12));
  67.         Button1 = new Button("1");
  68.         Button1.setFont(new Font("Dialog",Font.BOLD,12));
  69.         Button2 = new Button("2");
  70.         Button2.setFont(new Font("Dialog",Font.BOLD,12));
  71.         Button3 = new Button("3");
  72.         Button3.setFont(new Font("Dialog",Font.BOLD,12));
  73.         Button4 = new Button("4");
  74.         Button4.setFont(new Font("Dialog",Font.BOLD,12));
  75.         Button5 = new Button("5");
  76.         Button5.setFont(new Font("Dialog",Font.BOLD,12));
  77.         Button6 = new Button("6");
  78.         Button6.setFont(new Font("Dialog",Font.BOLD,12));
  79.         Button7 = new Button("7");
  80.         Button7.setFont(new Font("Dialog",Font.BOLD,12));
  81.         Button8 = new Button("8");
  82.         Button8.setFont(new Font("Dialog",Font.BOLD,12));
  83.         Button9 = new Button("9");
  84.         Button9.setFont(new Font("Dialog",Font.BOLD,12));
  85.         Button10 = new Button("0");
  86.         Button10.setFont(new Font("Dialog",Font.BOLD,12));
  87.         Button11 = new Button(".");
  88.         Button11.setFont(new Font("Dialog",Font.BOLD,12));
  89.         Button12 = new Button("=");
  90.         Button12.setFont(new Font("Dialog",Font.BOLD,12));
  91.         Button13 = new Button("+");
  92.         Button13.setFont(new Font("Dialog",Font.BOLD,12));
  93.         Button14 = new Button("-");
  94.         Button14.setFont(new Font("Dialog",Font.BOLD,12));
  95.         Button15 = new Button("*");
  96.         Button15.setFont(new Font("Dialog",Font.BOLD,12));
  97.         Button16 = new Button("/");
  98.         Button16.setFont(new Font("Dialog",Font.BOLD,12));
  99.         // End of Component Initialization
  100.  
  101.         // Add()s
  102.         Panel1.add(Button16);
  103.         Panel1.add(Button15);
  104.         Panel1.add(Button14);
  105.         Panel1.add(Button13);
  106.         Panel1.add(Button12);
  107.         Panel1.add(Button11);
  108.         Panel1.add(Button10);
  109.         Panel1.add(Button9);
  110.         Panel1.add(Button8);
  111.         Panel1.add(Button7);
  112.         Panel1.add(Button6);
  113.         Panel1.add(Button5);
  114.         Panel1.add(Button4);
  115.         Panel1.add(Button3);
  116.         Panel1.add(Button2);
  117.         Panel1.add(Button1);
  118.         add(Panel1);
  119.         add(display);
  120.         // End of Add()s
  121.  
  122.         InitialPositionSet();
  123.     } // End of init()
  124.  
  125.     // start()
  126.     public void start()
  127.     {
  128.     } // End of start()
  129.  
  130.     // stop()
  131.     public void stop()
  132.     {
  133.     } // End of stop()
  134.  
  135.     // destroy()
  136.     public void destroy()
  137.     {
  138.     } // End of destroy()
  139.  
  140.     public void paint(Graphics g)
  141.     {
  142.         // paint()
  143.         // End of paint()
  144.     }
  145.  
  146.     public void InitialPositionSet()
  147.     {
  148.         // InitialPositionSet()
  149.         resize(193,217);
  150.         display.reshape(8,4+MenuBarHeight,179,24);
  151.         Panel1.reshape(7,34+MenuBarHeight,179,177);
  152.         Button1.reshape(9,8,40,40);
  153.         Button2.reshape(49,8,40,40);
  154.         Button3.reshape(89,8,40,40);
  155.         Button4.reshape(129,8,40,40);
  156.         Button5.reshape(9,48,40,40);
  157.         Button6.reshape(49,48,40,40);
  158.         Button7.reshape(89,48,40,40);
  159.         Button8.reshape(129,48,40,40);
  160.         Button9.reshape(9,88,40,40);
  161.         Button10.reshape(49,88,40,40);
  162.         Button11.reshape(89,88,40,40);
  163.         Button12.reshape(129,88,40,40);
  164.         Button13.reshape(9,128,40,40);
  165.         Button14.reshape(49,128,40,40);
  166.         Button15.reshape(89,128,40,40);
  167.         Button16.reshape(129,128,40,40);
  168.         // End of InitialPositionSet()
  169.     }
  170.  
  171.     public boolean handleEvent(Event evt)
  172.     {
  173.         // handleEvent()
  174.         if (evt.id == Event.WINDOW_DESTROY && evt.target == this) Calculator_WindowDestroy(evt.target);
  175.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button1) Button1_Action(evt.target);
  176.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button11) Button1_Action(evt.target);
  177.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button2) Button1_Action(evt.target);
  178.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button3) Button1_Action(evt.target);
  179.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button4) Button1_Action(evt.target);
  180.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button5) Button1_Action(evt.target);
  181.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button6) Button1_Action(evt.target);
  182.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button7) Button1_Action(evt.target);
  183.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button8) Button1_Action(evt.target);
  184.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button9) Button1_Action(evt.target);
  185.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button10) Button1_Action(evt.target);
  186.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button14) Button14_Action(evt.target);
  187.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button13) Button13_Action(evt.target);
  188.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button15) Button13_Action(evt.target);
  189.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button16) Button13_Action(evt.target);
  190.         else if (evt.id == Event.ACTION_EVENT && evt.target == Button12) Button13_Action(evt.target);
  191.         // End of handleEvent()
  192.  
  193.         return super.handleEvent(evt);
  194.     }
  195.  
  196.     // Event Handling Routines
  197.     public void Calculator_WindowDestroy(Object target)
  198.     {
  199.         System.exit(0);
  200.     }
  201.  
  202.     public void Button1_Action(Object target)
  203.     {
  204.         String str;
  205.          
  206.         str = ((Button)(target)).getLabel();
  207.         
  208.         if (start) display.setText(str);
  209.         else display.setText(display.getText() + str);
  210.  
  211.         start = false;
  212.     }
  213.  
  214.     public void Button14_Action(Object target)
  215.     {
  216.         String str;
  217.         
  218.         str = ((Button)(target)).getLabel();
  219.     
  220.         if (start)
  221.         { 
  222.             display.setText(str); 
  223.             start = false; 
  224.         }
  225.         else
  226.         {  
  227.             calculate(atof(display.getText()));
  228.             op = str;
  229.             start = true;
  230.         }
  231.      
  232.     }
  233.  
  234.     public void Button13_Action(Object target)
  235.     {
  236.         String str;
  237.         
  238.         str = ((Button)(target)).getLabel();
  239.     
  240.         if (start) op = str;
  241.         else
  242.         {  
  243.             calculate(atof(display.getText()));
  244.             op = str;
  245.             start = true;
  246.         }
  247.     }
  248.  
  249.     // End of Event Handling Routines
  250.  
  251.     public void calculate(double n)
  252.     {  
  253.         if (op == "+") arg += n;
  254.         else if (op == "-") arg -= n;
  255.         else if (op == "*") arg *= n;
  256.         else if (op == "/") arg /= n;
  257.         else if (op == "%") arg %= n;
  258.         else if (op == "=") arg = n;
  259.         display.setText("" + arg);
  260.    }
  261.    
  262.     public double atof(String s)
  263.     {  
  264.        int i = 0;
  265.        int sign = 1;
  266.        double r = 0; // integer part
  267.        double f = 0; // fractional part
  268.        double p = 1; // exponent of fractional part
  269.        int state = 0; // 0 = int part, 1 = frac part
  270.       
  271.        while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
  272.        if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
  273.        else if (i < s.length() && s.charAt(i) == '+') { i++; }
  274.        while (i < s.length())
  275.        {  
  276.            char ch = s.charAt(i);
  277.            if ('0' <= ch && ch <= '9')
  278.            {  
  279.                if (state == 0) r = r * 10 + ch - '0';
  280.                else if (state == 1)
  281.                {  
  282.                    p = p / 10;
  283.                    r = r + p * (ch - '0');
  284.                }
  285.            }
  286.            else if (ch == '.') 
  287.            {  
  288.                if (state == 0) state = 1; 
  289.                else return sign * r;
  290.            }
  291.            else if (ch == 'e' || ch == 'E')
  292.            {  
  293.                long e = (int)parseLong(s.substring(i + 1), 10);
  294.                return sign * r * Math.pow(10, e);
  295.            }
  296.            else return sign * r;
  297.            i++;
  298.        }
  299.        return sign * r;
  300.     }
  301.  
  302.     public long parseLong(String s, int base)
  303.     {  
  304.         int i = 0;
  305.         int sign = 1;
  306.         long r = 0;
  307.       
  308.       while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
  309.       if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
  310.       else if (i < s.length() && s.charAt(i) == '+') { i++; }
  311.       while (i < s.length())
  312.       {  char ch = s.charAt(i);
  313.          if ('0' <= ch && ch < '0' + base)
  314.             r = r * base + ch - '0';
  315.          else if ('A' <= ch && ch < 'A' + base - 10)
  316.             r = r * base + ch - 'A' + 10 ;
  317.          else if ('a' <= ch && ch < 'a' + base - 10)
  318.             r = r * base + ch - 'a' + 10 ;
  319.          else 
  320.             return r * sign;
  321.          i++;
  322.       }
  323.       return r * sign;      
  324.    }
  325.  
  326.    
  327. } // End of Class Calculator
  328.  
  329.