Write a Java Program to generate a simple calculator. (WITH OUTPUT IMAGE).

Write a Simple Java Program to generate a simple calculator.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="xyz" width=200 height=300>
</applet>
*/
public class xyz extends java.applet.Applet implements ActionListener
{
            Double cv=0.0,pv=0.0,result=0.0;
            int count=0;
            TextField opd1 = new TextField(10);
        TextField opd2 = new TextField(10);
        TextField oprt = new TextField(1);
            TextField Res = new TextField(10);
            String opr="";
            public void init()
            {          
                        int i=0;
                Button b[] = new Button[17];
                        String a[] = {"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","=","Clear"};
                        setLayout(new GridLayout(0,4));
                        add(new Label("Operand 1: "));
                add(opd1);
                add(new Label("Operator: "));
                        add(oprt);
                        add(new Label("Operand 2: "));                                               
                add(opd2);
                        add(new Label("Result: "));
                add(Res);
                        opd1.addActionListener(this);
                opd2.addActionListener(this);
                oprt.addActionListener(this);
                        Res.addActionListener(this);
                        //setLayout(new GridLayout(0,3));
                        for(i=0;i<17;i++)
                        {
                                    b[i] = new Button(a[i]);
                                    add(b[i]);
                                    b[i].addActionListener(this);
                        }
                        opd1.setText("");
            }
            public void actionPerformed(ActionEvent ae)
            {                 String s=ae.getActionCommand();
                        if(s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*"))
                        {           count++;
                                    oprt.setText(s);
                                    opd2.setText("");        
                                    opr=s;
                                    pv = Double.parseDouble(opd1.getText());
                        }
                        else if(s.equals("="))
                        {
                                    cv=Double.parseDouble(opd2.getText());
                                    if(opr == "+")
                                    result = pv+cv;
                                    else if(opr == "-")
                                    result = pv-cv;
                                    else if(opr == "*")
                                    result = pv*cv;
                                    else if(opr == "/")
                                    result = pv/cv;
                        }
                        else if(s.equals("Clear"))
                        {
                                    pv=cv=result=0.0;
                                    count=0;
                                    opd1.setText("0");
                                    opd2.setText("0");
                                    //oprt.setText("0");
                        }
                        else
                        {
                                    if(count==0)
                                    opd1.setText(opd1.getText().concat(s));
                                    else
                                    opd2.setText(opd2.getText().concat(s));
                        }
                        Res.setText(String.valueOf(result));  
            }
}

Output:


Comments