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

  • 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...
  • Free Download E-DRAW MAX
    link 1 : http://hotfile.com/dl/ 254163134/8de2252/EDM.rar.html
  • Different Operating System and Their RAM Support
    Windows 8 64 bit Enterprise  Professional : 512 GB Windows 8 64 bit :128 GB Windows 8 32 bit : 4 GB Windows 7 64 bit Ultimate, Enterprise ...
  • How To Protect Your Computer From Keyloggers
    A keylogger is a small program that stores each keystroke a user types on a specific computer's keyboard. It is capable to send log file...
  • 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 Hackers Erase Tracks After Hacking
    Whenever someone comes in contact with another person, place, or thing, something of that person is left behind. This means that the attacke...
  • Shutdown Your Friend’s PC While Chatting
    Shutdown Your Friend’s PC While Chatting   Hey, Here this the trick for shutdown your friend’s PC while you are chat with friend.Here is som...
  • Steps To Find Manufacturing Date Of Your Computer
    WANT TO KNOW THE MANUFACTURING DATE OF YOUR COMPUTER?? Follow the Steps: 1) Open run and type debug & hit OK 2) In command prompt type D...

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