About Me

My photo
'm frank, smart enough to deal with any kind of people, 've got ebility to communicate with al kind of peolpe, every1 who meets me start liking me.:) i love to smile... interestingly my name means smile:)

Sunday, May 23, 2010

My first java application - simple calculator

yup!!! this is my first application in JAVA. its a calculator, yeh yeh it has bugs and 'm trying to take them off. but still eager to post this in my blog ;).

Anyways bug is the part of software cycle u see so its a bugfull calculator n soon a bugfree will be launched when i will learn jar creation. Kepp smiling and yes the mantra Happiness Always. dont forget it. cheers.

Please find the code below;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class CalC implements ActionListener {
private JFrame f;
private JButton b1, b2, b3, b4, b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18;
private JLabel l1;
public int sum = 0,res=0,n1=0,n2=0,sum1=1,d,n3=1;
public char a;

public CalC() {
f = new JFrame("Tabu's calculator");
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);
b16 = new JButton("0");
b16.addActionListener(this);
b17 = new JButton("Reset");
b17.addActionListener(this);
b10 = new JButton("+");
b10.addActionListener(this);
b11 = new JButton("-");
b11.addActionListener(this);
b12 = new JButton("*");
b12.addActionListener(this);
b13 = new JButton("/");
b13.addActionListener(this);
b14 = new JButton("%");
b14.addActionListener(this);
b15 = new JButton("=");
b15.addActionListener(this);
b18 = new JButton("Exit");
b18.addActionListener(this);
l1 = new JLabel(" ");
}



public void launchFrame() {
f.setLayout (new GridLayout(7,3));
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b16);
f.add(b10);
f.add(b11);
f.add(b12);
f.add(b13);
f.add(b14);
f.add(b15);
f.add(b17);
f.add(b18);
f.add(l1);
f.pack();
f.setSize(300,300);
f.setLocation(200,200);
f.setVisible(true);
}

public void actionPerformed (ActionEvent ev)
{
try
{
if(ev.getSource()==b1)
{
n1=n1*10+1;
l1.setText(" " +n1);

}

if(ev.getSource()==b2)
{
n1=n1*10+2;
l1.setText(" " +n1);
}

if(ev.getSource()==b3)
{
n1=n1*10+3;
l1.setText(" " +n1);
}

if(ev.getSource()==b4)
{
n1=n1*10+4;
l1.setText(" " +n1);
}

if(ev.getSource()==b5)
{
n1=n1*10+5;
l1.setText(" " +n1);

}

if(ev.getSource()==b6){
n1=n1*10+6;
l1.setText(" " +n1);
}

if(ev.getSource()==b7)
{
n1=n1*10+7;
l1.setText(" " +n1);
}

if(ev.getSource()==b8)
{
n1=n1*10+8;
l1.setText(" " +n1);
}

if(ev.getSource()==b9)
{
n1=n1*10+9;
l1.setText(" " +n1);
}

if(ev.getSource()==b16)
{
n1=n1*10+0;
l1.setText(" " +n1);
}

if(ev.getSource()==b10)
{
n2=n1;
n1=0;
sum= sum+n2;
l1.setText(" " +sum);
a='+';
}

if(ev.getSource()==b11)
{

if (n3==1)
{
n2=n1;
sum = n2;
l1.setText(" " +n2);
n3=0;
n1=0;
a='-';
}

else

{
n2=n1;
n1=0;
sum=sum-n2;
l1.setText(" " +sum);
n3=0;
a='-';
}
}

if(ev.getSource()==b12)
{
n2=n1;
n1=0;
sum1=sum1*n2;
l1.setText(" " +sum1);
a='*';
}


if(ev.getSource()==b13)
{
n2=n1;
n1=0;
l1.setText("singlw div only");
a='/';
}

if(ev.getSource()==b14)
{
n2=n1;
n1=0;
l1.setText(" " +n2);
a='%';
}


if(ev.getSource()==b17)
{
n1=0;
n2=0;
sum=0;
res=0;
l1.setText("00");
sum1=0;
n3=1;
}

if(ev.getSource()==b18)
{
f.setVisible(false);
}


if(ev.getSource()==b15)
{
switch (a)
{
case '+':
res =sum+ n1;
l1.setText(" " +res);
sum=res;
break;

case '-':
res = sum-n1;
l1.setText(" " +res);
sum=res;
break;


case '*':
res = n1*sum1;
l1.setText(" " +res);
sum1=res;
break;

case '/':


if(n2==0)
1.setText("/by0");
else

{
res = n2/n1;
l1.setText(" " +res);
}

break;


case '%':
res = n2%n1;
l1.setText(" " +res);
n1=0;
n2=0;
sum=0;
break;


}
a=' ';

}

}

catch(Exception e)
{
System.out.println("Error: " +e.getMessage());
}
}



public static void main(String args[]) {
CalC grid = new CalC();
grid.launchFrame();
}
}

No comments: