title: Java Calculator GUI operations
body: <p>There are 4 classes: <code>Main</code>, <code>GUI</code>, <code>Logic</code> and <code>Calculator</code>. I'm having difficulty making the Calculator class right, the first operation/calculation is giving me the wrong answer but the next one is right. I tried to create another variable (<code>fnum</code> and <code>snum</code>) for the first and second number but it didn't work. Do I need to modify the Logic class? </p>
  
  <p><strong>main class</strong></p>
  
  <pre><code>class Main
  { 
    public static void main( String args[] )
    {
      GUI gui = new GUI();
      gui.display();
    }
  }
  </code></pre>
  
  <p><strong>GUI class</strong></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  class GUI
  {
    private static final int H = 400;       // Height of window pixels
    private static final int W = 500;       // Width  of window pixels
  
    private JLabel      theMes      = new JLabel();       // Message area
    private JTextArea   theOutput1  = new JTextArea();    // Input number area
    private JTextArea   theOutput2  = new JTextArea();    // Result area
    private JScrollPane theSP       = new JScrollPane();
  
    private ButtonPress onButtonPress = new ButtonPress();
  
    public void display()
    {
      JFrame rpc           = new JFrame();            // Window
      Container cp         = rpc.getContentPane();    // Content Pane
      Container rootWindow = (Container) rpc;         // Root Window
      cp.setLayout(null);                             // No layout manager
      rootWindow.setSize( W, H );                     // Size of Window
  
      String labels[] = {
          "7",  "8", "9", "", "CR", "+",
          "4",  "5", "6", "",  "=", "-",
          "1",  "2", "3", "",  "",  "*",
          "C",  "0", "", "",   "",  "/" };
  
      final int LABELS = labels.length;      // # Button Labels
  
      final int GAP   = 15;                  // Horizontal Gap
  
      final int HLAB  = 20;                  // Label
      final int SHLAB = GAP;
      final int EHLAB = SHLAB+HLAB;
  
      final int HTA1  = 30;                  // Output area 1
      final int SHTA1 = EHLAB + GAP;
      final int EHTA1 = SHTA1 + HTA1;
  
      final int HTA2  = 100;                 // Output area 2
      final int SHTA2 = EHTA1 + GAP;
      final int EHTA2 = SHTA2 + HTA2;
  
      final int HBUT  = 170;                 // Buttons
      final int SHBUT = EHTA2 + GAP;
  
      final int BNR  = 4,    BNC  = 6;       // number rows cols of buttons
      final int BW  = W/BNC, BH = HBUT/BNR;  // Size of landscape for button
      final int SBH = SHBUT, SBW = 7;        // Start position for buttons
  
  
      JButton buttons[] = new JButton[LABELS];
  
      Font font = new Font("Serif",Font.BOLD,20);      // Button font
  
      for ( int i=0; i&lt;LABELS; i++ )
      {
        if ( labels[i].length() &gt;= 1 )
        {
          buttons[i] = new JButton( labels[i] );
          final int col = i%BNC * BW, row = i/BNC * BH;
          buttons[i].setBounds( SBW+col, SBH+row, BW-20, BH-10 );
          buttons[i].addActionListener( onButtonPress );
          buttons[i].setFont( font );
          cp.add( buttons[i] );
        }
      }
  
      font = new Font("Serif",Font.BOLD,14);         // Font is
  
      theMes.setBounds( 10, SHLAB, W-25, HLAB );     // Message area
      theMes.setText( "" );                          // Blank
      theOutput1.setFont( font );                    //  Uses font 
      cp.add( theMes );                              //  Add to canvas
  
      theOutput1.setBounds( 10, SHTA1, W-25, HTA1 ); // Input Area
      theOutput1.setText("");                        // Blank
      theOutput1.setFont( font );                    //  Uses font 
      cp.add( theOutput1 );                          //  Add to canvas
  
      font = new Font("Serif",Font.BOLD,14);         // Font is
  
      theSP.setBounds( 10, SHTA2, W-25, HTA2 );      // Scrolling pane
      theOutput2.setText( "" );                      //  Blank
      theOutput2.setFont( font );                    //  Uses font  
      cp.add( theSP );                               //  Add to canvas
      theSP.getViewport().add( theOutput2 );         //  In TextArea
      rootWindow.setVisible( true );                 // Make visible
  
      theMes.setText( "Calculator" );                // Opening message
    }
  
  
    private Calculator calc = new Calculator();
    private Logic logic     = new Logic( calc );
  
    class ButtonPress implements ActionListener       // Listener
    {
      public void actionPerformed( ActionEvent ae )   // Interaction
      {
        String label = ae.getActionCommand();         // Button label  
  
        String info = logic.process( label);
  
        theOutput2.setText( "" );
        if ( info == null )
          theOutput2.append( "" + logic.getResult() );
        else
          theOutput2.append( info );
  
        theOutput1.setText( "" );
        theOutput1.append( "Number entered: " + logic.getNumber() );
      }
    }
  }
  </code></pre>
  
  <p><strong>Logic class</strong></p>
  
  <pre><code>class Logic
  {
    private enum  State { FIRST_NUMBER, SUBSEQUENT_NUMBER };
    private State state = State.FIRST_NUMBER;
    private long  number = 0;
    private char  op = ' ';
    private Calculator calc = null;
  
  
    public Logic( Calculator calculator )
    {
      calc = calculator;
    }
  
    public String process( String button )
    {
      String info = null;
      if ( button.length() == 1 )
      {
        char c = button.charAt(0);
        if ( c &gt;= '0' &amp;&amp; c &lt;= '9' )               // Digit
        {
          number = number * 10 + c-'0';           // Build number
        } else {
          switch ( c )
          {
            case 'C' : number = 0;
                       break;
            case '=' :
            case '+' : case '-' :
            case '*' : case '/' :
              switch ( state )
              {
                case FIRST_NUMBER:
                  calc.setValue( number );
                  state = State.SUBSEQUENT_NUMBER;
                  break;
                case SUBSEQUENT_NUMBER:
                  if ( op != '=' )
                    calc.evaluate( op, number );
                  break;
              }
              op = c;  number = 0;
              break;
          }
        }
      } else {
        if ( button.equals( "CR" ) )               // Clear Result
        {
          calc.reset(); number = 0; state = State.FIRST_NUMBER;
        }
      }
  
      return info;
    }
  
    public long getResult()
    {
      return calc.getValue();
    }
  
    public long getNumber()
    {
      return number;
    }
  }
  </code></pre>
  
  <p><strong>Calculator class</strong></p>
  
  <pre><code>class Calculator
  {
  //Evaluate an arithmetic operation on the stored result
  // E.g evaluate( '+',9) would add 9 to the stored result
  // evaluate( '/',3) would divide the stored result
  // by 3
  // actions are '+'. '-', '*', '/'
  //Note: if the operation is
  // evaluate( '/',0 ) the result returned should be 0
  
  
  long value;
  
  
  public void evaluate( char action, long number )
   {
  
       if (action == '+'){
        value += Float.valueOf(number).floatValue();
  
  
       }
  
      else if (action == '-'){
        value -= Float.valueOf(number).floatValue();
  
      }
  
      else if (action == '*'){
        value *= Float.valueOf(number).floatValue();
  
      }
  
      else if (action == '/'){
        value /= Float.valueOf(number).floatValue();
  
      }
  
  }
  //Return the stored result
  public long getValue()
     {
      return value;
      }
  
  //Set the stored result to number
  public void setValue( long number )
  {
  
  }
  
  //Reset the stored number to 0
  public void reset()
  {
         if ( value != 0) value = 0;
      }
  }
  </code></pre>