// Math calculations
 import java.awt.*;
 import java.awt.event.*;

 public class Calculator extends Frame implements ActionListener
 {
   public static void main(String[] args)     // main starts the program
   {
     new Calculator();               // construct a Calculator
   }

   //--- Create Components - Buttons, TextFields, etc ---
   Label      info  =  new Label("Type two numbers, click Calculate");
   TextField  textA =  new TextField(9);                 // 9 characters wide
   TextField  textB =  new TextField(9);
   Button     calc  =  new Button("Calculate Answers");
   List       disp  =  new List(10);                     // List has 10 rows
   Button     quit  =  new Button("Quit Program");

   //--- Constructor - add components to Frame, control appearance ----
   public Calculator()
   {
     super("Calculator");            // run Frame constructor and set Title
     setSize(200,320);               // Set size of window
     setLayout(new FlowLayout());    // Use FlowLayout - the simplest Layout

     add(info);                      // Place controls into Frame
     add(textA);                     // They are added in order and
     add(textB);                     // placed one after another as they fit.
     add(calc);                      // The FlowLayout manager determines
     add(disp);                      // the positions - the programmer
     add(quit);                      // doesn't control exact positions.

     calc.addActionListener(this);   // Handle button actions with
     quit.addActionListener(this);   // actionPerformed method in this object

     setVisible(true);               // remember to make Frame visible
   }

   //--- Respond to Buttons ----
   public void actionPerformed(ActionEvent evt)
   {
      Object source = evt.getSource();   // find out which button was clicked

      if (source == quit)
      System.exit(0)}                          // ends the program

      else if (source == calc)
      {
        double a = Double.parseDoubletextA.getText() );
        double b = Double.parseDoubletextB.getText() );

                          // getText() retrieves String from TextField
                          // Double.parseDouble() converts String to double

        double add = a + b;           // double can contain decimal numbers
        double sub = a - b;
        double mul = a * b;
        double div = a / b;
        double exp = Math.pow(a,b);   // Math.pow calculates power a^b

        disp.removeAll();             // clears the disp List

        disp.add(a + " + " + b + " = " + add);  // The + sign is polymorphic
        disp.add(a + " - " + b + " = " + sub);  // It changes numbers to Strings
        disp.add(a + " * " + b + " = " + mul);  //  then joins all the Strings
        disp.add(a + " / " + b + " = " + div);  // You cannot simply .add a
        disp.add(a + " ^ " + b + " = " + exp);  //  a double number to a list
      }
   }

 }