0% found this document useful (0 votes)
26 views3 pages

Calc 21

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Calc 21

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.
Handle any possible exceptions like divided by zero.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Calculator extends Applet implements
ActionListener
{
String msg=" ";
int p,q,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
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("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
mod=new Button("%");
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)
{
try
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("+"))
{
p=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("-"))
{
p=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("*"))
{
p=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("/"))
{
p=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("%"))
{
p=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
q=Integer.parseInt(t1.getText());
if(OP=='+')
result=p+q;
else if(OP=='-')
result=p-q;
else if(OP=='*')
result=p*q;
else if(OP=='/')
result=p/q;
else if(OP=='%')
result=p%q;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
catch(ArithmeticException a)
{
JOptionPane.showMessageDialog(null,"dividebyzero");
}
}
}

You might also like