How to Make Scientific Calculator with GUI in Java

The Calculator performs all mathematical calculation including Sin Cos and Tan functions. We write this code in Eclipse and you can run it as well in Netbeans. 

Source Code

 Calculator.Java
public class Calculator {
    int x,y;
    public Calculator()
    {   x=0;
        y=0;
    }
    int sum(int a,int b)
    {   int c=a+b;
        return c;
    }
    int sum(int a,int b,int c)
    {   int d=a+b+c;
        return d;
    }
    float sum(float a,float b)
    {   float c=a+b;
        return c;
    }
    int mul(int a,int b)
    {   int c=a*b;
        return c;
    }
    int mul(int a,int b,int c)
    {   int d=a*b*c;
        return d;
    }
    float mul(float a,float b)
    {   float c=a*b;
        return c;
    }
    int div(int a,int b)
    {   int c=a/b;
        return c;
    }
    int div(int a,int b,int c)
    {   int d=a/b/c;
        return d;
    }
    float div(float a,float b)
    {   float c=a/b;
        return c;
    }
    int sub(int a,int b)
    {   int c=a-b;
        return c;
    }
    int sub(int a,int b,int c)
    {   int d=a-b-c;
        return d;
    }
    float sub(float a,float b)
    {   float c=a-b;
        return c;
    }
    double sin(double a)
    {
        Math.sin(a);
        return Math.sin(a);
    }
    double cos(double b)
    {
        Math.cos(b);
        return Math.cos(b);
    }
    double tan(double c) 
    {
         Math.tan(c);
         return Math.tan(c);
    }
     double log(double d) 
     {
         Math.log(d);
         return Math.log(d);
    }
     double log10(double l)
     {
         return Math.log10(x)/Math.log(10);
    }
     int remainder(int a,int b)
     {
         int c=(a/b);
         int d=(c*b);
         int e=(-d+a);
         return e;
     }
    double power(int a,int b)
    {
    double p = Math.pow(a, b);
    return p;
    }
    int isPrime(int n)
    {
        for(int i=2;2*i<n;i++){
            if(n%i==0)
                return 0;
        }
    return 1;
    }
    int fact(int n)
    {
        int num=n,fact=1;
        for (int a=num;a>0;a--)
            fact*=a;
        return fact;
    }
    }


Calculatorform.Java
public class Calculatorform extends javax.swing.JFrame {

    
    public Calculatorform() {
        initComponents();
    }
Calculator obj=new Calculator();
    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();
        txtVal1 = new javax.swing.JTextField();
        txtVal2 = new javax.swing.JTextField();
        BtnPlus = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        BtnMinus = new javax.swing.JButton();
        BtnMultiply = new javax.swing.JButton();
        BtnDivide = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        btnSine = new javax.swing.JButton();
        btnTangent = new javax.swing.JButton();
        btnCosine = new javax.swing.JButton();
        btnLog = new javax.swing.JButton();
        btnLn = new javax.swing.JButton();
        btnpower = new javax.swing.JButton();
        btnRemainder = new javax.swing.JButton();
        btnFactorial = new javax.swing.JButton();

        jCheckBox1.setText("jCheckBox1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.setText("jTextField1");

        txtVal1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtVal1ActionPerformed(evt);
            }
        });

        txtVal2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtVal2ActionPerformed(evt);
            }
        });

        BtnPlus.setText("+");
        BtnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnPlusActionPerformed(evt);
            }
        });

        jLabel2.setText("Enter Second Value");

        BtnMinus.setText("-");
        BtnMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnMinusActionPerformed(evt);
            }
        });

        BtnMultiply.setText("*");
        BtnMultiply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnMultiplyActionPerformed(evt);
            }
        });

        BtnDivide.setText("/");
        BtnDivide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnDivideActionPerformed(evt);
            }
        });

        jLabel3.setText("Enter First Value");

        btnSine.setText("Sin");
        btnSine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSineActionPerformed(evt);
            }
        });

        btnTangent.setText("Tan");
        btnTangent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTangentActionPerformed(evt);
            }
        });

        btnCosine.setText("Cos");
        btnCosine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCosineActionPerformed(evt);
            }
        });

        btnLog.setText("log");
        btnLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogActionPerformed(evt);
            }
        });

        btnLn.setText("ln");
        btnLn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLnActionPerformed(evt);
            }
        });

        btnpower.setText("Po");
        btnpower.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnpowerActionPerformed(evt);
            }
        });

        btnRemainder.setText("Rem");
        btnRemainder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemainderActionPerformed(evt);
            }
        });

        btnFactorial.setText("fac");
        btnFactorial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFactorialActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 72, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtVal1)
                    .addComponent(txtVal2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BtnPlus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtnMultiply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSine, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnCosine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtnMinus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(59, 59, 59))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btnRemainder, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnFactorial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnTangent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnLn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnLog, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
                                    .addComponent(btnpower, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(12, 12, 12)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(txtVal1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtVal2, 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.BASELINE)
                    .addComponent(BtnPlus)
                    .addComponent(BtnMinus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnMultiply)
                    .addComponent(BtnDivide))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCosine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnTangent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLn)
                    .addComponent(btnpower))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRemainder)
                    .addComponent(btnFactorial))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtVal1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtVal1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtVal1ActionPerformed

    private void txtVal2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtVal2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtVal2ActionPerformed

    private void BtnPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnPlusActionPerformed
            String a=txtVal1.getText();
            int V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            int V2=Integer.parseInt(b);
            int c=obj.sum(V1, V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_BtnPlusActionPerformed

    private void BtnMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnMinusActionPerformed
       String a=txtVal1.getText();
            int V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            int V2=Integer.parseInt(b);
            int c=obj.sub(V1, V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_BtnMinusActionPerformed

    private void BtnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnMultiplyActionPerformed
       String a=txtVal1.getText();
            int V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            int V2=Integer.parseInt(b);
            int c=obj.mul(V1, V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_BtnMultiplyActionPerformed

    private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnDivideActionPerformed
       String a=txtVal1.getText();
            float V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            float V2=Integer.parseInt(b);
            float c=obj.div(V1, V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_BtnDivideActionPerformed

    private void btnTangentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTangentActionPerformed
            String a=txtVal1.getText();
            double v=Double.parseDouble(a);
            v=Math.toRadians(v);
            double c=obj.tan(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnTangentActionPerformed

    private void btnCosineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCosineActionPerformed
            String a=txtVal1.getText();
            double v=Double.parseDouble(a);
            v=Math.toRadians(v);
            double c=obj.cos(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnCosineActionPerformed

    private void btnSineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSineActionPerformed
            String a=txtVal1.getText();
            double v=Double.parseDouble(a);
            v=Math.toRadians(v);
            double c=obj.sin(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnSineActionPerformed

    private void btnLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogActionPerformed
            String a=txtVal1.getText();
            double v=Double.parseDouble(a);
            double c=obj.log(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnLogActionPerformed

    private void btnLnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLnActionPerformed
            String a=txtVal1.getText();
            double v=Double.parseDouble(a);
            double c=obj.log10(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnLnActionPerformed

    private void btnpowerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnpowerActionPerformed
            String a=txtVal1.getText();
            int V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            int V2=Integer.parseInt(b);
            double c=obj.power(V1,V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_btnpowerActionPerformed

    private void btnRemainderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemainderActionPerformed
            String a=txtVal1.getText();
            int V1=Integer.parseInt(a);
            String b=txtVal2.getText();
            int V2=Integer.parseInt(b);
            double c=obj.remainder(V1, V2);
            txtVal1.setText(""+c);
            txtVal2.setText("");
    }//GEN-LAST:event_btnRemainderActionPerformed

    private void btnFactorialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFactorialActionPerformed
            String a=txtVal1.getText();
            int v=Integer.parseInt(a);
            int c=obj.fact(v);
            txtVal1.setText(""+c);
    }//GEN-LAST:event_btnFactorialActionPerformed

    /**
     * @param args the command line arguments
     */
main.java
    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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Calculatorform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Calculatorform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Calculatorform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Calculatorform.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 Calculatorform().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnDivide;
    private javax.swing.JButton BtnMinus;
    private javax.swing.JButton BtnMultiply;
    private javax.swing.JButton BtnPlus;
    private javax.swing.JButton btnCosine;
    private javax.swing.JButton btnFactorial;
    private javax.swing.JButton btnLn;
    private javax.swing.JButton btnLog;
    private javax.swing.JButton btnRemainder;
    private javax.swing.JButton btnSine;
    private javax.swing.JButton btnTangent;
    private javax.swing.JButton btnpower;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField txtVal1;
    private javax.swing.JTextField txtVal2;
    // End of variables declaration//GEN-END:variables
}


Output of the Program

GUI Scientific Calculator in Java

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment