Calculator, Java
Calculator, Java
Calculator, Java
*;
import java.awt.event.*;
//Declaration of Objects
Calculator()
//Giving Coordinates
l1.setBounds(50,100,100,20);
l2.setBounds(50,140,100,20);
l3.setBounds(50,180,100,20);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(50,250,50,20);
b2.setBounds(110,250,50,20);
b3.setBounds(170,250,50,20);
b4.setBounds(230,250,50,20);
b5.setBounds(290,250,50,20);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
f.setSize(400,350);
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
if(e.getSource()==b2)
t3.setText(String.valueOf(n1-n2));
if(e.getSource()==b3)
t3.setText(String.valueOf(n1*n2));
if(e.getSource()==b4)
t3.setText(String.valueOf(n1/n2));
if(e.getSource()==b5)
System.exit(0);
new Calculator();
}}
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
static JFrame f;
// create a textfield
static JTextField l;
// default constructor
calculator()
s0 = s1 = s2 = "";
// main function
// create a frame
f = new JFrame("calculator");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (Exception e) {
System.err.println(e.getMessage());
}
// create a object of class
// create a textfield
l = new JTextField(16);
l.setEditable(false);
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// equals button
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
// create . button
be = new JButton(".");
// create a panel
bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);
// add elements to panel
p.add(l);
p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bd);
p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);
p.setBackground(Color.blue);
f.add(p);
f.setSize(200, 220);
f.show();
}
String s = e.getActionCommand();
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;
l.setText(s0 + s1 + s2);
s0 = s1 = s2 = "";
l.setText(s0 + s1 + s2);
double te;
if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
else {
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
// convert it to string
s0 = Double.toString(te);
s1 = s;
s2 = "";
l.setText(s0 + s1 + s2);
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
JButton b10,b11,b12,b13,b14,b15;
int i,r,n1,n2;
JTextField res;
char op;
public Calculator() {
super("Calulator");
setLayout(new BorderLayout());
p.setLayout(new GridLayout(4,4));
for(int i=0;i<=9;i++) {
b[i]=new JButton(i+"");
p.add(b[i]);
b[i].addActionListener(this);
b10=new JButton("+");
p.add(b10);
b10.addActionListener(this);
b11=new JButton("-");
p.add(b11);
b11.addActionListener(this);
b12=new JButton("*");
p.add(b12);
b12.addActionListener(this);
b13=new JButton("/");
p.add(b13);
b13.addActionListener(this);
b14=new JButton("=");
p.add(b14);
b14.addActionListener(this);
b15=new JButton("C");
p.add(b15);
b15.addActionListener(this);
res=new JTextField(10);
add(p,BorderLayout.CENTER);
add(res,BorderLayout.NORTH);
setVisible(true);
setSize(200,200);
JButton pb=(JButton)ae.getSource();
if(pb==b15) {
r=n1=n2=0;
res.setText("");
} else if(pb==b14) {
n2=Integer.parseInt(res.getText());
eval();
res.setText(""+r);
} else {
boolean opf=false;
if(pb==b10) {
op='+';
opf=true;
if(pb==b11) {
op='-';
opf=true;
if(pb==b12) {
op='*';
opf=true;
if(pb==b13) {
op='/';
opf=true;
if(opf==false) {
for(i=0;i<10;i++) {
if(pb==b[i]) {
String t=res.getText();
t+=i;
res.setText(t);
} else {
n1=Integer.parseInt(res.getText());
res.setText("");
int eval() {
switch(op) {
case '+':
r=n1+n2;
break;
case '-':
r=n1-n2;
break;
case '*':
r=n1*n2;
break;
case '/':
r=n1/n2;
break;
return 0;
new Calculator();
Rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
import java.awt.*;
import java.awt.event.*;
/*********************************************/
char op;
String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };
String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };
///////////////////////////
MyCalculator(String frameText)//constructor
super(frameText);
displayLabel.setBounds(tempX,y,240,HEIGHT);
displayLabel.setBackground(Color.BLUE);
displayLabel.setForeground(Color.WHITE);
add(displayLabel);
add(memLabel);
tempX=TOPX;
y=TOPY+2*(HEIGHT+V_SPACE);
memoryButton[i].setForeground(Color.RED);
y+=HEIGHT+V_SPACE;
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);
for(int i=0;i<specialButton.length;i++)
specialButton[i].setForeground(Color.RED);
tempX=tempX+2*WIDTH+H_SPACE;
int digitX=TOPX+WIDTH+H_SPACE;
int digitY=TOPY+2*(HEIGHT+V_SPACE);
tempX=digitX; y=digitY;
for(int i=0;i<digitButton.length;i++)
digitButton[i].setForeground(Color.BLUE);
tempX+=WIDTH+H_SPACE;
if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}
}
//set Co-ordinates for Operator Buttons
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;
int opsY=digitY;
tempX=opsX; y=opsY;
for(int i=0;i<operatorButton.length;i++)
tempX+=WIDTH+H_SPACE;
operatorButton[i].setForeground(Color.RED);
if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}
addWindowListener(new WindowAdapter()
{System.exit(0);}
});
setLayout(null);
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setVisible(true);
//////////////////////////////////
String resText=""+temp;
if(resText.lastIndexOf(".0")>0)
resText=resText.substring(0,resText.length()-2);
return resText;
}
////////////////////////////////////////
/*******************************************/
MyCalculator cl;
//////////////////////////////////////////
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
////////////////////////////////////////////////
return false;
}
/////////////////////////////////////////////////
String tempText=((MyDigitButton)ev.getSource()).getLabel();
if(tempText.equals("."))
if(cl.setClear)
{cl.displayLabel.setText("0.");cl.setClear=false;}
else if(!isInString(cl.displayLabel.getText(),'.'))
cl.displayLabel.setText(cl.displayLabel.getText()+".");
return;
int index=0;
try{
index=Integer.parseInt(tempText);
}catch(NumberFormatException e){return;}
if(cl.setClear)
{cl.displayLabel.setText(""+index);cl.setClear=false;}
else
cl.displayLabel.setText(cl.displayLabel.getText()+index);
}//actionPerformed
}//class defination
/********************************************/
class MyOperatorButton extends Button implements ActionListener
MyCalculator cl;
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
///////////////////////
String opText=((MyOperatorButton)ev.getSource()).getLabel();
cl.setClear=true;
double temp=Double.parseDouble(cl.displayLabel.getText());
if(opText.equals("1/x"))
try
{double tempd=1/(double)temp;
cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
catch(ArithmeticException excp)
{cl.displayLabel.setText("Divide by 0.");}
return;
}
if(opText.equals("sqrt"))
try
{double tempd=Math.sqrt(temp);
cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
catch(ArithmeticException excp)
{cl.displayLabel.setText("Divide by 0.");}
return;
if(!opText.equals("="))
cl.number=temp;
cl.op=opText.charAt(0);
return;
switch(cl.op)
case '+':
temp+=cl.number;break;
case '-':
temp=cl.number-temp;break;
case '*':
temp*=cl.number;break;
case '%':
try{temp=cl.number%temp;}
catch(ArithmeticException excp)
case '/':
try{temp=cl.number/temp;}
catch(ArithmeticException excp)
break;
}//switch
cl.displayLabel.setText(MyCalculator.getFormattedText(temp));
//cl.number=temp;
}//actionPerformed
}//class
/****************************************/
MyCalculator cl;
/////////////////////////////////
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
////////////////////////////////////////////////
public void actionPerformed(ActionEvent ev)
char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);
cl.setClear=true;
double temp=Double.parseDouble(cl.displayLabel.getText());
switch(memop)
case 'C':
cl.memLabel.setText(" ");cl.memValue=0.0;break;
case 'R':
cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break;
case 'S':
cl.memValue=0.0;
case '+':
cl.memValue+=Double.parseDouble(cl.displayLabel.getText());
if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0") )
cl.memLabel.setText(" ");
else
cl.memLabel.setText("M");
break;
}//switch
}//actionPerformed
}//class
/*****************************************/
MyCalculator cl;
super(cap);
setBounds(x,y,width,height);
this.cl=clc;
this.cl.add(this);
addActionListener(this);
//////////////////////
String Res="";
return Res;
//////////////////////////////////////////////////////////
String opText=((MySpecialButton)ev.getSource()).getLabel();
if(opText.equals("Backspc"))
String tempText=backSpace(cl.displayLabel.getText());
if(tempText.equals(""))
cl.displayLabel.setText("0");
else
cl.displayLabel.setText(tempText);
return;
if(opText.equals("C"))
cl.memLabel.setText(" ");
Mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmm
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JFrame frame;
JTextField textfield;
JButton addButton,subButton,mulButton,divButton;
JPanel panel;
char operator;
Calculator(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
textfield.setFont(myFont);
textfield.setBackground(new Color(124,252,0));
textfield.setEditable(false);
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;
for(int i =0;i<9;i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
for(int i =0;i<10;i++) {
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
negButton.setBounds(50,430,100,50);
delButton.setBounds(150,430,100,50);
clrButton.setBounds(250,430,100,50);
panel.setLayout(new GridLayout(4,4,10,10));
panel.setBackground(new Color(124,252,0));
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
frame.add(panel);
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);
frame.setVisible(true);
}
public static void main(String[] args) {
@Override
for(int i=0;i<10;i++) {
if(e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
if(e.getSource()==decButton) {
textfield.setText(textfield.getText().concat("."));
if(e.getSource()==addButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='+';
textfield.setText("");
if(e.getSource()==subButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='-';
textfield.setText("");
if(e.getSource()==mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='*';
textfield.setText("");
if(e.getSource()==divButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='/';
textfield.setText("");
if(e.getSource()==equButton) {
num2=Double.parseDouble(textfield.getText());
switch(operator) {
case'+':
result=num1+num2;
break;
case'-':
result=num1-num2;
break;
case'*':
result=num1*num2;
break;
case'/':
result=num1/num2;
break;
textfield.setText(String.valueOf(result));
num1=result;
if(e.getSource()==clrButton) {
textfield.setText("");
if(e.getSource()==delButton) {
textfield.setText("");
for(int i=0;i<string.length()-1;i++) {
textfield.setText(textfield.getText()+string.charAt(i));
if(e.getSource()==negButton) {
temp*=-1;
textfield.setText(String.valueOf(temp));