Sunday, May 26, 2013

Addition Of Numbers

/*Example of class for addition of the two numbers*/

import java.awt.*;
import java.awt.event.*;
class as extends Frame implements ActionListener
  {
        TextField t1,t2,t3;    
         Button b1,b2,b3;

            public as()
              {
                     t1=new TextField(10);
         t2=new TextField(10);
                     t3=new TextField(10);
                      b1=new Button("ADD");
          b2=new Button("SUB");
          b3=new Button("EXIT");

              setLayout(new FlowLayout());
             
              add(b1); add(t1); add(b2);       add(t2);  add(t3);   add(b3);
             
                b1.addActionListener(this);
    b2.addActionListener(this);
                b3.addActionListener(this);
 
          }

            public static void main(String args[])throws Exception
   {
   
      as a=new as();

                  a.setSize(200,300);
                   a.setVisible(true);
             }

        public void actionPerformed(ActionEvent ae)
  {
 
                 int val1=Integer.parseInt(t1.getText());
     int val2=Integer.parseInt(t2.getText());
     int res;

if(ae.getSource()==b1)
   {
                    res=val1+val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b2)
   {
                    res=val1-val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b3)
   {
                    System.exit(0);
               }
      }
  }
    

Simple Applet Program

Following is a Simple applet program inn java.



import java.awt.*;
import java.applet.*;

public class app2 extends Applet
  {
     public void paint(Graphics g)
       {
             g.drawString("MY FIRST APPLET",30,30);
       }
  }

Applet in Java

HTML FILE CODE:

Create a file name sample.html and write the following code in it.

<applet code="app1"width=600 height=600></applet>


Then create a java file and write the following code in it.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class app1 extends Applet implements ActionListener
 {
   
    Button b;
    TextField t;

      public void Init()
{
   b=new Button("CLICK:");
                t=new TextField(20);

          setLayout(new FlowLayout());

             add(b);    add(t);

              b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent ae)
               {
                   if(ae.getSource()==b)
                      {
                          t.setText("YOUR NAME HERE:");
                      }
               }

         
     }

 
              

Simple Event Program In Java

/*EVENT PROGRAM IN JAVA*/

import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class aaa extends Frame implements ActionListener
 {

     Button b1,b2,b3,b4,b5,b6;
     Label a,b,c,d,r,t;
     TextField a1,a2,a3,a4,a5,a6,a7;
       int a1;

           public aaa()
{
   b1=new Button("SUBMIT:");
   b2=new Button("RESET:");
   b3=new Button("EXIT:");
   b4=new Button("TOTAL:");
   b5=new Button("NEW:");
   b6=new Button("OLD:");

   a=new Label("NAME:");
   b=new Label("AGE:");
   c=new Label("GENDER:");
   d=new Label("EMAIL_ID:");
   r=new Label("MOBILE NUMBER:");
   t=new Label("NATIONALITY:");

   a1=new TextField(20);
   a2=new TextField(20);
   a3=new TextField(20);
   a4=new TextField(20);
   a5=new TextField(20);
   a6=new TextField(20);
   a7=new TextField(20);

    setLayout(new FlowLayout());
                  add(b5);  add(b6);
   
                   

                b1.addActionListener(this);
                b2.addActionListener(this);
   b3.addActionListener(this);
   b4.addActionListener(this);
   b5.addActionListener(this);
   b6.addActionListener(this);

          }

public aaa(int a1)
{
                 if(a1==0)
          {
            setLayout(new FlowLayout());
                           add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);
          }
 }

          public void actionPerformed(ActionEvent ae)
{
  if(ae.getSource()==b5)
     {
        aaa(0);
     }

   if(ae.getSource()==b6)
      {
       
      setLayout(new FlowLayout());
            add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);  add(b4);
       }
             
                  if(ae.getSource()==b3)
       {
                         System.exit(0);
       }
             

            }

public static void main(String args[])throws Exception
 {

aaa g=new aaa();

                       g.setSize(300,600);

          g.setVisible(true);

             }

      }

       
 

Monday, October 22, 2012

Search Pattern in Java

Program to find a specific pattern from a given text or file is stated as below.



import java.io.*;

class srchPattern
{
  public static void main(String args[])
  {
    if(args.length == 0 || args.length == 1)
    {
      System.out.println("Insufficient arguments...");
      return;
    }

    File f = new File(args[1]);

    try
    {
      BufferedReader i = new BufferedReader(new FileReader(args[1]));

      String line;
      int ct = 0;

      if(f.exists() && f.isFile())
      {
        while((line = i.readLine()) != null)
        {
          if((line.indexOf(args[0])) != -1)
          {
            ct++;
          }
          System.out.println(line + " ");
        }

        if(ct == 0)
        {
          System.out.println("Pattern not Found");
        }
        else
        {
          System.out.println("Pattern found in the file...");
          System.out.println("Number of Occurances of the word " + args[0] + " in te file " + args[1] + " is " + ct);
        }
      }
    }
    catch(Exception e){}
  }
}

Word,character count in Java

Program  to count the number of occurrences of a word in a file is given as below:


import java.io.*;

class wordcnt
{
  public static void main(String args[])
  {
    int i = 0,cnt = 0;
    try
    {
      FileInputStream fin = new FileInputStream("d:/java/wordcnt.java");
      String word;
      char ch , charr[];
      charr = new char[100];
      String w = new String("word");
      while(( ch = (char)fin.read()) != -1)
      {
        if( ch != ' ' && ch != '\n')
        {
           charr[i] = ch ;
           i++;
        }
        else
        {
           word = new String(charr,0,i);
           if(word.equals(w))
              cnt++;   
           if( ch == '\n')
              System.out.println( word );
           else
              System.out.print( word + " ");
           i = 0;
        }
     };
     fin.close();
    }
    catch(Exception ie)
    {
    }
    System.out.println("\n\nWord Count : " + cnt);
  }
}

Copy file in Java

Program to copy a file content in java is as follows:

 import java.io.*;

class FileCopy
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fin = new FileInputStream("d:/java/stk.java");
    FileOutputStream fout = new FileOutputStream("d:/java/txtfile.txt");
    int b;

    while(true)
    {
      b = fin.read();
      if(b == -1)
      {
        break;
      }
      System.out.print((char)b);
      fout.write(b);
    }

    fin.close();
    fout.close();
  }
}

Polynomial operations in Java

Program for Ploynomial expression in java is as follows:


import java.lang.*;
import java.io.*;
class term
{
  int coeff,exp;
}
class poly
{
    term pl[] = new term[10];
    int n;
    poly()throws Exception
    {
        poly p[]=new poly[10];
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the no. of terms: ");
        n=Integer.parseInt(br.readLine());
    }
    public void getPoly()throws Exception
    {
        poly p[]=new poly[10];
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the base & exponent:");
        for(int i=0;i<n;i++)
        {
            p[i].coeff=Integer.parseInt(br.readLine());
            p[i].exp=Integer.parseInt(br.readLine());
        }
    }   
    public void putPoly()
    {
        poly p[]=new poly[10];
        System.out.println("Polynomial is : ");
        for(int i=0;i<n;i++)
        {
            System.out.println(" "+p[i].coeff+"x^"+p[i].exp);
        }
    }
    public void evalPoly()throws exception
    {
        poly p[]=new poly[10];
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int x,ans=0;
        System.out.println("Enter the value for x:");
         x=Integer.parseInt(br.readLine());   
        for(int i=0;i<n;i++)
        {
            ans=ans+p[i].coeff*pow(x,p[i].exp);
        }
        System.out.println("Ans : "+ans);
    }
    public static void main(String args[])throws Exception
    {
        poly p1=new poly();
        poly p2=new poly();
        p1.getPoly();
        p2.getPoly();
        p1.putPoly();
        p2.putPoly();
        p1.evalPoly();
       
    }   
}

Matrix in Java

Program for matrix in java is as follows:


import java.io.*;

class matrix
{
  int mat[][],r,c;

  matrix()
  {
     mat = new int[10][10];
  }
  public void getMat() throws Exception
  {
   // mat = new int[10][10];
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("\nEnter the Dim of the matrix : ");    
    r = Integer.parseInt(br.readLine());
    c = Integer.parseInt(br.readLine());

    System.out.println("\nEnter the matrix elements : ");    
    for(int i = 0 ; i < r ; i++)
    {
      for(int j = 0 ; j < c ; j++)
      {
        mat[i][j] = Integer.parseInt(br.readLine());
      }
    }
  }

  public void addMat(matrix m1,matrix m2)
  {
     r = m1.r;
     c = m1.c;

     for(int i = 0 ; i < r ; i++)
    {
       for(int j = 0 ; j < c ; j++)
       {
          mat[i][j] = m1.mat[i][j] + m2.mat[i][j];
       }
    }
  }

  public void putMat()
  {
    System.out.println("\nMatrix is : \n");
    for(int i = 0 ; i < r ; i++)
    {
      for(int j = 0 ; j < c ; j++)
      {
        System.out.print(" " + mat[i][j]);
      }
      System.out.println("");
    }
  }

  public static void main(String args[]) throws Exception
  {
    matrix m1 = new matrix();
    matrix m2 = new matrix();
    matrix m3 = new matrix();

    m1.getMat();
    m2.getMat();

    m1.putMat();
    m2.putMat();

    m3.addMat(m1,m2);
    m3.putMat();
  } 
}

Linear Search in Java

Below is simple program in java which shows the linear search.


import java.io.*;

class LinearSearch
{
  int ar[];
  int ele,n;

  public void setData() throws Exception
  {
     ar = new int[10];
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the size for array : ");    
    n = Integer.parseInt(br.readLine());

    System.out.println("Enter the data for array : ");    
    for(int i = 0 ; i < n ; i++)
    {
      ar[i] = Integer.parseInt(br.readLine());
    }
    
    System.out.println("Enter the data to be searched : ");
    ele = Integer.parseInt(br.readLine());
  }

  public void putData()
  {
    System.out.println("The array is : ");
    for(int i = 0 ; i < n ; i++)
    {
      System.out.println(" " + ar[i]);
    }
  }

  public void LinSrch()
  {
    int i;
   
    for(i = 0 ; i < n ; i++)
    {
      if(ar[i] == ele)
      {
        break;
      }
    }
    if(i == ar.length)
    {
      System.out.println("\nElement not found");
    }
    else
    {
      System.out.println("\nElement found at " + i);
    }
  }

  public static void main(String args[]) throws Exception
  {
    LinearSearch ln = new LinearSearch();
    ln.setData();
    ln.putData();
    ln.LinSrch();
  }

}

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...