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:
Getting basics like fulltext search, search for tags and comments working reliably would be a huge step forward already. ,
By timeboxing we communicate that our priority is schedule, most obviously over scope. ,
hi i like this site give me good scope for my projects
hi thanks for easy calculator in java
hello frinds i can explain this program to u this is just simple
i didnt get wat i want …
Thanks…but if explantion is provided it is much easier to understand.
thanks,this program helps me a lot… i also want to know how to run this program..thank you and God bless
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..
super program…thanks alot…it would be better if explanation was given..
Thanx a lot for the program .it is SUPERB……………………
Thanx for but can you also give code for creating a registration?
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
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();
}
}
its simple and easy to understand thanks a lot pa and also some explanation is need that the one query i have
calculator in java applet
It helps me a lot & very useful…
where is the main method in the program(Public static void main(String arg[]))
Thanks for give this program
jfoiusdjfdopfuisjkrjfdsfvjkxdcgvcvjdfjiguiosdjthuiofyhednhvcoisdjoijrtfiodu0ufjkjsdoicjfoijsdioufosjdiojfoijoisdjvicjklcjxoicujxfoijoisdvjiojjOIRFUodjijvcoxhjgfodvohdsfhvhcfgoixdj
hi thanks for your calci program plz explain this program
java
Java programming software send the link for window 7 nd 32 bit
hey!!!………where”s the main method dude???????
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 ?
can you please upload the algorithm for this program
its not give us result,,, any body help me////
In my applet i’m getting ‘Start:applet not initialized’ at left bottom.. what does it mean and how can i resolve it??
It’s not running.
Thank you very much its too much helping ….. God Bless You
Thanks for the program
not working waste……..
good program
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
thanks…works like a charm
where is the main method
thanks 4 the program
for an applet program main fn is not req. we just initialise the applet using init.
baba
Applets me main method nahi hota
and after compiling interpret toh
“appletviewer ” se hota he
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
the calculator program runs perfect
Thanks for save my precious time.
Prog works but can any1 explain me the function of the line 66 and 67..
there is no option to check the output
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
main method is not found
Tq so much
its nice to used the code but can you explain than to us
Without paint() how does this program gives output?
That’s program a very nice