import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code =”Calculator” width=250 height=350>

</applet>*/

public class Calculator extends Applet implements ActionListener

{

Button keysArray[];

Panel p1,p,keyPad;

TextField ledField;

double result;

boolean first;

boolean foundKey;

static boolean clearText;

int prevOperator;

Label l1;

public void init()

{

setBackground(Color.white);

setForeground(Color.red);

Font f=new Font(“Tahoma”,Font.BOLD,14);

setFont(f);

p=new Panel();

l1=new Label(“CALCULATOR”);

add(l1);

p1=new Panel();

ledField=new TextField(“0”,20);

p1.add(ledField);

keyPad=new Panel();

keysArray=new Button[17];

result=0.0;

prevOperator=0;

first=true;

clearText=true;

for(int i=0;i<=9;i++)

keysArray[i]=new Button(String.valueOf(i));

keysArray[10]=new Button(“/”);

keysArray[11]=new Button(“*”);

keysArray[12]=new Button(“-“);

keysArray[13]=new Button(“+”);

keysArray[14]=new Button(“=”);

keysArray[15]=new Button(“.”);

keysArray[16]=new Button(“CLR”);

keyPad.setLayout(new GridLayout(4,4));

for(int i=0;i<=16;i++)

{

keyPad.add(keysArray[i]);

keysArray[i].addActionListener(this);

}

add(l1,BorderLayout.NORTH);

add(ledField,BorderLayout.CENTER);

add(keyPad,BorderLayout.SOUTH);

add(keysArray[16],BorderLayout.EAST);

}

public void actionPerformed(ActionEvent e)

{

foundKey=false;

for(int i=0;i<keysArray.length && !foundKey;i++)

if(e.getSource()==keysArray[i])

{

foundKey=true;

switch(i)

{

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case 15:

if(clearText)

{

ledField.setText(” “);

clearText=false;

} ledField.setText(ledField.getText()+keysArray[i].getLabel());

break;

case 10:

case 11:

case 12:

case 13:

case 14:

clearText=true;

if(first)

{

if(ledField.getText().length()==0)

result=0.0;

else result=Double.valueOf(ledField.getText()).doubleValue();

first=false;

prevOperator=i;

}

else

{

switch(prevOperator)

{

case 10:

result/=Double.valueOf(ledField.getText()).doubleValue();

break;

case 11:

result*=Double.valueOf(ledField.getText()).doubleValue();

break;

case 12:

result-=Double.valueOf(ledField.getText()).doubleValue();

break;

case 13:

result+=Double.valueOf(ledField.getText()).doubleValue();

break;

}

ledField.setText(Double.toString(result));

if(i==14)

first=true;

else

prevOperator=i;

}

break;

case 16:

clearText=true;

first=true;

ledField.setText(” “);

result=0.0;

prevOperator=0;

break;

}

}

}

}

Advertisements