import java.awt.*;
import java.applet.Applet;
import java.lang.*;
public class Calculator extends Applet
{
public static void main(String args[])
{
Label lop1 = new Label("Operator 1");
Label lop2 = new Label("Operator 2");
Label lres = new Label("Result");
Label lerr = new Label("Error Message");
TextField tfop1 = new TextField(10);
TextField tfop2 = new TextField(10);
TextField tfres = new TextField(10);
TextArea taerr = new TextArea(4,15);
public void init()
{
resize(200,200);
add(lop1);
tfop1.setEditable(true);
tfop1.resize(tfop1.preferredSize());
add(tfop1);
add(lop2);
tfop2.setEditable(true);
tfop2.resize(tfop2.preferredSize());
add(tfop2);
add(lres);
tfres.setEditable(false);
tfres.resize(tfres.preferredSize());
add(tfres);
add(lerr);
taeer.setEditable(false);
taeer.resize(taeer.preferredSize());
add(taeer);
show();
}
public boolean keyDown(Event evt , int key)
{
if(evt.target == tfop2)
{
char c = (char)key;
if(c == '\n')
{
execute();
return(true);
}
return(false);
}
}
public void execute()
{
int op1,op2,result;
String opstr = tfop1.getText();
try(op1 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
opstr = tfop2.getText();
try(op2 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
result = op1+op2 ;
tfres.setText(Interger.toString( result ));
}
}
}