Saturday, 10 August 2013

program to implement calculator applet in java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
/*<applet code="calculators"width=400 height=300>
</applet>*/

public class calculators extends Applet implements ActionListener
{
TextField tf ;
Button b[ ]=new Button[30];
Panel p;
double op1,op2,res,m;
String old,nw,s;
int opr,x,i,n;

public void init( )
{
                setBackground(Color.pink);
                setForeground(Color.blue);

         m=0;
tf=new TextField(10);
tf.setText(" ");
setLayout(new BorderLayout());
add(tf,BorderLayout.NORTH);
p=new Panel( );
for(int i=0;i<10;i++)
{
b[i]=new Button("  "+i);
b[i].addActionListener(this);
p.add(b[i]);
}
b[10]=new Button(".");
b[10].addActionListener(this);
p.add(b[10]);

b[11]=new Button("+");
b[11].addActionListener(this);
p.add(b[11]);

b[12]=new Button("-");
b[12].addActionListener(this);
p.add(b[12]);

b[13]=new Button("*");
b[13].addActionListener(this);
p.add(b[13]);

b[14]=new Button("/");
b[14].addActionListener(this);
p.add(b[14]);

b[15]=new Button("sqrt");
b[15].addActionListener(this);
p.add(b[15]);

       

b[16]=new Button("M+");
b[16].addActionListener(this);
p.add(b[16]);

b[17]=new Button("M-");
b[17].addActionListener(this);
p.add(b[17]);

b[18]=new Button("MC");
b[18].addActionListener(this);
p.add(b[18]);

b[19]=new Button("MR");
b[19].addActionListener(this);
p.add(b[19]);

b[20]=new Button("<-");
b[20].addActionListener(this);
p.add(b[20]);

b[21]=new Button("CE");
b[21].addActionListener(this);
p.add(b[21]);

b[22]=new Button("CLEAR");
b[22].addActionListener(this);
p.add(b[22]);
               
                 b[23]=new Button("cbrt");
b[23].addActionListener(this);
p.add(b[23]);

                b[24]=new Button("sin");
b[24].addActionListener(this);
p.add(b[24]);

                b[25]=new Button("cos");
b[25].addActionListener(this);
p.add(b[25]);


                b[26]=new Button("tan");
b[26].addActionListener(this);
p.add(b[26]);
               

                b[27]=new Button("=");
b[27].addActionListener(this);
p.add(b[27]);

add(p,BorderLayout.CENTER);

       

}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource( )==b[0])
{
old=tf.getText( );
if(!old.equals(" "))
{
nw=old+0;
tf.setText(nw);
}}

else if(ae.getSource( )==b[1])
{
old=tf.getText( );
nw=old+"1";
tf.setText(nw);
}
else if(ae.getSource( )==b[2])
{
old=tf.getText( );
nw=old+"2";
tf.setText(nw);
}
else if(ae.getSource( )==b[3])
{
old=tf.getText( );
nw=old+"3";
tf.setText(nw);
}
else if(ae.getSource( )==b[4])
{
old=tf.getText( );
nw=old+"4";
tf.setText(nw);
}
else if(ae.getSource( )==b[5])
{
old=tf.getText( );
nw=old+"5";
tf.setText(nw);
}
else if(ae.getSource( )==b[6])
{
old=tf.getText( );
nw=old+"6";
tf.setText(nw);
}
else if(ae.getSource( )==b[7])
{
old=tf.getText( );
nw=old+"7";
tf.setText(nw);
}
else if(ae.getSource( )==b[8])
{
old=tf.getText( );
nw=old+"8";
tf.setText(nw);
}
else if(ae.getSource( )==b[9])
{
old=tf.getText( );
nw=old+"9";
tf.setText(nw);
}
else if(ae.getSource( )==b[10])
{
old=tf.getText( );
if(!old.equals(" "))
{
nw=old+".";
tf.setText(nw);
}
else
{
nw="0.";
tf.setText(nw);
}
}
else if(ae.getSource( )==b[11])
{
opr=1;
op1=Double.parseDouble(tf.getText( ));
tf.setText(" ");
}

else if(ae.getSource( )==b[12])
{
opr=2;
old=tf.getText( );
op1=Double.parseDouble(old);
tf.setText(" ");
}

else if(ae.getSource( )==b[13])
{
opr=3;
old=tf.getText( );
op1=Double.parseDouble(old);
tf.setText(" ");

}

else if(ae.getSource( )==b[14])
{
opr=4;
old=tf.getText( );
op1=Double.parseDouble(old);
tf.setText(" ");

}



else if(ae.getSource( )==b[15])
{
old=tf.getText( );
op1=Double.parseDouble(old);
res=Math.sqrt(op1);
tf.setText(" "+res);

}
                else if(ae.getSource( )==b[23])
{
old=tf.getText( );
op1=Double.parseDouble(old);
res=Math.cbrt(op1);
tf.setText(" "+res);

}
                else if(ae.getSource( )==b[24])
{
old=tf.getText( );
op1=Double.parseDouble(old);
res=Math.sin(op1);
tf.setText(" "+res);

}
                else if(ae.getSource( )==b[25])
{
old=tf.getText( );
op1=Double.parseDouble(old);
res=Math.cos(op1);
tf.setText(" "+res);

}
                else if(ae.getSource( )==b[26])
{
old=tf.getText( );
op1=Double.parseDouble(old);
res=Math.tan(op1);
tf.setText(" "+res);

}

else if(ae.getSource( )==b[27])
{
old=tf.getText( );
op2=Double.parseDouble(old);
tf.setText(" ");
switch(opr)
{
case 1:
{
res=op1+op2;
break;
}
case 2:
{
res=op1-op2;
break;
}
case 3:
{
res=op1*op2;
break;
}

                case 4:
{
res=op1/op2;
break;
}
               
}
x=(int)res;
if((res-x)==0)
{
tf.setText(" "+x);
}
else
{
tf.setText(" "+res);
}
}
else if(ae.getSource( )==b[17])
{
old=tf.getText( );
m=m+Double.parseDouble(old);
tf.setText(" "+m);

}

else if(ae.getSource( )==b[18])
{
old=tf.getText( );
m=m-Double.parseDouble(old);
tf.setText(" "+m);
  }
else if(ae.getSource( )==b[19])
{
m=0;
tf.setText(" "+m);
}

else if(ae.getSource( )==b[20])
{
tf.setText(" "+m);
}
else if(ae.getSource( )==b[21])
{
s=tf.getText();
n=s.length();
s=s.substring(0,n-1);
tf.setText(s);
}
else if(ae.getSource( )==b[22])
{
op2=0;
tf.setText(" ");
}
else if(ae.getSource( )==b[23])
{
opr=0;
op1=0;
op2=0;
tf.setText(" ");

}

}
}

producer and cosumer problem in java

simple program for producer consumer problem:


we have three programs to implement producer consumer problem
 first program for producer:

public class producer implements Runnable
{
   private Thread t;
   private Queue q1;
   public producer(Queue c)
   {
      q1=c;
      t=new Thread(this,"producer");
      t.start();
  }
public void run()
{
   int i=0;
   while(true)
    {
      q1.put(i);
      i++;
     System.out.println("inserted element" +i);
try
 {
   Thread.sleep(1000);
 }
catch(Exception e)
{
  System.out.println("Exception " +e);
}
}
}
}



second program for Consumer

public class consumer implements Runnable
  private Thread t;
  private Queue q1;
public consumer(Queue c)
{
q1=c;
t=new Thread(this,"consumer");
t.start();
}
public void run()
{
int x;
while(true)
{
x=q1.get();
System.out.println("deleted element" +x);
try
{
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println("exception" +e);
}
}
}
}




third program for Queue

public class Queue
{
 private int f;
 private int r;
 private int q[]=new int[10];
 private boolean empty;
 private boolean full;
 
public Queue()
{
 f=-1;
  r=-1;
   empty=true;
   full=false;
}
synchronized public void put(int x)
  { 
    try 
        {
          if(full)
            
              wait();
            
           
              
                r=(r+1)%10;
                q[r]=x;
                empty=false;
               
               if(f==-1)
               {      
                  f=0;
                  notify();
                  
               }
          }
     catch(Exception e)
              {
                 System.out.println("exception" +e);
             }
     }
synchronized public int get()
{
  try
     {
       if(empty)

          wait();
  
     

   int x;
        x=q[f];      
        if(f==r)
        {
          empty=true;
           f=r=-1;
         }
       else
           {
             f=(f+1)%10;
}
             full=false;
              notify();
            return x;
            }        
 catch(Exception e)
      {             
       System.out.println("exception" +e);
       return -1;
     }
}

}



save all program in same drive and complie

how to create notepad using java?

Simple java program
.
we need three program

first main program called NOTEPAD:
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Notepad extends Frame implements ActionListener
{
MenuBar mb;
Menu file,edit;
MenuItem ne,open,save,quit,copy,cut,paste,find,replace;
FileDialog fd;
String sel;
TextArea ta;
MyDialog p;
public Notepad()
{
setTitle("untitled");
setSize(500,500);
setLayout(new BorderLayout());
mb=new MenuBar();
file=new Menu("File");
ne=new MenuItem("new");
ne.addActionListener(this);
open=new MenuItem("open");
open.addActionListener(this);
save=new MenuItem("save");
save.addActionListener(this);
quit=new MenuItem("quit");
quit.addActionListener(this);
file.add(ne);
file.add(open);
file.add(save);
file.add(quit);
edit=new Menu("Edit");
cut=new MenuItem("cut");
cut.addActionListener(this);
copy=new MenuItem("copy");
copy.addActionListener(this);
paste=new MenuItem("paste");
paste.addActionListener(this);
find=new MenuItem("find");
find.addActionListener(this);
replace=new MenuItem("replace");
replace.addActionListener(this);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
edit.add(replace);
mb.add(file);
mb.add(edit);
setMenuBar(mb);
ta=new TextArea(300,300);
add (ta,BorderLayout.CENTER);
addWindowListener(new myWindowAdapter(this));
}
public static void main(String args[])
{
Notepad np=new Notepad();
np.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
   if(ae.getSource()==ne)
  {
   this.setTitle("untitled");
   ta.setText("");
   ta.requestFocus();
}
else if(ae.getSource()==open)
{
fd=new FileDialog(this,"open",FileDialog.LOAD);
fd.setVisible(true);
String filename=fd.getFile();
String dir=fd.getDirectory();
try
{
FileInputStream fin=new FileInputStream(dir+filename);
int x;
while((x=fin.read())!=-1)
{
ta.append(""+(char)x);
}
fin.close();
}
catch(FileNotFoundException e)
{
System.out.println("file doesnt exist");
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
else if(ae.getSource()==save)
{
fd=new FileDialog(this,"save As",FileDialog.SAVE);
fd.setVisible(true);
String filename=fd.getFile();
String dir=fd.getDirectory();
try
{
FileOutputStream fout=new FileOutputStream(dir+filename);
String text=ta.getText();
fout.write(text.getBytes());
fout.close();
}
catch(FileNotFoundException e)
{
System.out.println("file cannot be saved");
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
else if(ae.getSource()==quit)
{
this.setVisible(false);
p.dispose();
System.exit(0);
}

else if(ae.getSource()==cut)
{
sel =ta.getSelectedText();
int start=ta.getSelectionStart();
String text=ta.getText();
String prefix=text.substring(0,start);
String suffix=text.substring(start+sel.length());
ta.setText(prefix+suffix);
}
else if(ae.getSource()==copy)
{
sel=ta.getSelectedText();
}
else if(ae.getSource()==paste)
{

int start=ta.getSelectionStart();
String text=ta.getText();
String prefix=text.substring(0,start);
String suffix=text.substring(start);
ta.setText(prefix+suffix+sel);
}
else if(ae.getSource()==find)
{
if(p==null||!(p.getTitle().equals("Find")))
p=new MyDialog(this,"Find");
else p.setVisible(true);
}
else if(ae.getSource()==replace)
{
if(p==null||!(p.getTitle().equals("Replace")))
p=new MyDialog(this,"Replace");
else
 p.setVisible(true);
}
}}




second program for WINDOWS ADAPTER:


import java.awt.event.*;
public class myWindowAdapter extends WindowAdapter
{
Notepad np;
public myWindowAdapter(Notepad n)
{
np=n;
}
public void windowClosing(WindowEvent we)
{
np.setVisible(false);
if(np.p!=null)
np.p.dispose();
System.exit(0);
}
}


third program for DAILOG BOX


import java.awt.event.*;
import java.awt.*;
public class MyDialog extends Dialog implements ActionListener
{
TextField  findText,replaceText;
Notepad np;
Button find,findnext,replace,replaceall,close;
Panel p1,p2;
String ft;
String rw;
int st=0;
public MyDialog(Notepad n,String title)
{
super(n,title,false);
setLayout(new BorderLayout());
np=n;
p1=new Panel();
p2=new Panel();
findText=new TextField(20);
replaceText=new TextField(20);
p1.add(new Label ("Find what"));
p1.add(findText);
find=new Button("find");
find.addActionListener(this);
p2.add(find);
findnext=new Button("findnext");
findnext.addActionListener(this);
p2.add(findnext);
if(title.equals("Replace"))
{
p1.add(new Label("replace with"));
p1.add(replaceText);
replace=new Button("replace");
replace.addActionListener(this);
p2.add(replace);
replaceall=new Button("replaceall");
replaceall.addActionListener(this);
p2.add(replaceall);
}
close=new Button("close"); 
close.addActionListener(this);
p2.add(close);
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
setSize(350,150);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
}
});
}
public void actionPerformed(ActionEvent ae)
{
     if(ae.getSource()==find)
     {
        ft=findText.getText();
         String text=np.ta.getText();
        st=text.indexOf(ft);
        if(st!=-1)
         {
              np.ta.setSelectionStart(st);
               np.ta.setSelectionEnd(st+ft.length());
                np.ta.requestFocus();
          }
        else
              {
                    final Dialog d=new Dialog(this,"Notepad",true);
                   d.add(new Label("string not found"),BorderLayout.CENTER);
                   Panel p=new Panel();
                   Button ok=new Button("ok");
                    p.add(ok);
                     ok.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae)
          {
          d.dispose();
          }
          });
        d.addWindowListener(new WindowAdapter()
        {
        public void windowClosing(WindowEvent we)
        {
        d.dispose();
        }
       });
                d.add(p,BorderLayout.SOUTH);
                 d.setSize(175,100);
                 d.setVisible(true);
            }
    }
          else if(ae.getSource()==findnext)
           {
                  ft=findText.getText();
                     String text=np.ta.getText();
                     st=text.indexOf(ft,st+ft.length());
                       if(st!=-1)
                        {
                           np.ta.setSelectionStart(st);
                          np.ta.setSelectionEnd(st+ft.length());
                          np.ta.requestFocus();
                        }
               else
                 {
                      final Dialog d=new Dialog(this,"Notepad",true);
                      d.add(new Label("string not found"),BorderLayout.CENTER);
                       Panel p=new Panel();
                           Button ok=new Button("ok");
                           p.add(ok);
                                      ok.addActionListener(new ActionListener(){
                                       public void actionPerformed(ActionEvent ae)
                                      {
                                       d.dispose();
                                       }
                                       });
                                        d.addWindowListener(new WindowAdapter()
                                      {
                                              public void windowClosing(WindowEvent we)
                                      {
                                      d.dispose();
                                      }
                                     });
                                     d.add(p,BorderLayout.SOUTH);
                                    d.setSize(175,100);
                                      d.setVisible(true);
                         }

             else if(ae.getSource()==replaceall)
             {
                         ft=findText.getText();
                       rw=replaceText.getText();
                         st=0;
                      int count=0;
                  while(st!=-1)
                      {
                         String text=np.ta.getText();
                          st=text.indexOf(ft);
                     if(st!=-1)
                          {
                            np.ta.replaceRange(rw,st,st+ft.length());
                               np.ta.requestFocus();
                              count++;
                           }
                else
                   {
                      final Dialog d=new Dialog(this,"Notepad",true);
                         d.add(new Label("string not found"),BorderLayout.CENTER);
                          Panel p=new Panel();
                        Button ok=new Button("ok");
                      p.add(ok);
                  ok.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ae)
                         {
                            d.dispose();
                         }
                     });
                        d.addWindowListener(new WindowAdapter()
                           {
                                public void windowClosing(WindowEvent we)
                             {
                       d.dispose();
                       }
                       });
                        d.add(p,BorderLayout.SOUTH);
                          d.setSize(175,100);
                            d.setVisible(true);
                    }
              }
         }
}
}




COMPILE:
save all the three file in same drive

sample output