Friday 23 August 2013

56. HAMMIMG CODE

CODE:
import java.util.*;
class Hamming
{
static Scanner sc = new Scanner(System.in);
int data[];
int parity[];
int code[];
int a[];
static int n,i,p;
void input()
{
System.out.println("Enter the no.of data bits:");
n=sc.nextInt();
data=new int[n];
System.out.println("Enter the data bits:");
for(i=n-1;i>=0;i--)
{
data[i]=sc.nextInt();
}
if(n==2 || n==3 || n==4)
p=3;
if(n==5 || n==6 || n==7 || n==8 || n==9 || n==10 || n==11)
p=4;
System.out.println("\nNo.of parity bits required = "+p);
parity=new int[p];
code=new int[n+p];
a=new int[n+p];
} void calculate()
{
for(i=0;i<p;i++)
{  parity[i]=0;
}
int j=0; int d=0; int x=0;
for(i=0;i<n+p;i++)
{
if(i==(Math.pow(2,x)-1))
{
a[i]=parity[j];
j++;
x++;
}else
{
a[i]=data[d];
d++;
}}
for(i=0;i<n+p;i++)
{  code[i]=a[i];
}  if((code[0]+code[2]+code[4]+code[6])%2==0)
code[0]=0;
else
code[0]=1;
if((code[1]+code[2]+code[5]+code[6])%2==0)
code[1]=0;
else
code[1]=1;
if((code[3]+code[4]+code[5]+code[6])%2==0)
code[3]=0;
else
code[3]=1;
System.out.println("\nThe Hamming Code is :");
for(i=(n+p)-1;i>=0;i--)
{  System.out.print(code[i]);
}
System.out.println();
}  void errorcheck()
{
System.out.println("\nEnter the Corrupted Hamming Code:");
for(i=(n+p)-1;i>=0;i--)
{  code[i]=sc.nextInt();
}
System.out.println("\nThe Corrupted Hamming Code:");
for(i=(n+p)-1;i>=0;i--)
{  System.out.print(code[i]);
}
System.out.println();
parity[0]=code[0];
parity[1]=code[1];
parity[2]=code[3];
if((((code[2]+code[4]+code[6])%2==0 && parity[0]==0)||(((code[2]+code[4]+code[6])%2)!=0) && parity[0]==1))
  parity[0]=0;
else
  parity[0]=1;
if((((code[2]+code[5]+code[6])%2==0 && parity[1]==0)||(((code[2]+code[5]+code[6])%2)!=0) && parity[1]==1))
 parity[1]=0;
else
 parity[1]=1;
if((((code[4]+code[5]+code[6])%2==0 && parity[2]==0)||(((code[4]+code[5]+code[6])%2)!=0) && parity[2]==1 ))
 parity[2]=0;
else
 parity[2]=1;
int word=0;
for(i=2;i>=0;i--)
{   word=word+(parity[i]*(int)Math.pow(2,i));                                 
 }           if(word==0)
System.out.println("\nThe data code is correctly received");   
else
System.out.println("\nThe bit no. "+word+" is wrongly received");
 if(code[word-1]==0)
code[word-1]=1;
else
code[word-1]=0;
System.out.println("\nThe Corrected Hamming Code:");
for(i=(n+p)-1;i>=0;i--)
{  System.out.print(code[i]);
}  System.out.println();
}
public static void main(String arg[])
{  Hamming h=new Hamming();
h.input();
h.calculate();
h.errorcheck();
}
}
OUTPUT:
Enter the no.of data bits:
4
Enter the data bits:
1
0
1
1
No. of parity bits required = 3
The Hamming Code is :
1010101
Enter the Corrupted Hamming Code:
1
0
1
0
1
0
0
The Corrupted Hamming Code:
1010100
The bit no. 1 is wrongly received
The Corrected Hamming Code:
1010101


55. FRAMING TECHNIQUES

Code:
import java.io.*;
class Framing
{
            public static void main(String args[]) throws IOException
            {
                        String data;
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.print("\nEnter the Data Stream: ");
                        data=br.readLine();
                        String str;
                        int select;
                        System.out.print("\nThe various Framing techniques are:\n1) Character Count\n2) Byte Stuffing\n3) Bit Stuffing\n4) Physical Data Violation\n\nEnter the number for the corresponding Framing technique: ");
                        str=br.readLine();
                        select=Integer.parseInt(str);
                        switch(select)
                        {
                                    case 1: System.out.print("\nThe Frame after Processing is: "+CC(data));
                                                break;
                                    case 2: System.out.print("\nEnter the Flag Stream: ");
                                                String flag;
                                                flag=br.readLine();
                                                System.out.print("\nThe Frame after processing: "+Byte(data,flag));
                                                break;
                                    case 3: flag="01111110";
                                                System.out.print("\nThe Flag Stream is: "+flag);
                                                System.out.print("\n\nThe Frame after processing: "+Bit(data,flag));
                                                break;
                                    case 4: System.out.print("\nThe Frame after processing: "+PDV(data));
                                                break;
                        }
            }
            public static String CC(String input)
            {
                        int len=input.length();
                        String head=""+len;
                        String output=head+input;
                        return output;
            }
            public static String Byte(String input,String flag)
            {
                        int len=input.length();
                        int lenf=flag.length();
                        char na[]=new char [2*len];
                        int ni=0;
                        int fromindex=0;
                        for(int i=0;i<len;i++)
                        {
                                    fromindex=input.indexOf(flag,i);
                                    if(fromindex==i)
                                    {
                                                na[ni]='e';
                                                ni++;
                                                for(int j=0;j<lenf;j++,i++)
                                                {
                                                            na[ni]=flag.charAt(j);
                                                            ni++;
                                                }
                                    }
                                    else if(i<fromindex)
                                    {
                                                for(int l=i;l<fromindex;l++,i++)
                                                {
                                                            na[ni]=input.charAt(l);
                                                            ni++;
                                                }
                                    }
                                    else if(fromindex==-1)
                                    {
                                                for(int k=i;k<len;k++,i++)
                                                {
                                                            na[ni]=input.charAt(k);
                                                            ni++;
                                                }
                                                break;
                                    }
                                    i=i-1;
                        }
                        String output=new String (na,0,ni);
                        return flag+" "+output+" "+flag;
            }
            public static String Bit(String input,String flag)
            {
                        int count=0;
                        int len=input.length();
                        int maxs=len/5;
                        int ni=0;
                        char na[]=new char [len+maxs];
                        for(int i=0;i<len;i++)
                        {
                                    if(input.charAt(i)=='1')
                                    {
                                                count++;
                                                if(count==5)
                                                {
                                                            count=0;
                                                            na[ni]=input.charAt(i);
                                                            ni++;
                                                            na[ni]='0';
                                                            ni++;
                                                            continue;
                                                }
                                    }
                                    else
                                    {          
                                                count=0;
                                    }
                                    na[ni]=input.charAt(i);
                                    ni++;
                        }
                        String output=new String (na,0,ni);
                        return flag+" "+output+" "+flag;
                       
            }
            public static String PDV(String input)
            {
                        int len=input.length();
                        char na[]=new char [len];
                        int ni=0;
                        for(int i=0;i<len;i++)
                        {
                                    if(input.charAt(i)=='0')
                                    {
                                                na[ni]='L';
                                                ni++;
                                    }
                                    else
                                    {
                                                na[ni]='H';
                                                ni++;
                                    }
                        }
                        String output=new String (na);
                        return output;
            }
}

OUTPUT:
Enter the Data Stream: 0110
The various Framing techniques are:
1) Character Count
2) Byte Stuffing
3) Bit Stuffing
4) Physical Data Violation
Enter the number for the corresponding Framing technique: 1
The Frame after Processing is: 40110


Enter the Data Stream: 111111100011001010111111
The various Framing techniques are:
1) Character Count
2) Byte Stuffing
3) Bit Stuffing
4) Physical Data Violation
Enter the number for the corresponding Framing technique: 2
Enter the Flag Stream: 111
The Frame after processing: 111 e111e111100011001010e111e111 111

Enter the Data Stream: 1111111111111111
The various Framing techniques are:
1) Character Count
2) Byte Stuffing
3) Bit Stuffing
4) Physical Data Violation
Enter the number for the corresponding Framing technique: 3
The Flag Stream is: 01111110
The Frame after processing: 01111110 1111101111101111101 01111110

Enter the Data Stream: 1111100
The various Framing techniques are:
1) Character Count
2) Byte Stuffing
3) Bit Stuffing
4) Physical Data Violation
Enter the number for the corresponding Framing technique: 4
The Frame after processing: HHHHHLL





Need the code??
Download Link= https://www.dropbox.com/s/t099t9vrdromflb/Framing.docx