import java.awt.*; import java.applet.*; import java.awt.event.*; import java.applet.Applet; import java.util.*; class Kalk extends Applet implements ActionListener { TextField tf1=new TextField(""); Button b11=new Button("0"); Button b12=new Button("1"); Button b13=new Button("2"); Button b14=new Button("3"); Button b15=new Button("4"); Button b21=new Button("5"); Button b22=new Button("6"); Button b23=new Button("7"); Button b24=new Button("8"); Button b25=new Button("9"); Button b31=new Button("+"); Button b32=new Button("-"); Button b33=new Button("*"); Button b34=new Button("/"); Button b35=new Button("="); String tehe=""; int esimeneArv, teineArv; int vastus; double jvastus; char op; Kalk(){ Panel p1=new Panel(new GridLayout(3,5)); /* for(int i=11;i<36;i++){ p1.add(b + i); } */ p1.add(b11); p1.add(b12); p1.add(b13); p1.add(b14); p1.add(b15); p1.add(b21); p1.add(b22); p1.add(b23); p1.add(b24); p1.add(b25); p1.add(b31); p1.add(b32); p1.add(b33); p1.add(b34); p1.add(b35); setLayout(new BorderLayout()); add(tf1,BorderLayout.NORTH); add(p1, BorderLayout.CENTER); b11.addActionListener(this); b12.addActionListener(this); b13.addActionListener(this); b14.addActionListener(this); b15.addActionListener(this); b21.addActionListener(this); b22.addActionListener(this); b23.addActionListener(this); b24.addActionListener(this); b25.addActionListener(this); b31.addActionListener(this); b32.addActionListener(this); b33.addActionListener(this); b34.addActionListener(this); b35.addActionListener(this); } // end Kalk() public void actionPerformed(ActionEvent e){ if(e.getSource()==b11){ tehe=tehe+"0"; } if(e.getSource()==b12){ tehe=tehe+"1"; } if(e.getSource()==b13){ tehe=tehe+"2"; } if(e.getSource()==b14){ tehe=tehe+"3"; } if(e.getSource()==b15){ tehe=tehe+"4"; } if(e.getSource()==b21){ tehe=tehe+"5"; } if(e.getSource()==b22){ tehe=tehe+"6"; } if(e.getSource()==b23){ tehe=tehe+"7"; } if(e.getSource()==b24){ tehe=tehe+"8"; } if(e.getSource()==b25){ tehe=tehe+"9"; } if(e.getSource()==b31){ esimeneArv=Integer.parseInt(tehe); tf1.setText("" + esimeneArv); tehe=""; op='+'; } if(e.getSource()==b32){ esimeneArv=Integer.parseInt(tehe); tf1.setText("" + esimeneArv); tehe=""; op='-'; } if(e.getSource()==b33){ esimeneArv=Integer.parseInt(tehe); tf1.setText("" + esimeneArv); tehe=""; op='*'; } if(e.getSource()==b34){ esimeneArv=Integer.parseInt(tehe); tf1.setText("" + esimeneArv); tehe=""; op='/'; } if(e.getSource()==b35){ teineArv=Integer.parseInt(tehe); tf1.setText("" + teineArv); tehe=""; switch(op){ case '+': vastus = esimeneArv + teineArv; tf1.setText(""+vastus); break; case '-': vastus = esimeneArv - teineArv; tf1.setText(""+vastus); break; case '*': vastus = esimeneArv * teineArv; tf1.setText(""+vastus); break; case '/': jvastus = (double)esimeneArv / teineArv; String kontr="" + jvastus; //tf1.setText(kontr); System.out.println(esimeneArv+" "+teineArv+" "+kontr+" "+kontr.length()); if(kontr.substring(kontr.length()-1).equals("0")){ vastus=(int)jvastus; tf1.setText("" + vastus); kontr=""; } else { tf1.setText(""+jvastus); kontr=""; } break; } // end switch } } // end actionPerformed() public static void main(String argumendid[]) { Frame f=new Frame("KALKULAATOR"); f.setSize(200, 140); f.add(new Kalk()); f.setVisible(true); f.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); } // end of public static void main } // end class Kalk