Calculator design

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class calculator extends Applet implements ActionListener{

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23;

TextField t1;

Double a,b;

int ch;

public void init()

{      setLayout(null);

b1=new Button(“<-”);

b1.setBounds(30,70,70,30);

add(b1);

b1.addActionListener(this);

/*b2=new Button(“CE”);

b2.setBounds(70,70,30,30);

add(b2);

b2.addActionListener(this);*/

b3=new Button(“C”);

b3.setBounds(110,70,70,30);

add(b3);

b3.addActionListener(this);

/*

b4=new Button(“+-”);

b4.setBounds(150,70,30,30);

add(b4);

b4.addActionListener(this);

*/

b5=new Button(“sqrt”);

b5.setBounds(190,70,30,70);

add(b5);

b5.addActionListener(this);

b6=new Button(“7″);

b6.setBounds(30,110,30,30);

add(b6);

b6.addActionListener(this);

b7=new Button(“8″);

b7.setBounds(70,110,30,30);

add(b7);

b7.addActionListener(this);

b8=new Button(“9″);

b8.setBounds(110,110,30,30);

add(b8);

b8.addActionListener(this);

b9=new Button(“/”);

b9.setBounds(150,110,30,30);

add(b9);

b9.addActionListener(this);

/*b10=new Button(“%”);

b10.setBounds(190,110,30,30);

add(b10);*/

b11=new Button(“4″);

b11.setBounds(30,150,30,30);

add(b11);

b11.addActionListener(this);

b12=new Button(“5″);

b12.setBounds(70,150,30,30);

add(b12);

b12.addActionListener(this);

b13=new Button(“6″);

b13.setBounds(110,150,30,30);

add(b13);

b13.addActionListener(this);

b14=new Button(“*”);

b14.setBounds(150,150,30,30);

add(b14);

b14.addActionListener(this);

b15=new Button(“1/x”);

b15.setBounds(190,150,30,30);

add(b15);

b15.addActionListener(this);

b16=new Button(“1″);

b16.setBounds(30,190,30,30);

add(b16);

b16.addActionListener(this);

b17=new Button(“2″);

b17.setBounds(70,190,30,30);

add(b17);

b17.addActionListener(this);

b18=new Button(“3″);

b18.setBounds(110,190,30,30);

add(b18);

b18.addActionListener(this);

b19=new Button(“-”);

b19.setBounds(150,190,30,30);

add(b19);

b19.addActionListener(this);

b20=new Button(“=”);

b20.setBounds(190,190,30,70);

add(b20);

b20.addActionListener(this);

b21=new Button(“0″);

b21.setBounds(30,230,70,30);

add(b21);

b21.addActionListener(this);

b22=new Button(“.”);

b22.setBounds(110,230,30,30);

add(b22);

b22.addActionListener(this);

b23=new Button(“+”);

b23.setBounds(150,230,30,30);

add(b23);

b23.addActionListener(this);

t1=new TextField();

t1.setBounds(30,20,190,30);

add(t1);

}

public void paint (Graphics g)

{

g.drawRect(20, 10, 210,260 );

}

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

if(arg0.getSource()==b1)

{

if(t1.getText().length()>1)

{

t1.setText(t1.getText().substring(0, t1.getText().length()-1));

}

}

if(arg0.getSource()==b3)

{

t1.setText(null);

}

if(arg0.getSource()==b5)

{

a=Double.parseDouble(t1.getText());

b=Math.sqrt(a);

t1.setText(String.valueOf(b));

}

if(arg0.getSource()==b6)

{

t1.setText(t1.getText()+String.valueOf(b6.getLabel()));

}

if(arg0.getSource()==b7)

{

t1.setText(t1.getText()+String.valueOf(b7.getLabel()));

}

if(arg0.getSource()==b8)

{

t1.setText(t1.getText()+String.valueOf(b8.getLabel()));

}

if(arg0.getSource()==b9)

{

a=Double.parseDouble(t1.getText());

t1.setText(“”);

ch=1;

}

if(arg0.getSource()==b11)

{

t1.setText(t1.getText()+String.valueOf(b11.getLabel()));

}

if(arg0.getSource()==b12)

{

t1.setText(t1.getText()+String.valueOf(b12.getLabel()));

}

if(arg0.getSource()==b13)

{

t1.setText(t1.getText()+String.valueOf(b13.getLabel()));

}

if(arg0.getSource()==b14)

{

a=Double.parseDouble(t1.getText());

t1.setText(“”);

ch=2;

}

if(arg0.getSource()==b15)

{

a=Double.parseDouble(t1.getText());

t1.setText(String.valueOf(1/a));

}

if(arg0.getSource()==b16)

{

t1.setText(t1.getText()+String.valueOf(b16.getLabel()));

}

if(arg0.getSource()==b17)

{

t1.setText(t1.getText()+String.valueOf(b17.getLabel()));

}

if(arg0.getSource()==b18)

{

t1.setText(t1.getText()+String.valueOf(b18.getLabel()));

}

if(arg0.getSource()==b19)

{

a=Double.parseDouble(t1.getText());

t1.setText(“”);

ch=4;

}

if(arg0.getSource()==b21)

{

t1.setText(t1.getText()+String.valueOf(b21.getLabel()));

}

if(arg0.getSource()==b22)

{      if(!t1.getText().contains(“.”))

{

t1.setText(t1.getText()+String.valueOf(b22.getLabel()));

a=Double.parseDouble(t1.getText());

}

}

if(arg0.getSource()==b23)

{

a=Double.parseDouble(t1.getText());

t1.setText(“”);

ch=3;

}

if(arg0.getSource()==b20)

{

if (ch==1)

{

t1.setText(String.valueOf(a/Double.parseDouble(t1.getText())));

}

else if(ch==2)

{

t1.setText(String.valueOf(a*Double.parseDouble(t1.getText())));

}

else if(ch==3)

{

t1.setText(String.valueOf(a+Double.parseDouble(t1.getText())));

}

else if(ch==4)

{

t1.setText(String.valueOf(a-Double.parseDouble(t1.getText())));

}

}

}

}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s