/*Package declaration*/
package Hamming_Code;
/*Importing packets*/
import javax.swing.*;
public class Hamming_Code
{
public static void main(String args[])
{
Object[] options = {"Transmitting End", "Receiving End"};
int selection = JOptionPane.showOptionDialog(null,"Select the Terminal",null, JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE, null,options, options[0]);
if (selection==JOptionPane.YES_NO_OPTION)
{
TransmittingEnd();
}
else
{
ReceivingEnd();
}
}
static void TransmittingEnd()//For tranmitting end operations
{
Transmitting_End T=new Transmitting_End();
T.main();
}
static void ReceivingEnd()//For receiving end operations
{
Receiving_End R=new Receiving_End();
R.main();
}
}
class Transmitting_End
{
/*Declaration of variables*/
StringBuffer Take_Input=new StringBuffer("ENTER THE DATAWORD");//Statement to display
StringBuffer Message=new StringBuffer();//Message bits taken from user
StringBuffer Appended_Word=new StringBuffer();
StringBuffer Transmitted_Word=new StringBuffer();//Hamming code
int parity[];//Parity bits
int m,p,n,x=0;//m,p and denotes numbers of message bits,parity bits and total number of bits respectively.x is temperory variable
boolean valid;//Checking validity of input
void input() /*Taking input*/
{
do
{
Message.append(JOptionPane.showInputDialog(Take_Input));
m=Message.length();
for(int i=0;i<m;i++)
{
if(Message.charAt(i)!='1'&&Message.charAt(i)!='0')
{
valid=false;
break;
}
valid=true;
}
if(!valid)
{
Message.delete(0, m);
Take_Input.delete(0,Take_Input.length());
Take_Input.append("INVALID DATAWORD \nPLEASE ENTER A VALID DATAWORD ");
}
}
while(!valid);
}
void calculate()/*Calculating positions of databits in hamming code leaving pariry positions*/
{
for(int i=0;x<m;i++)
{
if(Math.pow(2,p)==(i+1))
{
p++;
Transmitted_Word.append('_');
}
else
{
Transmitted_Word.append(Message.charAt(x));
x++;
}
}
n=p+m;
Appended_Word.append(Transmitted_Word);
}
void appendParity()/*Appending parity bits to the databits*/
{
parity=new int[p];
x=0;
for(int i=0;i<p;i++)
{
for(int j=1;j<=n;j++)
{
if(convert(j).length()>i)
{
if(convert(j).charAt(convert(j).length()-i-1)=='1')
{
if(Transmitted_Word.charAt(j-1)=='1')
{
parity[i]++;
}
}
}
}
}
for(int i=0;i<p;i++)
{
parity[i]=parity[i]%2;
}
for(int i=0;i<n;i++)
{
if(Transmitted_Word.charAt(i)=='_')
{
Transmitted_Word.replace(i, i+1,parity[x]+"");
x++;
}
}
}
StringBuffer convert(int a)/*Function used to convert decimal to binary*/
{
StringBuffer s=new StringBuffer();
do
{
s.append(a%2);
a=a/2;
}
while(a!=0);
s.reverse();
return s;
}
void output()/*Displaying output*/
{
JOptionPane.showMessageDialog(null, "DATAWORD : "+Message+"\n"+"DATAWORD IN HAMMING FORMAT : "+Appended_Word+"\n"+"HAMMING CODE : "+Transmitted_Word);
}
void main()/* Calling all functions in sequence*/
{
input();
calculate();
appendParity();
output();
}
}
class Receiving_End
{
/*Declaration of variables*/
StringBuffer Take_Input=new StringBuffer("ENTER THE RECIEVED CODEWORD");//Statement to display
StringBuffer Message=new StringBuffer();//Isolated message bits
StringBuffer Recieved_Word=new StringBuffer();//Bits recieved from user
StringBuffer Corrected_Word=new StringBuffer();//Corrected hamming code
int parity[];//Parity bits
int m=0,p=0,n,x,error=0;//m,p and denotes numbers of message bits,parity bits and total number of bits respectively.x is temperory variable
boolean valid;//Chacking validity of input
void input()/*Taking input*/
{
do
{
valid=true;
Recieved_Word.append(JOptionPane.showInputDialog(Take_Input));
n=Recieved_Word.length();
for(int i=0;i<n;i++)
{
if(Recieved_Word.charAt(i)!='1'&&Recieved_Word.charAt(i)!='0')
{
valid=false;
break;
}
valid=true;
}
if(!valid)
{
Recieved_Word.delete(0, n);
Take_Input.delete(0,Take_Input.length());
Take_Input.append("INVALID CODEWORD \nPLEASE ENTER A VALID CODE WORD ");
}
}
while(!valid);
}
void calculate()/*Calculating number of parity bits and data bits*/
{
for(int i=0;i<n;i++)
{
if(Math.pow(2,p)==(i+1))
{
p++;
}
else
{
Message.append(Recieved_Word.charAt(i));
m++;
}
}
}
int check()/*Checking for errors*/
{
parity=new int[p];
for(int i=0;i<p;i++)
{
for(int j=1;j<=n;j++)
{
if(convert(j).length()>i)
{
if(convert(j).charAt(convert(j).length()-i-1)=='1')
{
if(Recieved_Word.charAt(j-1)=='1')
{
parity[i]++;
}
}
}
}
}
for(int i=0;i<p;i++)
{
parity[i]=parity[i]%2;
if(parity[i]!=0)
{
error++;
}
}
return error;
}
StringBuffer convert(int a)/*Function used to convert decimal to binary*/
{
StringBuffer s=new StringBuffer();
do
{
s.append(a%2);
a=a/2;
}
while(a!=0);
s.reverse();
return s;
}
int convertIntoDecimal(int a[],int length)/*Function used to convert binary to decimal*/
{
int dec=0;
for(int i=0;i<length;i++)
{
dec=dec+a[i]*(int)(Math.pow(2, length-1-i));
}
return dec;
}
void main()/* Calling all functions in sequence*/
{
input();
calculate();
int c=check();
if(c==0)
{
JOptionPane.showMessageDialog(null,"RECEIVED MESSAGE : "+Recieved_Word+"\nNO ERRORS DETECTED");
}
else
{
int temp[]=new int[p];
for(int i=0;i<p;i++)
{
temp[i]=parity[i];
}
for(int i=0;i<p;i++)
{
parity[i]=temp[p-1-i];
}
int position=convertIntoDecimal(parity,p);
JOptionPane.showMessageDialog(null,"ERROR DETECTED AT "+position);
Corrected_Word.append(Recieved_Word);
if(Corrected_Word.charAt(position-1)=='0')
{
Corrected_Word.replace(position-1,position,"1");
}
else
{
Corrected_Word.replace(position-1, position,"0");
}
JOptionPane.showMessageDialog(null,"RECEIVED CODE : "+Recieved_Word+"\nERROR DETECTED AT "+position+"\nCORRECTED CODE : "+Corrected_Word);
}
}
}
package Hamming_Code;
/*Importing packets*/
import javax.swing.*;
public class Hamming_Code
{
public static void main(String args[])
{
Object[] options = {"Transmitting End", "Receiving End"};
int selection = JOptionPane.showOptionDialog(null,"Select the Terminal",null, JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE, null,options, options[0]);
if (selection==JOptionPane.YES_NO_OPTION)
{
TransmittingEnd();
}
else
{
ReceivingEnd();
}
}
static void TransmittingEnd()//For tranmitting end operations
{
Transmitting_End T=new Transmitting_End();
T.main();
}
static void ReceivingEnd()//For receiving end operations
{
Receiving_End R=new Receiving_End();
R.main();
}
}
class Transmitting_End
{
/*Declaration of variables*/
StringBuffer Take_Input=new StringBuffer("ENTER THE DATAWORD");//Statement to display
StringBuffer Message=new StringBuffer();//Message bits taken from user
StringBuffer Appended_Word=new StringBuffer();
StringBuffer Transmitted_Word=new StringBuffer();//Hamming code
int parity[];//Parity bits
int m,p,n,x=0;//m,p and denotes numbers of message bits,parity bits and total number of bits respectively.x is temperory variable
boolean valid;//Checking validity of input
void input() /*Taking input*/
{
do
{
Message.append(JOptionPane.showInputDialog(Take_Input));
m=Message.length();
for(int i=0;i<m;i++)
{
if(Message.charAt(i)!='1'&&Message.charAt(i)!='0')
{
valid=false;
break;
}
valid=true;
}
if(!valid)
{
Message.delete(0, m);
Take_Input.delete(0,Take_Input.length());
Take_Input.append("INVALID DATAWORD \nPLEASE ENTER A VALID DATAWORD ");
}
}
while(!valid);
}
void calculate()/*Calculating positions of databits in hamming code leaving pariry positions*/
{
for(int i=0;x<m;i++)
{
if(Math.pow(2,p)==(i+1))
{
p++;
Transmitted_Word.append('_');
}
else
{
Transmitted_Word.append(Message.charAt(x));
x++;
}
}
n=p+m;
Appended_Word.append(Transmitted_Word);
}
void appendParity()/*Appending parity bits to the databits*/
{
parity=new int[p];
x=0;
for(int i=0;i<p;i++)
{
for(int j=1;j<=n;j++)
{
if(convert(j).length()>i)
{
if(convert(j).charAt(convert(j).length()-i-1)=='1')
{
if(Transmitted_Word.charAt(j-1)=='1')
{
parity[i]++;
}
}
}
}
}
for(int i=0;i<p;i++)
{
parity[i]=parity[i]%2;
}
for(int i=0;i<n;i++)
{
if(Transmitted_Word.charAt(i)=='_')
{
Transmitted_Word.replace(i, i+1,parity[x]+"");
x++;
}
}
}
StringBuffer convert(int a)/*Function used to convert decimal to binary*/
{
StringBuffer s=new StringBuffer();
do
{
s.append(a%2);
a=a/2;
}
while(a!=0);
s.reverse();
return s;
}
void output()/*Displaying output*/
{
JOptionPane.showMessageDialog(null, "DATAWORD : "+Message+"\n"+"DATAWORD IN HAMMING FORMAT : "+Appended_Word+"\n"+"HAMMING CODE : "+Transmitted_Word);
}
void main()/* Calling all functions in sequence*/
{
input();
calculate();
appendParity();
output();
}
}
class Receiving_End
{
/*Declaration of variables*/
StringBuffer Take_Input=new StringBuffer("ENTER THE RECIEVED CODEWORD");//Statement to display
StringBuffer Message=new StringBuffer();//Isolated message bits
StringBuffer Recieved_Word=new StringBuffer();//Bits recieved from user
StringBuffer Corrected_Word=new StringBuffer();//Corrected hamming code
int parity[];//Parity bits
int m=0,p=0,n,x,error=0;//m,p and denotes numbers of message bits,parity bits and total number of bits respectively.x is temperory variable
boolean valid;//Chacking validity of input
void input()/*Taking input*/
{
do
{
valid=true;
Recieved_Word.append(JOptionPane.showInputDialog(Take_Input));
n=Recieved_Word.length();
for(int i=0;i<n;i++)
{
if(Recieved_Word.charAt(i)!='1'&&Recieved_Word.charAt(i)!='0')
{
valid=false;
break;
}
valid=true;
}
if(!valid)
{
Recieved_Word.delete(0, n);
Take_Input.delete(0,Take_Input.length());
Take_Input.append("INVALID CODEWORD \nPLEASE ENTER A VALID CODE WORD ");
}
}
while(!valid);
}
void calculate()/*Calculating number of parity bits and data bits*/
{
for(int i=0;i<n;i++)
{
if(Math.pow(2,p)==(i+1))
{
p++;
}
else
{
Message.append(Recieved_Word.charAt(i));
m++;
}
}
}
int check()/*Checking for errors*/
{
parity=new int[p];
for(int i=0;i<p;i++)
{
for(int j=1;j<=n;j++)
{
if(convert(j).length()>i)
{
if(convert(j).charAt(convert(j).length()-i-1)=='1')
{
if(Recieved_Word.charAt(j-1)=='1')
{
parity[i]++;
}
}
}
}
}
for(int i=0;i<p;i++)
{
parity[i]=parity[i]%2;
if(parity[i]!=0)
{
error++;
}
}
return error;
}
StringBuffer convert(int a)/*Function used to convert decimal to binary*/
{
StringBuffer s=new StringBuffer();
do
{
s.append(a%2);
a=a/2;
}
while(a!=0);
s.reverse();
return s;
}
int convertIntoDecimal(int a[],int length)/*Function used to convert binary to decimal*/
{
int dec=0;
for(int i=0;i<length;i++)
{
dec=dec+a[i]*(int)(Math.pow(2, length-1-i));
}
return dec;
}
void main()/* Calling all functions in sequence*/
{
input();
calculate();
int c=check();
if(c==0)
{
JOptionPane.showMessageDialog(null,"RECEIVED MESSAGE : "+Recieved_Word+"\nNO ERRORS DETECTED");
}
else
{
int temp[]=new int[p];
for(int i=0;i<p;i++)
{
temp[i]=parity[i];
}
for(int i=0;i<p;i++)
{
parity[i]=temp[p-1-i];
}
int position=convertIntoDecimal(parity,p);
JOptionPane.showMessageDialog(null,"ERROR DETECTED AT "+position);
Corrected_Word.append(Recieved_Word);
if(Corrected_Word.charAt(position-1)=='0')
{
Corrected_Word.replace(position-1,position,"1");
}
else
{
Corrected_Word.replace(position-1, position,"0");
}
JOptionPane.showMessageDialog(null,"RECEIVED CODE : "+Recieved_Word+"\nERROR DETECTED AT "+position+"\nCORRECTED CODE : "+Corrected_Word);
}
}
}