Java - Calculator Example

This post will show how to create Calculator Application using Java langguage. This is only an example, you can add some feature into this example such as sin,cos,tan expression.

Note : This example using netbean v 7.4

Step By Step

  1. Create New Project Choose Java Application
  2. Rename Project Name to Calculator
  3. Delete file Calculator.java
  4. Add New JFrame Form under package calculator and change class Name to CalcMainUI
  5. Drag and drop java Swing controller into frame. Refer CalcMainUIbelow. ( Note : you can change the position of button as you like).
  6. Create Java class . calculateClass.java

CalcMainUI Code behind

package calculator;

import java.math.BigDecimal;
import javax.swing.JOptionPane;

/**
 *
 * @author developersnote

 */
public class CalcMainUI extends javax.swing.JFrame {

    /**
     * Creates new form CalcMainUI
     */
    public CalcMainUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jLabel1.setText("Calculation");

        jTextField1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        jButton1.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton1.setText("1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton2.setText("2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton3.setText("3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton4.setText("4");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton5.setText("5");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton6.setText("6");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton7.setText("7");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton8.setText("8");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton9.setText("9");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton10.setText("0");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton11.setText("+");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton12.setText("-");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jButton13.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton13.setText("*");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton14.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton14.setText("/");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton15.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton15.setText("=");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        jButton16.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jButton16.setText("Clear");
        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton16ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButton3))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButton6))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jButton10)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jButton7)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jButton8)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButton9)))
                            .addGap(70, 70, 70)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton5)
                            .addComponent(jButton6)
                            .addComponent(jButton12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton7)
                            .addComponent(jButton8)
                            .addComponent(jButton9)
                            .addComponent(jButton13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton14)
                            .addComponent(jButton10)))
                    .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton16)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    public void printOne() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "1") ;
        }else{
             jTextField1.setText("1");
        }
       
    }
    public void printTwo() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "2") ;
        }else{
             jTextField1.setText("2");
        }
    }
    public void printThree() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "3") ;
        }else{
             jTextField1.setText("3");
        }
    }
    public void printFour() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "4") ;
        }else{
             jTextField1.setText("4");
        }
    }
    public void printFive() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "5") ;
        }else{
             jTextField1.setText("5");
        }
    }
    public void printSix() {
         String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "6") ;
        }else{
             jTextField1.setText("6");
        }
    }
    public void printSeven() {
         String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "7") ;
        }else{
             jTextField1.setText("7");
        }
    }   
    public void printEight() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "8") ;
        }else{
             jTextField1.setText("8");
        }
    } 
    public void printNine() {
         String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "9") ;
        }else{
             jTextField1.setText("9");
        }
    }
    public void printZero() {
        String previousValue = jTextField1.getText();
        if(!previousValue.trim().equalsIgnoreCase("")){
            jTextField1.setText(previousValue + "0") ;
        }else{
             jTextField1.setText("0");
        }
    }

    public void addSymbolPlus() {      
       
        String previousValue = jTextField1.getText();
        if(previousValue.length() > 1){
            if(previousValue.charAt(previousValue.length() - 2) != '+' &&
               previousValue.charAt(previousValue.length() - 2) != '-' &&
               previousValue.charAt(previousValue.length() - 2) != '*' &&
               previousValue.charAt(previousValue.length() - 2) != '/'){
                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " + ") ;
                }else{
                     jTextField1.setText("");

                }
            }
        }else if(previousValue.length() == 1){
            if(previousValue.charAt(previousValue.length() - 1) != '+' &&
               previousValue.charAt(previousValue.length() - 1) != '-' &&
               previousValue.charAt(previousValue.length() - 1) != '*' &&
               previousValue.charAt(previousValue.length() - 1) != '/'){
               if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " + ") ;
                }else{
                     jTextField1.setText("");

                }
            }
        }
       
    }
    public void addSymbolMinus() {
       
        String previousValue = jTextField1.getText();
        if(previousValue.length() > 1){
            if(previousValue.charAt(previousValue.length() - 2) != '+' &&
               previousValue.charAt(previousValue.length() - 2) != '-' &&
               previousValue.charAt(previousValue.length() - 2) != '*' &&
               previousValue.charAt(previousValue.length() - 2) != '/'){
                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " - ") ;
                }else{
                     jTextField1.setText("");
                }
            }
        }else if(previousValue.length() == 1){
             if(previousValue.charAt(previousValue.length() - 1) != '+' &&
               previousValue.charAt(previousValue.length() - 1) != '-' &&
               previousValue.charAt(previousValue.length() - 1) != '*' &&
               previousValue.charAt(previousValue.length() - 1) != '/'){

                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " - ") ;
                }else{
                     jTextField1.setText("");
                }

             }
        }       
       
    }
    public void addSymbolTimes() {

        String previousValue = jTextField1.getText();
        if(previousValue.length() > 1){
            if(previousValue.charAt(previousValue.length() - 2) != '+' &&
               previousValue.charAt(previousValue.length() - 2) != '-' &&
               previousValue.charAt(previousValue.length() - 2) != '*' &&
               previousValue.charAt(previousValue.length() - 2) != '/'){
                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " * ") ;
                }else{
                     jTextField1.setText("");
                }
            }
        }else if (previousValue.length() == 1){
             if(previousValue.charAt(previousValue.length() - 1) != '+' &&
               previousValue.charAt(previousValue.length() - 1) != '-' &&
               previousValue.charAt(previousValue.length() - 1) != '*' &&
               previousValue.charAt(previousValue.length() - 1) != '/'){
                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " * ") ;
                }else{
                     jTextField1.setText("");
                }

              }
        }            
       
    }
    public void addSymbolDevide() {
        String previousValue = jTextField1.getText();
        if(previousValue.length() > 1){
            if(previousValue.charAt(previousValue.length() - 2) != '+' &&
               previousValue.charAt(previousValue.length() - 2) != '-' &&
               previousValue.charAt(previousValue.length() - 2) != '*' &&
               previousValue.charAt(previousValue.length() - 2) != '/'){
                if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " / ") ;
                }else{
                     jTextField1.setText("");
                }
            }
        }else if(previousValue.length() == 1){
             if(previousValue.charAt(previousValue.length() - 1) != '+' &&
               previousValue.charAt(previousValue.length() - 1) != '-' &&
               previousValue.charAt(previousValue.length() - 1) != '*' &&
               previousValue.charAt(previousValue.length() - 1) != '/'){
               if(!previousValue.trim().equalsIgnoreCase("")){
                    jTextField1.setText(previousValue + " / ") ;
                }else{
                     jTextField1.setText("");
                }
             }
        }
    }

    public void calculateValue() {
        try
        {
         calculateClass calc = new calculateClass();//initialize value
         String previousValue = jTextField1.getText();
         String Value = "";
         String symbol = "";
        
         boolean isStartCalc = false;
         boolean getSymbol = false;
         for(int i = 0; i < previousValue.length() ; i++){
             char _temp = previousValue.charAt(i);
             if(_temp != ' '){
                 if(!getSymbol)
                    Value += String.valueOf(_temp);
             }else{                
                    getSymbol = true;                
             }
            
             if(getSymbol){
                 if(_temp != ' '){                    

                     if(!symbol.trim().equalsIgnoreCase("")){
                        calc.calculate(calculateClass.getSymbol(),Double.parseDouble(Value));
                        i++;
                        Value = "";
                        symbol = String.valueOf(_temp);
                        calculateClass.setSymbol(symbol);
                     }else{
                        symbol = String.valueOf(_temp);
                        calculateClass.setSymbol(symbol);
                     }

                     if(calculateClass.getValueCalculateinDouble() == 0.0){
                        if(!Value.equalsIgnoreCase("")){
                            calculateClass.setValueCalculateinDouble(Double.parseDouble(Value));
                            i++;
                        }
                        Value = "";                      
                     }   
                                 
                    
                     getSymbol = false;
                 }
             }

           
         }
         calculateClass.setSymbol(symbol);
         calc.calculate(calculateClass.getSymbol(),Double.parseDouble(Value));

         BigDecimal bd = new BigDecimal(calculateClass.getValueCalculateinDouble());

         jTextField1.setText(bd.toPlainString());
         calculateClass.setSymbol("");
         calculateClass.setValueCalculateinDouble(0.0);
         calculateClass.setValueCalculateinString("");
         Value = "";
         symbol = "";

         isStartCalc = false;
         getSymbol = false;
        }
        catch (Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage());

        }

    }

    public void clear() {
        calculateClass.setSymbol("");
        calculateClass.setValueCalculateinDouble(0.0);
        calculateClass.setValueCalculateinString("");
        jTextField1.setText("");
    }
   
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printSeven();
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printOne();
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printTwo();
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printThree();
    }                                       

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printFour();
    }                                       

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printFive();
    }                                       

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printSix();
    }                                       

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printEight();
    }                                       

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        printNine();
    }                                       

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        printZero();
    }                                        

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        addSymbolPlus();
    }                                        

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        addSymbolMinus();
    }                                        

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        addSymbolTimes();
    }                                        

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        addSymbolDevide();
    }                                        

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         calculateValue();
    }                                        

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        clear();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalcMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalcMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalcMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalcMainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalcMainUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  
}

calculateClass.java class

package calculator;

/**
 *
 * @author developersnote
 */
public class calculateClass {
    private static String _symbol;
    private static String _valueCalculateinString;
    private static double _valueCalculateinDouble;


    public calculateClass(){
        if(_symbol == null){
        _symbol = "";
        _valueCalculateinString = "";
        _valueCalculateinDouble = 0.0;
        }
    }

    public void calculate(String symbol,double value){
        if(symbol.equalsIgnoreCase("+")){
            _valueCalculateinDouble += value;
        }else if(symbol.equalsIgnoreCase("-")){
            _valueCalculateinDouble -= value;
        }else if(symbol.equalsIgnoreCase("*")){
            _valueCalculateinDouble *= value;
        }else if(symbol.equalsIgnoreCase("/")){
            _valueCalculateinDouble /= value;
        }

        _valueCalculateinString = Double.toString(_valueCalculateinDouble);
    }

     /**
     * @return the _symbol
     */
    public static String getSymbol() {
        return _symbol;
    }

    /**
     * @param aSymbol the _symbol to set
     */
    public static void setSymbol(String aSymbol) {
        _symbol = aSymbol;
    }

    /**
     * @return the _valueCalculateinString
     */
    public static String getValueCalculateinString() {
        return _valueCalculateinString;
    }

    /**
     * @param aValueCalculateinString the _valueCalculateinString to set
     */
    public static void setValueCalculateinString(String aValueCalculateinString) {
        _valueCalculateinString = aValueCalculateinString;
    }

    /**
     * @return the _valueCalculateinDouble
     */
    public static double getValueCalculateinDouble() {
        return _valueCalculateinDouble;
    }

    /**
     * @param aValueCalculateinDouble the _valueCalculateinDouble to set
     */
    public static void setValueCalculateinDouble(double aValueCalculateinDouble) {
        _valueCalculateinDouble = aValueCalculateinDouble;
    }  
}



By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example