Java applet program for calculator

Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the + – * % operations. Add a text field to display the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
/* 
<applet code="Cal" width=300 height=300>
</applet>
*/
 
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;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))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.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;
t1.setText(""+result);
}	
if(str.equals("clear"))
{
t1.setText("");
}
}
}

Output:

Java program base calculator output
Java program base calculator output

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

66 thoughts on “Java applet program for calculator

  1. thanks,this program helps me a lot… i also want to know how to run this program..thank you and God bless

  2. thanks,this program helps me a lot… i also want to know how to run this program..coz i will use this in school..thank you sir..

  3. mmm i got it it is so nice example thanks we wait for more examples u know ure teacher gave me asighnment to make calculatore in jave but i found it is code one again thanks

  4. import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    class cal extends Frame implements ActionListener
    {
    double temp1,temp2,result;
    char ch;
    JTextField t=new JTextField();
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,d,sum,sub,mul,div,equ,ac;

    cal()
    {
    b1=new JButton(“1”);
    b1.addActionListener(this);

    b2=new JButton(“2”);
    b2.addActionListener(this);
    b3=new JButton(“3”);
    b3.addActionListener(this);
    b4=new JButton(“4”);
    b4.addActionListener(this);
    b5=new JButton(“5”);
    b5.addActionListener(this);
    b6=new JButton(“6”);
    b6.addActionListener(this);
    b7=new JButton(“7”);
    b7.addActionListener(this);
    b8=new JButton(“8”);
    b8.addActionListener(this);
    b9=new JButton(“9”);
    b9.addActionListener(this);
    b0=new JButton(“0”);
    b0.addActionListener(this);
    d=new JButton(“.”);
    d.addActionListener(this);
    sum=new JButton(“+”);
    sum.addActionListener(this);
    sub=new JButton(“-“);
    sub.addActionListener(this);
    mul=new JButton(“*”);
    mul.addActionListener(this);
    div=new JButton(“/”);
    div.addActionListener(this);
    equ=new JButton(“=”);
    equ.addActionListener(this);
    ac=new JButton(“AC”);
    ac.addActionListener(this);

    Panel p=new Panel();
    p.setLayout(new GridLayout(4,3));
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b0);
    p.add(d);
    p.add(sum);
    p.add(sub);
    p.add(mul);
    p.add(div);
    p.add(equ);
    p.add(ac);
    Panel p1=new Panel();
    p1.setLayout(new GridLayout(1,1));
    p1.add(t);
    Frame f=new Frame();
    f.setLayout(new GridLayout(2,1));

    f.add(p1);
    f.add(p);
    f.setSize(400,400);
    f.setVisible(true);

    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
    {
    t.setText(t.getText()+”1″);
    }
    if(e.getSource()==b2)
    {
    t.setText(t.getText()+”2″);
    }
    if(e.getSource()==b3)
    {
    t.setText(t.getText()+”3″);
    }
    if(e.getSource()==b4)
    {
    t.setText(t.getText()+”4″);
    }
    if(e.getSource()==b5)
    {
    t.setText(t.getText()+”5″);
    }
    if(e.getSource()==b6)
    {
    t.setText(t.getText()+”6″);
    }
    if(e.getSource()==b7)
    {
    t.setText(t.getText()+”7″);
    }
    if(e.getSource()==b8)
    {
    t.setText(t.getText()+”8″);
    }
    if(e.getSource()==b9)
    {
    t.setText(t.getText()+”9″);
    }
    if(e.getSource()==b0)
    {
    t.setText(t.getText()+”0″);
    }
    if(e.getSource()==d)
    {
    t.setText(t.getText()+”.”);
    }
    if(e.getSource()==ac)
    {
    t.setText(“”);
    }
    if(e.getSource()==sum)
    {
    temp1=Double.parseDouble(t.getText());
    t.setText(“”);
    ch=’+’;
    }
    if(e.getSource()==sub)
    {
    temp1=Double.parseDouble(t.getText());
    t.setText(“”);
    ch=’-‘;
    }
    if(e.getSource()==mul)
    {
    temp1=Double.parseDouble(t.getText());
    t.setText(“”);
    ch=’*’;
    }
    if(e.getSource()==div)
    {
    temp1=Double.parseDouble(t.getText());
    t.setText(“”);
    ch=’/’;
    }
    if(e.getSource()==equ)
    {

    temp2=Double.parseDouble(t.getText());
    if(ch==’+’)
    {
    result=temp1+temp2;
    t.setText(Double.toString(result));
    }
    if(ch==’-‘)
    {
    result=temp1-temp2;
    t.setText(Double.toString(result));
    }
    if(ch==’*’)
    {
    result=temp1*temp2;
    t.setText(Double.toString(result));
    }
    if(ch==’/’)
    {
    result=temp1/temp2;
    t.setText(Double.toString(result));
    }

    }
    }
    public static void main(String arg[])
    {
    cal c=new cal();

    }
    }

  5. its simple and easy to understand thanks a lot pa and also some explanation is need that the one query i have

  6. jfoiusdjfdopfuisjkrjfdsfvjkxdcgvcvjdfjiguiosdjthuiofyhednhvcoisdjoijrtfiodu0ufjkjsdoicjfoijsdioufosjdiojfoijoisdjvicjklcjxoicujxfoijoisdvjiojjOIRFUodjijvcoxhjgfodvohdsfhvhcfgoixdj

  7. it is very useful for me… i can easily understand its very simple pls give explanation of this pgm then where is the main method ?

  8. In my applet i’m getting ‘Start:applet not initialized’ at left bottom.. what does it mean and how can i resolve it??

  9. probably u have miss spelled the class name. use caps ‘C’ for cal class name in both the java code as well as in the html file

  10. baba
    Applets me main method nahi hota
    and after compiling interpret toh
    “appletviewer ” se hota he

  11. I m getting 2 errors in 16 th lin new. Nd char ch = str.charAT (0); plz change it plz helpbme tomorrow I have lab internal dam plz send rly

  12. Thanks. It was crazy looking for a solution. This code is when a decimal point is added.

    else if(operador == ‘.’)
    result = Double.parseDouble((int)v1+”.”+(int)v2);

    Bye

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in