Which Anti Virus

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 31 August 2013

Make Calculator Yourself using java.

Posted on 20:19 by Unknown
write a following code in netbeans IDE 7.1 and save file as calc.java and run it and you will have your own calculator... 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculator;

/**
 *
 * @author NISH
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Calculator extends JFrame implements ActionListener{
    JTextField txt;
    JButton btn[]=new JButton[9];
    JPanel jp1,jp2,jp3;
    JButton plus,minus,mul,div,zero,dot,equal,cncl,square,sqrt,fact;
    String str;
    Boolean flag=false,flag_eq=false,flag_op=false,flag_a=false,flag_b=false;
        Character opr=null;
        String first=null,second=null;
        Double a,b;
        Double ans=0.0;
        Integer x,y,z=0;
   public Calculator()
   {
       this.setLayout(new FlowLayout());
       txt=new JTextField("",19);
       txt.setBounds(0, 0, 200, 20);
     
       jp1=new JPanel();
       jp2=new JPanel(new GridLayout(5, 4, 5, 5));
     
       jp1.add(txt);
       plus=new JButton("" +  "+" + "");
       minus=new JButton("" +  "-" + "");
       mul=new JButton("" +  "*" + "");
       div=new JButton("" +  "/" + "");
       zero=new JButton("" +  "0" + "");
       dot=new JButton("" +  "." + "");
       equal=new JButton("" +  "=" + "");
       cncl=new JButton("" +  "C" + "");
       square=new JButton("" +  "^" + "");
       sqrt=new JButton("" +  "VX" + "");
       fact=new JButton("" +  "X!" + "");
       for(int i=0;i<13;i++)
       {
           if(i==3)
           {
               jp2.add(plus);
           }
           if(i==6)
           {
               jp2.add(minus);
           }
           if(i==9)
           {
               jp2.add(mul);
           }
         
           if(i<9)
           {
                btn[i]=new JButton(" " + (i +1) +" ");
                jp2.add(btn[i]);
           }
       }
       jp2.add(zero);
       jp2.add(dot);
        jp2.add(div);
        jp2.add(cncl);
        jp2.add(square);
       jp2.add(sqrt);
        jp2.add(fact);
       jp2.add(equal);
     
       add(jp1);
       add(jp2);
     
       for(int i=0;i<9;i++)
       {
           btn[i].addActionListener(this);
       }
       plus.addActionListener(this);
       minus.addActionListener(this);
       mul.addActionListener(this);
       div.addActionListener(this);
       zero.addActionListener(this);
       dot.addActionListener(this);
       equal.addActionListener(this);
       cncl.addActionListener(this);
       square.addActionListener(this);
       sqrt.addActionListener(this);
       fact.addActionListener(this);
     
   }
   public static void main(String args[])
   {
       Calculator cl=new Calculator();
       cl.setTitle("Calculator");
       cl.setSize(240, 230);
       cl.setVisible(true);
       cl.setResizable(false);
   }

    @Override
    public void actionPerformed(ActionEvent e) {
       
        for(int i=0;i<9;i++)
        {
            if(e.getSource().equals(btn[i]))
            {
                if(flag_eq==true)
                {
                    txt.setText(null);
                    flag_eq=false;
                }
                if(flag_op==true)
                {
                    txt.setText(null);
                    flag_op=false;
                }
                if(txt.getText()!=null)
                {
                    str=txt.getText();
                    txt.setText(str + String.valueOf((i+1)));
                }
                else
                {
                    txt.setText(String.valueOf((i+1)));
                }
            }
        }
     
        if(e.getSource().equals(plus))
        {
            first=txt.getText().toString();
            opr='+';
            flag_op=true;
            //txt.setText("");
        }
        if(e.getSource().equals(minus))
        {
            first=txt.getText().toString();
            opr='-';
            flag_op=true;
           
            //txt.setText("");
        }
        if(e.getSource().equals(mul))
        {
            first=txt.getText().toString();
            opr='*';
            flag_op=true;
            //txt.setText("");
        }
        if(e.getSource().equals(div))
        {
            first=txt.getText().toString();
            opr='/';
            flag_op=true;
            //txt.setText("");
        }
        if(e.getSource().equals(zero))
        {
                if(txt.getText()!=null)
                {
                    str=txt.getText();
                    txt.setText(str + "0");
                }
                else
                {
                    txt.setText("0");
                }
        }
        if(e.getSource().equals(cncl))
        {
            txt.setText("");
        }
        if(e.getSource().equals(square))
        {
           first=txt.getText().toString();
           opr='^';
           flag_op=true;
           //txt.setText("");
        }
        if(e.getSource().equals(fact))
        {
           long l=1;
         
           for(int i=Integer.parseInt(txt.getText().toString());i>=1;i--)
           {
               l=l*i;
           }
           txt.setText(String.valueOf(l));
        }
        if(e.getSource().equals(sqrt))
        {
           txt.setText(String.valueOf(Math.sqrt(Double.parseDouble(txt.getText().toString()))));
        }
        if(e.getSource().equals(dot))
        {
            if(txt.getText()!=null)
                txt.setText(txt.getText().toString() + ".");
            else
                txt.setText(".");
        }
        if(e.getActionCommand().equals("="))
           
        {
             
                   a=Double.parseDouble(first);
                   b=Double.parseDouble(txt.getText().toString());
                 
             if(opr=='/')
             {
                if(first.indexOf(".")!=-1 && txt.getText().indexOf(".")!=-1)
                {
                      txt.setText(String.valueOf((Double.parseDouble(first) / Double.parseDouble(txt.getText().toString()))));
                }
             
                else
                {
                    txt.setText(String.valueOf(Float.parseFloat(first) / Float.parseFloat(txt.getText().toString())));
                }
             }
             else
              {
             switch(opr)
             {
                case '+':
                   
                        ans=a+b;
                       
                   
                    break;
                case '*':
                   
                        ans=a*b;
                     
                   
                    break;
                case '-':
                   
                        ans=a-b;
                       
                   
                    break;
                 case '^':
                 
                    ans=Math.pow(a, b);
                   
                    break;
               
            }
                String st= String.valueOf(ans);
                st=st+0;
                int indx=st.indexOf(".");
                if( st.charAt(indx+1)=='0' && st.charAt(indx+2) =='0')
                {
                    String st1=st.substring(0, indx);
                    txt.setText(st1);
                }
                else
                {
                    txt.setText(String.valueOf(ans));
                }
               
               
                flag_eq=true;
               
            }  
         
        }
    }
}


OUTPUT:-



Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in java | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • GTU Paper Solution and Material as Per GTU syllabus
    Go to this website and download the Study material of GTU Syllabus. http://gtu-paper.blogspot.com/
  • How to read the barcodes???
    Trick to read the barcodes The first 3 digits of the barcode is the country code wherein the product was made. CODE COUNTRY 00-13 USA & ...
  • Different Types Of Computer Ports
  • Features Of Android KitKat
    1) The new Phone (Dialer) app is now linked online with Google Maps. This means if your type Big Bazaar in Phone app (Not Search) and if tha...
  • C Program Library files(Header Files)
    1. <stdio.h>: input and output  function in program. 2. <conio.h>: to clear screen and  pause information function. 3. <ctype...
  • Windiows 8 RUN Commands
    Useful windows RUN Commands 1. Accessibility Controls – access.cpl 2. Accessibility Wizard – accwiz 3. Add Hardware Wizard – hdwwiz.cpl 4. A...
  • Free Download E-DRAW MAX
    link 1 : http://hotfile.com/dl/ 254163134/8de2252/EDM.rar.html
  • How to make a bootable pendrive using command prompt!!
    1. Type “diskpart” inside the command  prompt and hit “ENTER”. 2. After last step type “list disk” inside the  command prompt and hit “ENTER...
  • How To Install Android on PC or Laptop
    Android has a got very important place in our Tech World.Now in market you can get many devices running on Android like Smartphones to Table...
  • Android Secrete codes
    1. Phone Information, Usage and Battery – *#*#4636#*#* 2. IMEI Number – *#06# 3. Enter Service Menu On Newer Phones – *#0*# 4. Detailed Came...

Categories

  • Android
  • Android Apps
  • apps
  • BlackBerry
  • Cprog
  • dfd
  • erd
  • Facebook
  • general
  • Hacking
  • HTC
  • ios
  • java
  • Mobile
  • nokia
  • Samsung
  • Srs
  • Window Apps
  • Windows 8

Blog Archive

  • ▼  2013 (91)
    • ►  November (6)
    • ►  October (5)
    • ►  September (17)
    • ▼  August (3)
      • Make Calculator Yourself using java.
      • COMMON ERROR OF PC AND THEIR SOLUTION
      • WiFi DIRECT in Andoird
    • ►  July (8)
    • ►  June (13)
    • ►  May (12)
    • ►  April (27)
Powered by Blogger.

About Me

Unknown
View my complete profile