Monday, June 24, 2013

Advance Calculator (Java Platform);

import java.awt.*;
import java.awt.event.*;
class Cal extends Frame implements ActionListener{

    Button bMc = new Button("MC");
    Button bMPlus = new Button("M+");
    Button bMMin = new Button("M-");
    Button bBkcs = new Button("<--");

    Button bCe = new Button("CE");
    Button bPlusOrEqual = new Button("+/-");
    Button bSquareRoot = new Button("v");
    Button bPresentage = new Button("%");
   
    Button b11 = new Button("7");
    Button b12 = new Button("8");
    Button b13 = new Button("9");
    Button bDivide = new Button("/");
   
    Button b16 = new Button("4");
    Button b17 = new Button("5");
    Button b18 = new Button("6");
    Button bMultiply = new Button("*");

    Button b21 = new Button("1");
    Button b22 = new Button("2");
    Button b23 = new Button("3");
    Button bMinus = new Button("-");

    Button bPlus = new Button("+");
    Button b26 = new Button("0");
    Button bPoint = new Button(".");
    Button bEquals = new Button("=");

    TextField t = new TextField(25);
   
    String fun1,fun2,fun3,fun4,fun5;
Cal(){
   
    Panel p1 = new Panel();
    Panel p2 = new Panel();
    setSize(25,100);
    add(p1,BorderLayout.NORTH);
    add(p2,BorderLayout.CENTER);

    GridLayout g = new GridLayout(6,4);

    bMc.addActionListener(this);
    bMPlus.addActionListener(this);
    bMMin.addActionListener(this);

    bBkcs.addActionListener(this);
    bCe.addActionListener(this);
    bPlusOrEqual.addActionListener(this);
    bSquareRoot.addActionListener(this);

    b11.addActionListener(this);
    b12.addActionListener(this);
    b13.addActionListener(this);
    bDivide.addActionListener(this);
    bPresentage.addActionListener(this);

    b16.addActionListener(this);
    b17.addActionListener(this);
    b18.addActionListener(this);
    bMultiply.addActionListener(this);

    b21.addActionListener(this);
    b22.addActionListener(this);
    b23.addActionListener(this);
    bMinus.addActionListener(this);
    bPlus.addActionListener(this);

    b26.addActionListener(this);
    bPoint.addActionListener(this);
    bEquals.addActionListener(this);

    p2.setLayout(g);
    p1.add(t);

    p2.add(bMc);
    p2.add(bMPlus);
    p2.add(bMMin);

    p2.add(bBkcs);
    p2.add(bCe);
    p2.add(bPlusOrEqual);
    p2.add(bSquareRoot);
    p2.add(bPresentage);

    p2.add(b11);
    p2.add(b12);
    p2.add(b13);
    p2.add(bDivide);

    p2.add(b16);
    p2.add(b17);
    p2.add(b18);
    p2.add(bMultiply);

    p2.add(b21);
    p2.add(b22);
    p2.add(b23);
    p2.add(bMinus);
    p2.add(bPlus);

    p2.add(b26);
    p2.add(bPoint);
    p2.add(bEquals);
   
    setSize(300,400);
    setBackground(Color.green);
}

    public void actionPerformed(ActionEvent e){
    if(e.getSource().equals(b11)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b12)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b13)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b16)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b17)){         t.setText(t.getText() + e.getActionCommand());
    
    }else if (e.getSource().equals(b18)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b21)){
    t.setText(t.getText() + e.getActionCommand());
    
    }else if (e.getSource().equals(b22)){
    t.setText(t.getText() + e.getActionCommand());

     }else if (e.getSource().equals(b23)){
    t.setText(t.getText() + e.getActionCommand());
    
    }else if (e.getSource().equals(bPoint)){
    t.setText(t.getText() + e.getActionCommand());

    }else if (e.getSource().equals(b26)){
    t.setText(t.getText() + e.getActionCommand());
   
    }else if (bPlus.equals(e.getSource())){
        fun1 = t.getText();
        t.setText(null);

    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun1) +     Integer.parseInt(t.getText()));
        t.setText(Val);

    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun1) +     Integer.parseInt(t.getText()));
        t.setText(Val);


    }else if (bCe.equals(e.getSource())){
        fun2 = t.getText();
        t.setText(null);

    }else if(bCe.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun2) +     Integer.parseInt(t.getText()));
        t.setText(Val);
   
    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun2) +     Integer.parseInt(t.getText()));
        t.setText(Val);


   
    }else if (bMinus.equals(e.getSource())){
        fun3 = t.getText();
        t.setText(null);

    }else if(bCe.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun3) -     Integer.parseInt(t.getText()));
        t.setText(Val);
   
    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun3) -     Integer.parseInt(t.getText()));
        t.setText(Val);



    }else if (bMultiply.equals(e.getSource())){
        fun4 = t.getText();
        t.setText(null);

    }else if(bCe.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun4) *             Integer.parseInt(t.getText()));
        t.setText(Val);
   
    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun4) *             Integer.parseInt(t.getText()));
        t.setText(Val);



    }else if (bDivide.equals(e.getSource())){
        fun5 = t.getText();
        t.setText(null);

    }else if(bCe.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun5) /             Integer.parseInt(t.getText()));
        t.setText(Val);
   
    }else if(bEquals.equals(e.getSource())){
    String Val = String.valueOf(Integer.parseInt(fun5) /             Integer.parseInt(t.getText()));
        t.setText(Val);


    }
}               
   
    public static void main(String args[]){
        Cal c = new Cal();
        c.setVisible(true);
   
    }
}

No comments:

Post a Comment