JAVA CODE

//Ex12_4 program
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Ex12_4 extends Applet
             implements ActionListener
{
   private TextField MTxt;
   private Button ClrBtn;
   private int amount, total;

   public void init()
   {
      MTxt = new TextField (7);
      MTxt.addActionListener (this);
      add (MTxt);

      ClrBtn = new Button ("Clear");
      ClrBtn.addActionListener (this);
      add (ClrBtn);
   }

   public void paint (Graphics g)
   {
      g.drawString ("Total: RM " + total, 30, 50);
   }

   public void actionPerformed (ActionEvent e)
   {
      if (e.getSource() == MTxt)
      {
         amount = Integer.parseInt (MTxt.getText());
         total = total + amount;
      }
      else if (e.getSource() == ClrBtn)
         total = 0;

      MTxt.setText ("");
      MTxt.requestFocus();
      repaint();
   }
}