Wednesday, December 8, 2010

Calculator Design


import java.util.Random;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/*

<applet code=MyApplet width = 400 height= 200>

</applet>

*/

public class MyApplet extends Applet implements ActionListener,Runnable {

    Random rand;
    Button bt[];
    Panel pnl1;
    TextField ipTextField;
    String fnum,snum;
    boolean aflag,sflag,mflag,dflag,dotflag,sinflag,cosflag,tanflag;

    public void run(){
    }

    public void init(){
        setBackground(Color.white);
        rand = new Random();
        fnum = "0";
        snum = "0";
        int nb=25;
        bt = new Button[28];

        pnl1 = new Panel();
        ipTextField = new TextField(10);

        setLayout(new BorderLayout());

        pnl1.setLayout(new GridLayout(7,4));

        for(int i=0;i<=9;i++)
            bt[i] = new Button( new Integer(i).toString());

        bt[10] = new Button("+");
        bt[11] = new Button("-");
        bt[12] = new Button("X");
        bt[13] = new Button("/");
        bt[14] = new Button("=");
        bt[15] = new Button(".");
        bt[16] = new Button("AC");
        bt[17] = new Button("sin");
        bt[18] = new Button("cos");
        bt[19] = new Button("tan");
        bt[20] = new Button("Rand");
        bt[21] = new Button("Backspace");
        bt[22] = new Button("Sqrt");
        bt[23] = new Button("X^2");
        bt[24] = new Button("X!");
        bt[25] = new Button("1/X");
        bt[26] = new Button("PI");
        bt[27] = new Button("X^3");

        for(int i=0;i<28;i++) {
            pnl1.add(bt[i]);
            bt[i].addActionListener(this);
            }

        add(ipTextField,BorderLayout.NORTH);
        add(pnl1,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent ae) {
    String msg;
    msg = ae.getActionCommand();

    String tmsg = ipTextField.getText();

    if(msg.equals("0"))
        tmsg = tmsg + "0";
    else if(msg.equals("1"))
        tmsg = tmsg + "1";
    else if(msg.equals("2"))
        tmsg = tmsg + "2";
    else if(msg.equals("3"))
        tmsg = tmsg + "3";
    else if(msg.equals("4"))
        tmsg = tmsg + "4";
    else if(msg.equals("5"))
        tmsg = tmsg + "5";
    else if(msg.equals("6"))
        tmsg = tmsg + "6";
    else if(msg.equals("7"))
        tmsg = tmsg + "7";
    else if(msg.equals("8"))
        tmsg = tmsg + "8";
    else if(msg.equals("9"))
         tmsg = tmsg + "9";
    else if(msg.equals("AC")) { // All clear button.............
         fnum = "";
         snum = "";
         tmsg = "";

         aflag = false;
         sflag = false;
         mflag = false;
         dflag = false;
         dotflag = false;
    }
    else if(msg.equals("Backspace")) {
        fnum = ipTextField.getText();

        if(fnum == null)
            tmsg = "";
        else
            tmsg = fnum.substring(0,fnum.length()-1);
    }
    else if(msg.equals("+")) {
        fnum = ipTextField.getText();

        tmsg = "";
        aflag = true;
        dotflag = false;
    }
    else if(msg.equals("-")) {
        fnum = ipTextField.getText();
        tmsg = "";
        sflag = true;
        dotflag = false;
    }
    else if(msg.equals("X"))
{

fnum = ipTextField.getText();

tmsg = "";

mflag = true;

dotflag = false;

}

else if(msg.equals("/"))
{

fnum = ipTextField.getText();

tmsg = "";

dflag = true;

dotflag = false;

}

else if(msg.equals("sin"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.sin(dval)+"";

}

else if(msg.equals("cos"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.cos(dval)+"";

}

else if(msg.equals("tan"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.tan(dval)+"";

}

else if(msg.equals("X^2"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = (dval*dval)+"";

}

else if(msg.equals("X^3"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = (dval*dval*dval)+"";

}

else if(msg.equals("Sqrt"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.sqrt(dval)+"";

}

else if(msg.equals("X!"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();




long li=1;

for(int i = 1; i<=(int)dval;i++)

li *= i;

tmsg = li+"";




}

else if(msg.equals(".")&&(dotflag==false))

{

tmsg = tmsg + ".";

dotflag = true;

}

else if(msg.equals("PI"))

{

tmsg = Math.PI+"";

dotflag = true;

}

else if(msg.equals("Rand"))

{

tmsg = rand.nextFloat()+"";

dotflag = true;

}

else if(msg.equals("1/X"))

{

fnum = ipTextField.getText();

double d1;

Double do1;

do1 = Double.valueOf(fnum);

d1 = do1.doubleValue();



if(d1==0.0)

{

try
{

ipTextField.setText("Divide by Zero Attempted.");

Thread.sleep(1000);
}

catch(InterruptedException ie)
{
}


tmsg = "";

}

else

{

tmsg =( (1/d1) + "" );

dotflag = true;

}

}

else if(msg.equals("="))
{

snum = ipTextField.getText();

if(snum == null) snum = "0";

if(fnum == null) fnum = "0";



double n1,n2;

Double do1,do2;

do1 = Double.valueOf(fnum);

do2 = Double.valueOf(snum);


n1 = do1.doubleValue();

n2 = do2.doubleValue();



if(aflag==true)

{

tmsg = (n1+n2+"");

}


if(sflag==true)

{

tmsg = (n1-n2+"");

}


if(mflag==true)

{

tmsg = (n1*n2+"");

}

if(dflag==true)


{

tmsg = (n1/n2+"");

}

aflag = false;

sflag = false;

mflag = false;

dflag = false;

dotflag = true;

}

ipTextField.setText(tmsg);

}

public void paint(Graphics g)

{

}

}