JAVA Demo Programs



1) Simple Calculator

-----------------------------------------------------------------
//A program for calculator
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;
/*
<applet code="Calculator" height="300" width="250">
</applet>
*/

public class Calculator extends Applet implements ActionListener
{
Button but[]=new Button[17];
Panel pan;
BorderLayout bl;
TextField tf;
boolean bolOp=false;
double data;
double temp;
String num="789/456*123-0.=+";
char chOp;
Font fon;
Image img;
public void init()
{
img=getImage(getCodeBase(),"bClear1.gif");
//********* Seting layout *********
setLayout(null);
//******** Setting Font ***********
fon=new Font("Times New Roman",Font.BOLD,12);
setFont(fon);
//******** Crating instanceof Button calss *********
for(int i=0;i<16;i++)
{
but[i]=new Button();
}
//************ Adding TextFields on Applet *******
tf=new TextField();
tf.setBounds(10,10,200,30);
add(tf);
//*********** Crating Panel **************
pan=new Panel();
//**************** Putting button on Panel ********
pan.setLayout(new GridLayout(4,5,5,5));
for(int i=0;i<16;i++)
{
but[i].setLabel(""+num.charAt(i));
pan.add(but[i]);

}

pan.setBounds(10,85,200,200);
add(pan);
//*********** Adding Clear Button *********
but[16]=new Button("Clear");
but[16].setBounds(30,50,80,30);
add(but[16]);

//**************** Register the button with action Listner
for(int i=0;i<17;i++)
(but[i]).addActionListener(this);
addMouseListener(new MyEvent());
}//End init()


public void paint(Graphics g)
{
g.drawImage(img,130,50,this);

}

public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==but[0])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(0));
}
else
{
tf.setText(tf.getText()+num.charAt(0));
}
if(o==but[1])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(1));
}
else
{
tf.setText(tf.getText()+num.charAt(1));
}
if(o== but[2] )
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(2));
}
else
{
tf.setText(tf.getText()+num.charAt(2));
}
//*********** For / operator ************
if(o==but[3] )
{
bolOp=true;
data=Double.parseDouble(tf.getText());
chOp='/';
}
if(o== but[4])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(4));
}
else
{
tf.setText(tf.getText()+num.charAt(4));
}
if(o== but[5])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(5));
}
else
{
tf.setText(tf.getText()+num.charAt(5));
}
if(o== but[6])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(6));
}
else
{
tf.setText(tf.getText()+num.charAt(6));
}
//*********** For * operator ************
if(o== but[7])
{ bolOp=true;
data=Double.parseDouble(tf.getText());
chOp='*';
}

if(o== but[8])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(8));
}
else
{
tf.setText(tf.getText()+num.charAt(8));
}
if(o== but[9])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(9));
}
else
{
tf.setText(tf.getText()+num.charAt(9));
}
if(o== but[10])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(10));
}
else
{
tf.setText(tf.getText()+num.charAt(10));
}
//*********** For -- operator ************
if(o== but[11])
{ bolOp=true;
data=Double.parseDouble(tf.getText());
chOp='-';
}
if(o== but[12])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(12));
}
else
{
tf.setText(tf.getText()+num.charAt(12));
}
if(o== but[13])
if(bolOp==true)
{
bolOp=false;
tf.setText(""+num.charAt(13));
}
else
{
tf.setText(tf.getText()+num.charAt(13));
}
//*********** For = operator ************
if(o== but[14])
{
bolOp=true;
switch(chOp)
{
case '+':
temp=data+Double.parseDouble(tf.getText());
tf.setText(""+temp);
break;
case '-':
temp=data-Double.parseDouble(tf.getText());
tf.setText(""+temp);
break;
case '*':
temp=data*Double.parseDouble(tf.getText());
tf.setText(""+temp);
break;
case '/':
temp=data/Double.parseDouble(tf.getText());
tf.setText(""+temp);
break;
}
}
//*********** For + operator ************
if(o== but[15])
{ bolOp=true;
data=Double.parseDouble(tf.getText());
chOp='+';
}
if(o== but[16])
{
tf.setText(null);
}
}//*****End actionPerformed() Method
class MyEvent extends MouseAdapter
{
public void mouseEntered(MouseEvent me)
{
int x=me.getX();
int y=me.getY();
if((x>=130 && x<=190) || (y>=80 && y<=50))
{ img=getImage(getCodeBase(),"bClear.gif");
//System.out.println("Entered"+me.getX()+" "+me.getY());
repaint();
}
else
{ img=getImage(getCodeBase(),"bClear2.gif");
repaint();
}

}
public void mouseExited(MouseEvent me)
{
System.out.println("Leaved");
}
}
}//*****End of Calculator Class
--------------------------------------------------------------------