Sunday, February 12, 2012

IMPLEMENTATION OF SCIENTIFIC CALCULATOR USING EVENT DRIVEN PROGRAMMING [ java ]


IMPLEMENTATION OF SCIENTIFIC CALCULATOR USING EVENT DRIVEN PROGRAMMING


AIM:
            To write a java program to implement the scientific calculator using event driven    programming

ALGORITHM:

            1. Create the class scientific calculator .Define and declare its variables.
            2. Using scientific calculator constructor create buttons that are in the scientific                                  calculator.
            3. Using actionPerformed() method define the function that has to be done when the                        corresponding buttons are pressed.
            4. In the main function create the objects for the class scientific calculator and then using                 that objects set the title for the program as scientific calculator and then press the                          buttons in the calculator to get the results which you want

PROGRAM:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ScientificCalculator extends JFrame implements ActionListener
{
            JTextField tfield;
           
            double temp,temp1,result,a,ml;
            static double m1,m2;
            int k=1,x=0,y=0,z=0;
            char ch;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,zero,clr,pow2,pow3,exp,fac,plus,min,div,log,    rec,mul,eq,addsub,dot,mr,mc,mp,mm,sqrt,sin,cos,tan;
            Container cont;
            JPanel textpanel,buttonpanel;
            ScientificCalculator()
            {
                        cont=getContentPane();
                        cont.setLayout(new BorderLayout());
                JPanel textpanel=new JPanel();
                tfield=new JTextField(25);
                tfield.setHorizontalAlignment(SwingConstants.RIGHT);
                tfield.addKeyListener(new KeyAdapter()
                {
                        public void keyTyped(KeyEvent keyevent)
                        {
                                    char c=keyevent.getKeyChar();
                                    if(c>='0'&&c<='9')
                                    {
                                    }
                                    else
                                    {
                                                keyevent.consume();
                                    }
                        }
                });
            textpanel.add(tfield);
            buttonpanel=new JPanel();
            buttonpanel.setLayout(new GridLayout(8,4,2,2));
            boolean t=true;
            mr=new JButton("MR");
            buttonpanel.add(mr);
            mr.addActionListener(this);
            mc=new JButton("MC");
            buttonpanel.add(mc);
            mc.addActionListener(this);
            mp=new JButton("M+");
            buttonpanel.add(mp);
            mp.addActionListener(this);
            mm=new JButton("M-");
            buttonpanel.add(mm);
            mm.addActionListener(this);
            b1=new JButton("1");
            buttonpanel.add(b1);
            b1.addActionListener(this);
            b2=new JButton("2");
            buttonpanel.add(b2);
            b2.addActionListener(this);
            b3=new JButton("3");
            buttonpanel.add(b3);
            b3.addActionListener(this);
            b4=new JButton("4");
            buttonpanel.add(b4);
            b4.addActionListener(this);
            b5=new JButton("5");
            buttonpanel.add(b5);
            b5.addActionListener(this);
            b6=new JButton("6");
            buttonpanel.add(b6);
            b6.addActionListener(this);
            b7=new JButton("7");
            buttonpanel.add(b7);
            b7.addActionListener(this);
            b8=new JButton("8");
            buttonpanel.add(b8);
            b8.addActionListener(this);
            b9=new JButton("9");
            buttonpanel.add(b9);
            b9.addActionListener(this);
            zero=new JButton("0");
            buttonpanel.add(zero);
            zero.addActionListener(this);
            plus=new JButton("+");
            buttonpanel.add(plus);
            plus.addActionListener(this);
            min=new JButton("-");
            buttonpanel.add(min);
            min.addActionListener(this);
            mul=new JButton("*");
            buttonpanel.add(mul);
            mul.addActionListener(this);
            div=new JButton("/");
            buttonpanel.add(div);
            div.addActionListener(this);
            addsub=new JButton("+/-");
            buttonpanel.add(addsub);
            addsub.addActionListener(this);
            dot=new JButton(".");
            buttonpanel.add(dot);
            dot.addActionListener(this);
            eq=new JButton("=");
            buttonpanel.add(eq);
            eq.addActionListener(this);
            rec=new JButton("1/x");
            buttonpanel.add(rec);
            rec.addActionListener(this);
            sqrt=new JButton("Sqrt");
            buttonpanel.add(sqrt);
            sqrt.addActionListener(this);
            log=new JButton("log");
            buttonpanel.add(log);
            log.addActionListener(this);
            sin=new JButton("SIN");
            buttonpanel.add(sin);
            sin.addActionListener(this);
            cos=new JButton("COS");
            buttonpanel.add(cos);
            cos.addActionListener(this);
            tan=new JButton("TAN");
            buttonpanel.add(tan);
            tan.addActionListener(this);
            pow2=new JButton("x^2");
            buttonpanel.add(pow2);
            pow2.addActionListener(this);
            pow3=new JButton("X^3");
            buttonpanel.add(pow3);
            pow3.addActionListener(this);
            exp=new JButton("Exp");
            buttonpanel.add(exp);
            exp.addActionListener(this);
            fac=new JButton("n!");
            buttonpanel.add(fac);
            fac.addActionListener(this);
            clr=new JButton("AC");
            buttonpanel.add(clr);
            clr.addActionListener(this);
            cont.add("Center",buttonpanel);
            cont.add("North",textpanel);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            public void actionPerformed(ActionEvent e)
            {
                        String s=e.getActionCommand();
                        if(s.equals("1"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"1");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"1");
                                                z=0;
                                    }
                        }
                        if(s.equals("2"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"2");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"2");
                                                z=0;
                                    }
                        }
                        if(s.equals("3"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"3");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"3");
                                                z=0;
                                    }
                        }
                        if(s.equals("4"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"4");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"4");
                                                z=0;
                                    }
                        }
                        if(s.equals("5"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"5");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"5");
                                                z=0;
                                    }
                        }
                        if(s.equals("6"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"6");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"6");
                                                z=0;
                                    }
                        }
                        if(s.equals("7"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"7");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"7");
                                                z=0;
                                    }
                        }
                        if(s.equals("8"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"8");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"8");
                                                z=0;
                                    }
                        }
                        if(s.equals("9"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"9");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"9");
                                                z=0;
                                    }
                        }
                        if(s.equals("0"))
                        {
                                    if(z==0)
                                    {
                                                tfield.setText(tfield.getText()+"0");
                                    }
                                    else
                                    {
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+"0");
                                                z=0;
                                    }
                        }
                        if(s.equals("AC"))
                        {
                                                tfield.setText("");
                                                x=0;y=0;
                                                z=0;
                                    }
                        if(s.equals("log"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.log(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                               
                                    }
                        }
                        if(s.equals("1/x"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=1/(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("Exp"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.exp(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("x^2"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.pow(Double.parseDouble(tfield.getText()),2);
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("X^3"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.pow(Double.parseDouble(tfield.getText()),3);
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("+/-"))
                        {
                                    if(x==0)
                                    {
                                                tfield.setText("-"+tfield.getText());
                                                x=1;
                                    }
                                    else
                                    {
                                        tfield.setText(tfield.getText());      
                                    }
                        }
                        if(s.equals("."))
                        {
                                    if(y==0)
                                    {
                                                tfield.setText(tfield.getText()+".");
                                                y=1;
                                    }
                                    else
                                    {
                                        tfield.setText(tfield.getText());      
                                    }
                        }
                        if(s.equals("+"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                                temp=0;
                                                ch='+';
                                    }
                                    else
                                    {
                                        temp=Double.parseDouble(tfield.getText());
                                                tfield.setText("");
                                                ch='+';
                                                y=0;x=0;
                                    }
                                    tfield.requestFocus();
                        }
                        if(s.equals("-"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                                temp=0;
                                                ch='-';
                                    }
                                    else
                                    {
                                                            y=0;x=0;
                                        temp=Double.parseDouble(tfield.getText());
                                                tfield.setText("");
                                                ch='-';
                                    }
                                    tfield.requestFocus();
                        }
                        if(s.equals("/"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                                temp=1;
                                                ch='/';
                                    }
                                    else
                                    {
                                                            y=0;x=0;
                                        temp=Double.parseDouble(tfield.getText());
                                                ch='/';
                                                tfield.setText("");
                                    }
                                    tfield.requestFocus();
                        }
                        if(s.equals("*"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                                temp=1;
                                                ch='*';
                                    }
                                    else
                                    {
                                                            y=0;x=0;
                                        temp=Double.parseDouble(tfield.getText());
                                                ch='*';
                                                tfield.setText("");
                                    }
                                    tfield.requestFocus();
                        }
                        if(s.equals("MC"))
                        {
                                    ml=0;
                                    tfield.setText("");       
                        }
                        if(s.equals("MR"))
                        {
                           tfield.setText("");    
                           tfield.setText(tfield.getText()+ml);
                        }
                        if(s.equals("M+"))
                        {
                                    if(k==1)
                                    {
                                                ml=Double.parseDouble(tfield.getText());
                                                k++;
                                    }
                                    else
                                    {
                                                ml+=Double.parseDouble(tfield.getText());
                                                tfield.setText(""+ml);                                     
                                    }
                        }
                        if(s.equals("M-"))
                        {
                                    if(k==1)
                                    {
                                                ml=Double.parseDouble(tfield.getText());
                                                k++;
                                    }
                                    else
                                    {
                                                ml-=Double.parseDouble(tfield.getText());
                                                tfield.setText(""+ml);                                     
                                    }
                        }
                        if(s.equals("Sqrt"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.sqrt(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                               
                                    }
                        }
                        if(s.equals("SIN"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.sin(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("COS"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.cos(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("TAN"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=Math.tan(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        }
                        if(s.equals("="))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                                temp1=Double.parseDouble(tfield.getText());
                                                switch(ch)
                                                {
                                                            case '+':
                                                                        result=temp+temp1;
                                                                        break;
                                                    case '-':
                                                                        result=temp-temp1;
                                                                        break;
                                                            case '/':
                                                                        result=temp/temp1;
                                                                        break;
                                                            case '*':
                                                                        result=temp*temp1;
                                                                        break;
                                                }
                                tfield.setText("");
                                tfield.setText(tfield.getText()+result);
                                z=1;
                                    }
                        }
                        if(s.equals("n!"))
                        {
                                    if(tfield.getText().equals(""))
                                    {
                                                tfield.setText("");
                                    }
                                    else
                                    {
                                        a=fact(Double.parseDouble(tfield.getText()));
                                                tfield.setText("");
                                                tfield.setText(tfield.getText()+a);
                                    }
                        tfield.requestFocus();
                        }
            }
            double fact(double x)
            {
                        /*int er=0;
                        if(x>0)
                        {
                                    er=20;
                                    return 0;
                        }*/
                        double i,s=1;
                        for(i=2;i<=x;i+=1.0)
                           s*=i;
                           return s;
            }
    public static void main(String srgs[])
    {
            /*try
            {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
            catch(Exception e)
            {
            /*/        ScientificCalculator f;
                        f=new ScientificCalculator();
                        f.setTitle("Scientific calculator");
                        f.pack();
                        f.setVisible(true);
    /*     }   */
    }
            }






















OUTPUT:










RESULT
            Thus the java program to implement the scientific calculator using event driven      programming was done and verified.

No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin