Tuesday, 4 November 2014

PROGRAM FOR HAMMING CODE GENERATION AND DETECTION


/*PROGRAM FOR HAMMING CODE GENERATION AND DETECTION */


package program;
import java.util.*;
class code
{
    Scanner sc=new Scanner(System.in);
    int a[]=new int[10];
    int d[]=new int[10];
    int n,i,ch,j,x;
    double r,r1;

    void get()
    {
        System.out.println("\nEnter the Number of parity bits");
        n=sc.nextInt();
        r1=Math.pow(2,n);
        x=(int)r1;
    }
    void generate()
    {
        System.out.println("Enter the Four bits");
        for(i=0;i<4;i++)
            a[i]=sc.nextInt();
        d[7]=a[0];
        d[6]=a[1];
        d[5]=a[2];
        d[3]=a[3];
        d[1]=(d[3]+d[5]+d[7])%2;
        d[2]=(d[3]+d[6]+d[7])%2;
        d[4]=(d[5]+d[6]+d[7])%2;

    }
    void detect()
    {
              System.out.println("Enter the Seven bit Number");
              for(i=x-1;i>=1;i--)
              {
                  a[i]=sc.nextInt();
              }
        d[7]=a[7];
        d[6]=a[6];
        d[5]=a[5];
        d[3]=a[3];
        d[1]=(a[3]+a[5]+a[7])%2;
        d[2]=(a[3]+a[6]+a[7])%2;
        d[4]=(a[5]+a[6]+a[7])%2;

        for(i=0;i<n;i++)
        {
            r=Math.pow(2,i);
            j=(int)r;
            if(a[j]==d[j])
            {
                System.out.println("No Error at "+j+ "Position\n");
            }
           if(a[j]!=d[j])
            {
                System.out.println("The Error Occured at "+j+ "Position\n");

            }
        }
    }
    void display()
    {
        for(i=x-1;i>=1;i--)
        {
            System.out.print( d[i]);
        }
    }
}
public class Hamming
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int ch;
        code c=new code();
     do
       {
        System.out.println("\n1.Hamming code Generation\n2.Error Detection\n3.Exit\n");
        ch=sc.nextInt();
      
        switch(ch)
          {
           case 1:
                c.get();
                c.generate();
                c.display();
                break;
            case 2:
                c.get();
                c.detect();
                c.display();
                 break;
            case 3:

        }
     }while(ch!=3);
    }

}



No comments:

Post a Comment