Tuesday, 7 July 2015

Write a Program to create calculator using Applet


Applet Program
  
         
import java.applet.Applet;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AppletCalc extends Applet implements ActionListener {

    TextField ipValue1, ipValue2, display;
    Button sum, div, sub, mul;

    public void init() {
        ipValue1 = new TextField();
        ipValue2 = new TextField();
        display = new TextField();
        sum = new Button("SUM");
        div = new Button("DIV");
        sub = new Button("SUB");
        mul = new Button("MUL");
        ipValue1.addActionListener(this);
        ipValue2.addActionListener(this);
        display.addActionListener(this);
        sum.addActionListener(this);
        sub.addActionListener(this);
        div.addActionListener(this);
        mul.addActionListener(this);
        add(ipValue1);
        add(ipValue2);
        add(display);
        add(sum);
        add(sub);
        add(div);
        add(mul);
        GridLayout gl = new GridLayout(3, 1);
        setLayout(gl);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String str = e.getActionCommand();
        int val1 = Integer.parseInt(ipValue1.getText());
        int val2 = Integer.parseInt(ipValue2.getText());
        int val3;
        if (str.equals("SUM")) {
            val3 = val1 + val2;
            display.setText(Integer.toString(val3));
        } else if (str.equals("SUB")) {
            val3 = val1 - val2;
            display.setText(Integer.toString(val3));
        } else if (str.equals("MUL")) {
            val3 = val1 * val2;
            display.setText(Integer.toString(val3));
        } else if (str.equals("DIV")) {
            val3 = val1 / val2;
            display.setText(Integer.toString(val3));
        }
    }
}
  
  
Output

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete