Sunday, 18 February 2018

calculator using java awt

https://www.dropbox.com/s/uxe35t2kua9huxq/Calci.jar?dl=0

import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Calci extends Frame implements ActionListener {
TextArea ta;
Button b1,b2,b3,b0,b4,b5,b6,b7,b8,b9,ba,bs,bm,bi,bo,bb,be,bp,bc,bn,bq;
Calci(){
    ab.add("b1");ab.add("b2");ab.add("b3");ab.add("b4");ab.add("b5");ab.add("b6");
    ab.add("b7");ab.add("b8");ab.add("b9");ab.add("b0");ab.add("ba");ab.add("bs");
    ab.add("bm");ab.add("bi");
    ta=new TextArea("",3,10,TextArea.SCROLLBARS_NONE);
    b1=new Button("1");ba=new Button("+");bq=new Button("^");
    b2=new Button("2");bs=new Button("-");
    b3=new Button("3");bm=new Button("x");
    b4=new Button("4");bi=new Button("/");
    b5=new Button("5");bo=new Button("%");
    b6=new Button("6");bb=new Button("<-");
    b7=new Button("7");be=new Button("=");
    b8=new Button("8");bp=new Button(".");
    b9=new Button("9");bc=new Button("C");
    b0=new Button("0");bn=new Button("~");
    b1.setBackground(Color.CYAN);b2.setBackground(Color.CYAN);b3.setBackground(Color.CYAN);
    b4.setBackground(Color.CYAN);b5.setBackground(Color.CYAN);b6.setBackground(Color.CYAN);
    b7.setBackground(Color.CYAN);b8.setBackground(Color.CYAN);b9.setBackground(Color.CYAN);
    ta.setBounds(20,30,200,50);b1.setBounds(20,120,40,20);
    b7.setBounds(20,80,40,20);b2.setBounds(60,120,40,20);
    b8.setBounds(60,80,40,20);b3.setBounds(100,120,40,20);
    b9.setBounds(100,80,40,20);bb.setBounds(20,140,40,20);
    b4.setBounds(20,100,40,20);b0.setBounds(60,140,40,20);
    b5.setBounds(60,100,40,20);bp.setBounds(100,140,40,20);
    b6.setBounds(100,100,40,20);bi.setBounds(140,80,40,20);
    ba.setBounds(140,140,40,20);bn.setBounds(20,160,40,20);
    bc.setBounds(60,160,40,20);bq.setBounds(100,160,40,20);
    be.setBounds(140,160,40,20);
    b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);
     b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);
      b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);
       b0.addActionListener(this);ba.addActionListener(this);bs.addActionListener(this);
        be.addActionListener(this);bi.addActionListener(this);bm.addActionListener(this);
        bb.addActionListener(this);bn.addActionListener(this);bq.addActionListener(this);
        bc.addActionListener(this);bo.addActionListener(this);bp.addActionListener(this);
    add(ta);bm.setBounds(140,100,40,20);bs.setBounds(140,120,40,20);
    add(b7);add(b4);add(b1);add(bb);add(ba);add(bi);add(bn);
    add(b8);add(b5);add(b2);add(b0);add(bs);add(bq);add(be);
    add(b9);add(b6);add(b3);add(bp);add(bm);add(bc);
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {dispose();
        }
    });
    ta.setText("");
    setLayout(null);
}
public void actionPerformed(ActionEvent e){
    if(bb==e.getSource()){
ta.setText(ta.getText().substring(0,ta.getText().length()-1));}
    else if(bc==e.getSource()){
        ta.setText("");}
    else if(bn==e.getSource())
    {
        Double d=Double.parseDouble(ta.getText());
        if(ta.getText().charAt(0)=='-')
        {
            ta.setText(ta.getText().substring(1));
        }
        else if(d!=0.0)
        {
            ta.setText("-".concat(ta.getText()));
        }
    }
    else if(bq==e.getSource())
    {
        Double d=Double.parseDouble(ta.getText());
         if(d!=0.0)
        {
            d=d*d;
            ta.setText(String.valueOf(d));
        }

    }

    else if(be==e.getSource())
    {
        try{

        ta.setText(exp(ta.getText().replace('x','*')));

        }
        catch(Exception ext)
        {

        }

    }  else {

    ta.append(e.getActionCommand());

    }
}
public String  exp(String foo) throws Exception
{
    ScriptEngineManager s=new ScriptEngineManager();
    ScriptEngine se=s.getEngineByName("JavaScript");
    return String.valueOf(se.eval(foo));
}
    public static void main(String[] args) {
        Calci c=new Calci();
        c.setSize(200,300);
        c.setVisible(true);
    }
}

No comments:

Post a Comment

CODING FPGROWTH IN PYTHON FROM SCRATCH

dats=[['google','amazon',],['amazon','google','python','cse'],['cse','google...