Лекция: Элементарный калькулятор
import java.awt.*;
import java.applet.*;
public class calc extends Applet
{
int op1,op2,a,s;
TextField tx1;
Button but1;
Button but2;
Button but3;
Button but4;
Button but5;
Button but6;
Button but7;
Button but8;
Button but9;
Button but10;
Button but11;
Button but12;
Button but13;
Button but14;
Button but15;
Button but16;
public void init()
{
tx1=new TextField(10);
add(tx1);
but1=new Button(«1»);
add(but1);
but2=new Button(«2»);
add(but2);
but3=new Button(«3»);
add(but3);
but4=new Button("/");
add(but4);
but5=new Button(«4»);
add(but5);
but6=new Button(«5»);
add(but6);
but7=new Button(«6»);
add(but7);
but8=new Button("*");
add(but8);
but9=new Button(«7»);
add(but9);
but10=new Button(«8»);
add(but10);
but11=new Button(«9»);
add(but11);
but12=new Button("-");
add(but12);
but13=new Button(".");
add(but13);
but14=new Button(«0»);
add(but14);
but15=new Button("=");
add(but15);
but16=new Button("+");
add(but16);
}
public boolean action(Event evt, Object arg)
{
if (!(evt.target instanceof Button))
{
return false;
}
if (arg==«1»)
{
tx1.setText(tx1.getText()+«1»);
}
if (arg==«2»)
{
tx1.setText(tx1.getText()+«2»);
}
if (arg==«3»)
{
tx1.setText(tx1.getText()+«3»);
}
if (arg==«4»)
{
tx1.setText(tx1.getText()+«4»);
}
if (arg==«5»)
{
tx1.setText(tx1.getText()+«5»);
}if (arg==«6»)
{
tx1.setText(tx1.getText()+«6»);
}
if (arg==«7»)
{
tx1.setText(tx1.getText()+«7»);
}
if (arg==«8»)
{
tx1.setText(tx1.getText()+«89»);
if (arg==«9»)
{
tx1.setText(tx1.getText()+«9»);
}
if (arg==«0»)
{
tx1.setText(tx1.getText()+«0»);
}
if (arg=="+")
{
a=1;
string str1=tx1.getText();
op1=Integer.parseInt(str1);
tx1.setText("");
}
if (arg=="-")
{
a=2;
string str1=tx1.getText();
op1=Integer.parseInt(str1);
tx1.setText("");
}
if (arg=="*")
{
a=3;
string str1=tx1.getText();
op1=Integer.parseInt(str1);
tx1.setText("");
}
if (arg=="/")
{
a=4;
string str1=tx1.getText();
op1=Integer.parseInt(str1);
tx1.setText("");
}
if (arg=="=")
{
string str2=tx1.getText();
op2=Integer.parseInt(str2);
if (a==1) { s=op1+op2; };
if (a==2) { s=op1-op2; };
if (a==3) { s=op1*op2; };
if (a==4) { s=op1/op2; };
tx1.setText(String.valueOf(s));
}
if (arg==«9»)
{
tx1.setText(tx1.getText()+«9»);
}
repaint();
return true;
}
}
}