Calculator in Java including UI

Hey here is one more program which i have implemented. Its a calculator with good gui 
Enjoy!!


Attached File  cal.png   26.43K   163 downloads

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{

       
String s,ans=" ",str=" ";

       
TextField Text1;

       
int flag=0,str_length;

       
Button Button1,Button2,Button3,Button4,Button5,Button6,Button7,Button8,Button9,Button0;

       
Button ButtonClr,ButtonEql,ButtonPlus,ButtonMinus, ButtonMul,ButtonPoint,ButtonDiv,ButtonBck;

       
Color BackColor;

       
public void init()

         
{

               
Text1=new TextField(10);                add(Text1);    

               
Button1=new Button("1");                add(Button1);     Button1.addActionListener(this);

               
Button2=new Button("2");                add(Button2);     Button2.addActionListener(this);

               
Button3=new Button("3");                add(Button3);     Button3.addActionListener(this);

               
Button4=new Button("4");                add(Button4);     Button4.addActionListener(this);

               
Button5=new Button("5");                add(Button5);     Button5.addActionListener(this);

               
Button6=new Button("6");                add(Button6);     Button6.addActionListener(this);

               
Button7=new Button("7");                add(Button7);     Button7.addActionListener(this);

               
Button8=new Button("8");             &nb
sp;  add
(Button8);     Button8.addActionListener(this);

               
Button9=new Button("9");                add(Button9);     Button9.addActionListener(this);

               
Button0=new Button("0");                add(Button0);     Button0.addActionListener(this);

               
ButtonClr=new Button("CLR");    add(ButtonClr); ButtonClr.addActionListener(this);

               
ButtonEql=new Button("=");        add(ButtonEql);       ButtonEql.addActionListener(this);

               
ButtonPlus=new Button("+");      add(ButtonPlus);   ButtonPlus.addActionListener(this);

               
ButtonMinus=new Button("-");    add(ButtonMinus);  ButtonMinus.addActionListener(this);

               
ButtonMul=new Button("*");        add(ButtonMul);       ButtonMul.addActionListener(this);

               
ButtonPoint=new Button(".");    add(ButtonPoint);  ButtonPoint.addActionListener(this);

               
ButtonDiv=new Button("/");        add(ButtonDiv);       ButtonDiv.addActionListener(this);

               
ButtonBck=new Button("<-");      add(ButtonBck);        ButtonBck.addActionListener(this);

               
BackColor = new Color(60,60,122);

       
}

       

       
public void paint(Graphics g)

         
{

                 g
.setColor(BackColor);

                 g
.fillRect(50,50,250,300);

                 
Text1.setBounds(70,60,210,20);

                 
Button1.setBounds(80,100,50,20);  

                 
Button2.setBounds(150,100,50,20);

                 
Button3.setBounds(220,100,50,20);

                 
Button4.setBounds(80,140,50,20);

                 
Button5.setBounds(150,140,50,20);

                 
Button6.setBounds(220,140,50,20);

                 
Button7.setBounds(80,180,50,20);

                 
Button8.setBounds(150,180,


50,20);

                 
Button9.setBounds(220,180,50,20);

                 
Button0.setBounds(80,220,50,20);

                 
ButtonClr.setBounds(150,220,50,20);

                 
ButtonEql.setBounds(220,220,50,20);

                 
ButtonPlus.setBounds(80,260,50,20);

                 
ButtonMinus.setBounds(150,260,50,20);

                 
ButtonMul.setBounds(220,260,50,20);

                 
ButtonPoint.setBounds(80,300,50,20);

                 
ButtonDiv.setBounds(150,300,50,20);

                 
ButtonBck.setBounds(220,300,50,20);

         
}

         

       
public void actionPerformed(ActionEvent evt)

         
{

                 s
=Text1.getText();

                 
if(evt.getSource()==Button1) {

                         addChar
(s,"1");        

                 
}

                 

                 
else if(evt.getSource()==Button2){

                          addChar
(s,"2");  

                 
}

                 

                 
else if(evt.getSource()==Button3){

                          addChar
(s,"3");  

                 
}

                 

                 
else if(evt.getSource()==Button4){

                         addChar
(s,"4");  

                 
}

                 

                 
else if(evt.getSource()==Button5){

                          addChar
(s,"5");  

                 
}

                 

                 
else if(evt.getSource()==Button6){

                          addChar
(s,"6");  

                 
}

                 

                 
else if(evt.getSource()==Button7){

                         addChar
(s,"7");  

                 
}

                 

                 
else if(evt.getSource()==Button8){

                       
  addChar
(s,"8");  

                 
}

                 

                 
else if(evt.getSource()==Button9){

                          addChar
(s,"9");  

                 
}

                 

                 
else if(evt.getSource()==Button0){

                          addChar
(s,"0");  

                 
}

                 

                 
else if(evt.getSource()==ButtonClr){

                       
Text1.setText(" ");

                 
}

                 

                 
else if(evt.getSource()==ButtonPlus){

                          addChar
(s,"+");  

                          flag
=1;

                         

                 
}

                 

                 
else if(evt.getSource()==ButtonMinus){

                          addChar
(s,"-");  

                          flag
=2;

                 
}

                 

                 
else if(evt.getSource()==ButtonMul){

                          addChar
(s,"*");  

                          flag
=3;

                 
}

                 
else if(evt.getSource()==ButtonPoint){

                          addChar
(s,".");

                 
}

                 

                 
else if(evt.getSource()==ButtonDiv){

                          addChar
(s,"/");

                          flag
=4;

                 
}

                 

                 
else if(evt.getSource()==ButtonBck){

                           deleteChar
(s);

                   
}

                           

                 
else if(evt.getSource()==ButtonEql){

                           
if(flag==1)

                          get_String
(flag);

                         
if(flag==2)

                          get_String
(flag);

                         
if(flag==3)

                          get_String
(flag);

                         
if(


flag==4);

                          get_String
(flag);

                 
}

         
}

         

         
public void addChar(String input, String character)

         
{

                 

                 
if(String.valueOf(input)==null)

                           character
=String.valueOf(input);

                           
else{

                                        input
+=character;

                                       
Text1.setText(input);

                                 
}

           
}

               

               

         
public void get_String( int cnt)

               
{  

                   
String y=" ",x=" ";

                   
char operator;

                   
int i,j,count=0;

                   str
=Text1.getText();

                   str_length
=str.length();

                 

                   
if(cnt==1)

                       
{

                         count
=str.indexOf("+");

                         
}

                         
else if(cnt==2)

                         
{

                                count
=str.indexOf("-");

                         
}

                         
else if(cnt==3)

                         
{

                                  count
=str.indexOf("*");

                         
}

                         
else if(cnt==4)

                           
{

                                   count
=str.indexOf("/");

                         
}

                 
try

                 
{

                   
operator=str.charAt(count);

                   x
=str.substring(0,count);

                   y
=str.substring(count+1,str_length);

                   compute
(operator,x,y);

                   
}

                   
catch(Exception e){}

                 
}

                 

                 
public void compute(char op,String val1,String val2)

                 
{

                         
Double value1=0.0,value2=0.0,tmp_ans=0.0;

                          value1
=Double.parseDouble(val1);

                          value2
=Double.parseDouble(val2);

                         
if(op=='+') tmp_ans=value1+value2;

                         
if(op=='-') tmp_ans=value1-value2;

                         
if(op=='*') tmp_ans=value1*value2;

                         
if(op=='/') tmp_ans=value1/value2;

                          ans
=String.valueOf(tmp_ans);

                         
Text1.setText(ans);

                 
}

                         

                 
public void deleteChar( String str)

                   
{

                           
String tmp_str="";

                           str_length
=str.length();

                           tmp_str
=str.substring(0,str_length-1);

                           
Text1.setText(tmp_str);

                   
}
}

Thank you

No comments:

Post a Comment