home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch06 / Calc.java next >
Encoding:
Java Source  |  1998-12-14  |  9.7 KB  |  465 lines

  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5.  
  6. /**
  7.  * A simple calculator
  8.  */
  9. public class Calc extends Applet
  10. {
  11.  
  12.     Display  display = new Display();
  13.  
  14. /**
  15.  *  Initialize the Calc applet
  16.  */
  17.     public void init () {
  18.  
  19.         setLayout(new BorderLayout());
  20.         Keypad   keypad = new Keypad();
  21.  
  22.         add ("North", display);
  23.         add ("Center", keypad);
  24.     }
  25.  
  26. /**
  27.  * This allows the class to be used either as an applet
  28.  * or as a standalone application.
  29.  */
  30. public static void main (String args[]) {
  31.  
  32.        Frame f = new Frame ("Calculator");
  33.        Calc calc = new Calc ();
  34.  
  35.        calc.init ();
  36.  
  37.        f.setSize (210, 200);
  38.        f.add ("Center", calc);
  39.        f.show ();
  40. }
  41.  
  42. class Keypad extends Panel
  43. implements ActionListener
  44. {
  45.          Button b7;
  46.          Button b8;
  47.          Button b9;
  48.          Button bDiv;
  49.          Button b4;
  50.          Button b5; 
  51.          Button b6;
  52.          Button bMult;
  53.          Button b1;
  54.          Button b2;
  55.          Button b3;
  56.          Button bMin;
  57.          Button bDec;
  58.          Button b0;
  59.          Button bSign;
  60.          Button bPlus;
  61.          Button bC;
  62.          Button bEqu;
  63.  
  64. /**
  65.  * Initialize the keypad, add buttons, set colors, etc.
  66.  */
  67.     Keypad (){
  68.  
  69.          Font    font = new Font ("Times", Font.BOLD, 14);
  70.          Color   functionColor = new Color (255, 255, 0);
  71.          Color   numberColor = new Color (0, 255, 255);
  72.          Color   equalsColor = new Color (0, 255, 0);
  73.          setFont (font);
  74.  
  75.          b7 = new Button ("7");
  76.          add (b7);
  77.          b7.setBackground (numberColor);
  78.          b7.addActionListener(this);
  79.  
  80.          b8 = new Button ("8");
  81.          add (b8);
  82.          b8.setBackground (numberColor);
  83.          b8.addActionListener(this);
  84.  
  85.          b9 = new Button ("9");
  86.          add (b9);
  87.          b9.setBackground (numberColor);
  88.          b9.addActionListener(this);
  89.  
  90.          bDiv = new Button ("/");
  91.          add (bDiv);
  92.          bDiv.setBackground (numberColor);
  93.          bDiv.addActionListener(this);
  94.  
  95.          b4 = new Button ("4");
  96.          add (b4);
  97.          b4.setBackground (numberColor);
  98.          b4.addActionListener(this);
  99.  
  100.          b5 = new Button ("5");
  101.          add (b5);
  102.          b5.setBackground (numberColor); 
  103.          b5.addActionListener(this);
  104.  
  105.          b6 = new Button ("6");
  106.          add (b6);
  107.          b6.setBackground (numberColor);
  108.          b6.addActionListener(this);
  109.  
  110.          bMult = new Button ("x");
  111.          add (bMult);
  112.          bMult.setBackground (numberColor);
  113.          bMult.addActionListener(this);
  114.  
  115.          b1 = new Button ("1");
  116.          add (b1);
  117.          b1.setBackground (numberColor);
  118.          b1.addActionListener(this);
  119.  
  120.          b2 = new Button ("2");
  121.          add (b2);
  122.          b2.setBackground (numberColor);
  123.          b2.addActionListener(this);
  124.  
  125.          b3 = new Button ("3");
  126.          add (b3);
  127.          b3.setBackground (numberColor);
  128.          b3.addActionListener(this);
  129.  
  130.          bMin = new Button ("-");
  131.          add (bMin);
  132.          bMin.setBackground (numberColor);
  133.          bMin.addActionListener(this);
  134.  
  135.          bDec = new Button (".");
  136.          add (bDec);
  137.          bDec.setBackground (numberColor);
  138.          bDec.addActionListener(this);
  139.  
  140.          b0 = new Button ("0");
  141.          add (b0);
  142.          b0.setBackground (numberColor);
  143.          b0.addActionListener(this);
  144.  
  145.          bSign = new Button ("+/-");
  146.          add (bSign);
  147.          bSign.setBackground (numberColor);
  148.          bSign.addActionListener(this);
  149.  
  150.          bPlus = new Button ("+");
  151.          add (bPlus);
  152.          bPlus.setBackground (numberColor);
  153.          bPlus.addActionListener(this); 
  154.  
  155.          bC = new Button ("C");
  156.          add (bC);
  157.          bC.setBackground (functionColor);
  158.          bC.addActionListener(this);
  159.  
  160.          add (new Label (""));
  161.          add (new Label (""));
  162.  
  163.          bEqu = new Button ("=");
  164.          add (bEqu);
  165.          bEqu.setBackground (equalsColor);
  166.          bEqu.addActionListener(this);
  167.  
  168.          setLayout (new GridLayout (5, 4, 4, 4));
  169.     }
  170.  
  171.     public void actionPerformed(ActionEvent event)
  172.     {
  173.         Object object = event.getSource();
  174.  
  175.         if(object == bC)
  176.         {
  177.            display.Clear ();
  178.         }
  179.  
  180.         if(object == bDec)
  181.         {
  182.            display.Dot ();
  183.         }
  184.  
  185.         if(object == bPlus)
  186.         {
  187.            display.Plus ();
  188.         }
  189.  
  190.         if(object == bMin)
  191.         {
  192.            display.Minus ();
  193.         }
  194.  
  195.         if(object == bMult)
  196.         {
  197.            display.Mul ();
  198.         }
  199.  
  200.         if(object == bDiv)
  201.         {
  202.            display.Div ();
  203.         }
  204.  
  205.         if(object == bSign)
  206.         {
  207.            display.Chs ();
  208.         }
  209.  
  210.         if(object == bEqu)
  211.         {
  212.            display.Equals ();
  213.         }
  214.  
  215.         if(object == b0)
  216.         {
  217.            display.Digit ("0");
  218.         }
  219.  
  220.         if(object == b1)
  221.         {
  222.            display.Digit ("1");
  223.         }
  224.  
  225.         if(object == b2)
  226.         {
  227.            display.Digit ("2");
  228.         }
  229.  
  230.         if(object == b3)
  231.         {
  232.            display.Digit ("3");
  233.         }
  234.  
  235.         if(object == b4)
  236.         {
  237.            display.Digit ("4");
  238.         }
  239.  
  240.         if(object == b5)
  241.         {
  242.            display.Digit ("5");
  243.         }
  244.  
  245.         if(object == b6)
  246.         {
  247.            display.Digit ("6");
  248.         }
  249.  
  250.         if(object == b7)
  251.         {
  252.            display.Digit ("7");
  253.         }
  254.  
  255.         if(object == b8)
  256.         {
  257.            display.Digit ("8");
  258.         }
  259.  
  260.         if(object == b9)
  261.         {
  262.            display.Digit ("9");
  263.         }
  264.  
  265.     }
  266. }
  267. }
  268.  
  269. /* -------------------------------------------------- */
  270.  
  271. /**
  272.  * The Keypad handles the input for the calculator
  273.  * and writes to the display.
  274.  */
  275.  
  276. /* -------------------------------------------------- */
  277.  
  278. /**
  279.  * The Display class manages displaying the calculated result
  280.  * as well as implementing the calculator function keys.
  281.  */
  282. class Display extends Panel{
  283.  
  284.     double     last = 0;
  285.     int        op = 0;
  286.     boolean    equals = false;
  287.     int        maxlen = 10;
  288.     String     s;
  289.     Label      readout = new Label("");
  290.  
  291. /**
  292.  * Initialize the display
  293.  */
  294.     Display () {
  295.  
  296.        setLayout(new BorderLayout());
  297.        setBackground (Color.red);
  298.         setFont (new Font ("Courier", Font.BOLD + Font.ITALIC, 30));
  299.         readout.setAlignment(1);
  300.         add ("Center",readout);
  301.        repaint();
  302.         Clear ();
  303.     }
  304.  
  305. /**
  306.  * Handle clicking a digit.
  307.  */
  308.     void Digit (String digit) {
  309.          checkEquals ();
  310.  
  311.               /*
  312.                *       Strip leading zeros
  313.                */
  314.          if (s.length () == 1 && s.charAt (0) == '0' && digit.charAt (0) != '.')
  315.                      s = s.substring (1);
  316.  
  317.          if (s.length () < maxlen)
  318.                      s = s + digit;
  319.          showacc ();
  320.     }
  321.  
  322. /**
  323.  * Handle a decimal point.
  324.  */
  325.     void Dot () {
  326.          checkEquals ();
  327.  
  328.               /*
  329.                *       Already have '.'
  330.                */
  331.          if (s.indexOf ('.') != -1)
  332.                      return;
  333.  
  334.          if (s.length () < maxlen)
  335.                      s = s + ".";
  336.          showacc ();
  337.     }
  338.  
  339. /**
  340. If the user clicks equals without
  341.  clicking an operator
  342.  * key first (+,-,x,/), zero the display.
  343.  */
  344.     private void checkEquals () {
  345.          if (equals == true) {
  346.                         equals = false;
  347.                      s = "0";
  348.          }
  349.     }
  350.  
  351. /**
  352.  * Stack the addition operator for later use.
  353.  */
  354.     void Plus () {
  355.          op = 1;
  356.          operation ();
  357.     }
  358.  
  359. /**
  360.  * Stack the subtraction operator for later use.
  361.  */
  362.     void Minus () {
  363.          op = 2;
  364.          operation ();
  365.     }
  366.  
  367. /**
  368.  * Stack the multiplication operator for later use.
  369.  */
  370.     void Mul () {
  371.          op = 3;
  372.          operation ();
  373.     }
  374.  
  375. /**
  376.  * Stack the division operator for later use.
  377.  */
  378.     void Div () {
  379.          op = 4;
  380.          operation ();
  381.     }
  382.  
  383. /**
  384.  * Interpret the display value as a double, and store it
  385.  * for later use (by Equals).
  386.  */
  387.     private void operation () {
  388.          if (s.length () == 0) return;
  389.  
  390.          Double xyz = Double.valueOf (s);
  391.          last = xyz.doubleValue ();
  392.  
  393.          equals = false;
  394.          s = "0";
  395.     }
  396. /**
  397.  * Negate the current value and redisplay.
  398.  */
  399.     void Chs () {
  400.          if (s.length () == 0) return;
  401.  
  402.          if (s.charAt (0) == '-') s = s.substring (1);
  403.          else s = "-" + s;
  404.  
  405.          showacc ();
  406.     }
  407.  
  408. /**
  409.  * Finish the last calculation and display the result.
  410.  */
  411.     void Equals () {
  412.          double acc;
  413.  
  414.          if (s.length () == 0)  return;
  415.          Double xyz = Double.valueOf (s);
  416.          switch (op)  {
  417.               case 1:
  418.                      acc = last + xyz.doubleValue ();
  419.                      break;
  420.  
  421.               case 2:
  422.                      acc = last - xyz.doubleValue ();
  423.                      break;
  424.  
  425.               case 3:
  426.                      acc = last * xyz.doubleValue ();
  427.                      break;
  428.  
  429.               case 4:
  430.                      acc = last / xyz.doubleValue ();
  431.                      break;
  432.  
  433.               default:
  434.                      acc = 0;
  435.                      break;
  436.          }
  437.  
  438.          s = new Double (acc).toString ();
  439.          showacc ();
  440.          equals = true;
  441.          last = 0;
  442.          op = 0;
  443.     }
  444.  
  445. /**
  446.  * Clear the display and the internal last value.
  447.  */
  448.     void Clear () {
  449.          last = 0; 
  450.          op = 0;
  451.          s = "0";
  452.          equals = false;
  453.          showacc ();
  454.     }
  455.  
  456. /**
  457.  * Demand that the display be repainted.
  458.  */
  459.     private void showacc () {
  460.         readout.setText(s);
  461.         repaint ();
  462.     }
  463.  
  464. }
  465.