Design simple arithmetic Calculator using Grid Layout manager.

 // Simple Caluculatorusing Grid Layout


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code = "SimpleCalc" Width=400 height=300>
</applet>
*/

public class SimpleCalc extends Applet implements ActionListener
{
    int v1,v2,result,i;
    TextField t;
    Button b[] = new Button[10];
    Button add,sub,mul,div,mod,clear,eq;
    char op;
   
    public void init()
    {
    GridLayout g = new GridLayout(4,5);
    setLayout(g);
   
    t = new TextField(10);
    for(i=0;i<=9;i++)
    {
         b[i] = new Button(""+i);
    }

    add = new Button("+");
    sub = new Button("-");
    mul = new Button("*");
    div = new Button("/");
    mod = new Button("%");
    clear = new Button("CLEAR");
    eq = new Button("=");
   
    // add components to applet window
    add(t);
    for(i=0;i<=9;i++)
    {  
        add(b[i]);
    }
    add(add);
    add(sub);
    add(mul);
    add(div);
    add(mod);
    add(clear);
    add(eq);

    // Register with Listeners
    t.addActionListener(this);
    for(i=0;i<=9;i++)
    {
          b[i].addActionListener(this);
    }
    add.addActionListener(this);
    sub.addActionListener(this);
    mul.addActionListener(this);
    div.addActionListener(this);
    mod.addActionListener(this);
    clear.addActionListener(this);
    eq.addActionListener(this);
     
    }
   
    public void actionPerformed(ActionEvent ae)
    {
    String str = ae.getActionCommand();
    char ch = str.charAt(0);
    if(Character.isDigit(ch))
    {
        t.setText(t.getText()+str);    // concatinatng
    }
    else if(str.equals("+"))
    {
        v1 = Integer.parseInt(t.getText());
        op = '+';
        t.setText("");
    }
    else if(str.equals("-"))
    {
        v1 = Integer.parseInt(t.getText());
        op = '-';
        t.setText("");
    }
    else if(str.equals("*"))
    {
        v1 = Integer.parseInt(t.getText());
        op = '*';
        t.setText("");
    }
    else if(str.equals("/"))
    {
        v1 = Integer.parseInt(t.getText());
        op = '/';
        t.setText("");
    }
    else if(str.equals("%"))
    {
        v1 = Integer.parseInt(t.getText());
        op = '%';
        t.setText("");
    }
   
    if(str.equals("="))
    {
        v2 = Integer.parseInt(t.getText());
            if(op == '+')
        result = v1 + v2;
        else if(op == '-')
        result = v1 - v2;
        else if(op == '*')
        result = v1 * v2;
        else if(op == '/')
        result = v1 / v2;
        else if(op == '%')
        result = v1 % v2;

        t.setText(""+result);
    }

    if(str.equals("CLEAR"))
    {
        t.setText("");
    }
   
    }  // actionPerformed

}  // main class

Comments

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)