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

  • Online Mobile Recharge Websites
    Hey here is the list of websites which helps you to make a online recharge of your mobile. Rechargeitnow.com FastRecharge.com Indiamobilerec...
  • 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/
  • 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...
  • Windows 8 - Keyboard shortcuts
    Press this key To do this Right Shift for eight seconds Turn Filter Keys on and off Left Alt+Left Shift+PrtScn (or PrtScn) Turn High Contras...
  • 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...
  • HOW TO : Speed Up Youtube Buffering Speed
    1. Click start then type  system.ini  in the input address box 2. Then you will see the file  system.ini,  click it and it will open in a no...
  • 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 ...
  • Make Calculator Yourself using java.
    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 t...
  • How To recover Saved Password In Firefox ?
    1. Open Firefox Web Broweser 2. Then Click on FireFox > Option > Option as shown in below picture 3. Then a POP Up box will appear, In...
  • How To Check Antivirus Strength or Test Antivirus Software or Check Antivirus Which is Used by You is Good or Worst?
    One of the most asked question is  Which antivirus has good strength or How to check antivirus strength? This post is for them who want to c...

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