In: Computer Science
Can this code be rewritten but keeping the same functions as the original and with detailed comments. I'm going to post the original homework lab just in case.
Attached to this assignment is a Java program that converts a text file to a list of hexadecimal numbers. Each of those hexidecimal numbers represents the bit pattern of a character from the file with the parity bits (even parity) for a hamming code inserted. Each text character takes 8 bits and the hamming code adds 4 bits. This hamming code provides single-bit error correction. Requirements 1. The program must be written in Java. If you have not used Java before, you can learn it enough to do this assignment, by looking at the provided program. 2. You can use Eclipse to write, compile and test your program, but you may also use any other development environment you like. Only the .java file will be submitted. 3. The program will use the provided Hamming class. It will implement the decode function. The decode function is the reverse of the encode function, but it also performs single bit correction when necessary. 4. Display a message to the console when an error is corrected, as in the example below. 5. The main function must be rewritten to read hexidecimal numbers from hamming.txt file and write decoded and corrected text to output.txt. 6. Test the program with different input files. The instructor will test it with a hamming.txt file different from the one provided.
import java.io.*; import java.util.*; public class Hamming { private char letter; private int[] bits = new int[12]; private int code; public Hamming(char let) { letter = let; encode(); } public Hamming(int c) { code = c; decode(); } public int getCode() { return code; } public char getLetter() { return letter; } private void encode() { int value = letter; for (int i = 0; i < 12; i++) { if (i != 0 && i != 1 && i != 3 && i != 7) { bits[i] = value % 2; value /= 2; } } bits[0] = bits[2] ^ bits[4] ^ bits[6] ^ bits[8] ^ bits[10]; // ^ is XOR in Java bits[1] = bits[2] ^ bits[5] ^ bits[6] ^ bits[9] ^ bits[10]; bits[3] = bits[4] ^ bits[5] ^ bits[6] ^ bits[11]; bits[7] = bits[8] ^ bits[9] ^ bits[10] ^ bits[11]; code = 0; for (int i = 11; i >= 0; i--) { code *= 2; code += bits[i]; } } private void decode() { int error = 0; int value = code; for (int i = 0; i < 12; i++) { bits[i] = value % 2; value /= 2; } if (bits[0] != (bits[2] ^ bits[4] ^ bits[6] ^ bits[8] ^ bits[10])) error += 1; if (bits[1] != (bits[2] ^ bits[5] ^ bits[6] ^ bits[9] ^ bits[10])) error += 2; if (bits[3] != (bits[4] ^ bits[5] ^ bits[6] ^ bits[11])) error += 4; if (bits[7] != (bits[8] ^ bits[9] ^ bits[10] ^ bits[11])) error += 8; if (error != 0) bits[error - 1] ^= 1; letter = 0; for (int i = 11; i >= 0; i--) { if (i != 0 && i != 1 && i != 3 && i != 7) { letter *= 2; letter += bits[i]; } } if (error != 0) System.out.println("Error in bit " + (error - 1) + " corrected in character " + letter); } public static void main(String[] args) throws FileNotFoundException { Scanner inFile = new Scanner( new File("hamming.txt")); PrintStream outFile = new PrintStream(new File("output.txt")); String line; int code; System.out.println("File hamming.txt opened"); while (inFile.hasNextInt(16)) { code = inFile.nextInt(16); Hamming ham = new Hamming(code); outFile.print(ham.getLetter()); } inFile.close(); System.out.println("File output.txt closed"); } }
Done accordingly. Comment for further help. Please uprate.
Code:
package ButtonGame;
import java.io.*;
import java.util.*;
/*Hamming class:
* Attributes :
* letter -->
character representation of code or it is used as in input
* bits -->
character is 8 byte so after adding 4 hamming bits total bits are
12
* code -->
letter to hexadecimal representation or it is used as input
*
* Methods:
* Hamming(char) --> takes
character as input and set letter as input to the function and
calls encode function to convert input to code
* Hamming(int) --> takes int as
input and set code as input to the function and calls decode
function to convert input to letter
* getCode() --> return
code
* getLetter() --> returns
letter
* encode() --> converts letter
to hex code
* decode() --> converts hex code
to letter
*
*/
public class Hamming {
/*Attribute declaration section*/
private char letter;
private int[] bits = new int[12];
private int code;
/*Constructor :
* Input : character
* Output: None
* Processing : set letter to given input and calls
encode that sets letter to hex code.
* So with this letter,bits and code all are getting
set.we call this when we want to encode.
*/
public Hamming(char let) {
letter = let;
encode();
}
/*Constructor :
* Input : int
* Output: None
* Processing : set code to given input and calls
decode that sets code to letter.
* So with this letter,bits and code all are getting
set.we can this when we want to decode.
*/
public Hamming(int c) {
code = c;
decode();
}
/* Getter :
* Input : None
* Output: code
* Processing : returns code
*/
public int getCode() {
return code;
}
/* Getter :
* Input : None
* Output: letter
* Processing : returns letter
*/
public char getLetter() {
return letter;
}
/* Private method :
* Input : None
* Output: None
* Processing : set bits according to hamming coding
scheme
* and converts it into integer and set to code
*/
private void encode() {
int value = letter;
//coverting letter to bits and
setting to bits array
for (int i = 0; i < 12; i++)
{
//leaving bits
0,1,3,7 for hamming bits
if (i != 0
&& i != 1 && i != 3 && i != 7) {
bits[i] = value % 2;
value /= 2;
}
}
//calculating hamming bits and
setting to corresponding position
bits[0] = bits[2] ^ bits[4] ^
bits[6] ^ bits[8] ^ bits[10]; // ^ is XOR in Java
bits[1] = bits[2] ^ bits[5] ^
bits[6] ^ bits[9] ^ bits[10];
bits[3] = bits[4] ^ bits[5] ^
bits[6] ^ bits[11];
bits[7] = bits[8] ^ bits[9] ^
bits[10] ^ bits[11];
//converting bits to decimal
number
//using binary to decimal
conversion
code = 0;
for (int i = 11; i >= 0; i--)
{
code *= 2;
code +=
bits[i];
}
}
/* Private method :
* Input : None
* Output: None
* Processing : convert decimal to bits and
* convert bits to letter according to hamming coding
scheme
*/
private void decode() {
int error = 0;
int value = code;
//binary to decimal
conversion
for (int i = 0; i < 12; i++)
{
bits[i] = value
% 2;
value /=
2;
}
//checking error bits according to
hamming code scheme
if (bits[0] != (bits[2] ^ bits[4] ^
bits[6] ^ bits[8] ^ bits[10]))
error +=
1;
if (bits[1] != (bits[2] ^ bits[5] ^
bits[6] ^ bits[9] ^ bits[10]))
error +=
2;
if (bits[3] != (bits[4] ^ bits[5] ^
bits[6] ^ bits[11]))
error +=
4;
if (bits[7] != (bits[8] ^ bits[9] ^
bits[10] ^ bits[11]))
error +=
8;
//If there is error
//setting bit error to 1
if (error != 0)
bits[error - 1]
^= 1;
letter = 0;
//converting bits to letter using
binary to decimal conversion
for (int i = 11; i >= 0; i--)
{
if (i != 0
&& i != 1 && i != 3 && i != 7) {
letter *= 2;
letter += bits[i];
}
}
//if there is error printing error
bits
if (error != 0)
System.out.println("Error in bit " + (error - 1) + " corrected in
character " + letter);
}
public static void main(String[] args) throws
FileNotFoundException {
/* Encoding section
* Read file line by line and
looping for each character in line creating new object calling
constructor where we pass char.
* once we call constructor it will
call encode and sets code
* we call getcode and print it to
output file
*/
/*Scanner inFile = new Scanner(new
File("input.txt"));
PrintStream outFile = new
PrintStream(new File("hamming.txt"));
String codeString;
System.out.println("File input.txt
opened");
while (inFile.hasNextLine())
{
codeString =
inFile.nextLine();
for(char
c:codeString.toCharArray()) {
Hamming ham = new Hamming(c);
outFile.print(ham.getCode()+" ");
}
}
inFile.close();
outFile.close();
*/
/* Decoding section
* Read file hex by hex creating new
object calling constructor where we pass integer.
* once we call constructor it will
call decode and sets letter
* we call getLetter and print it to
output file
*/
Scanner inFileDecode = new
Scanner(new File("hamming.txt"));
PrintStream outFileDecode = new
PrintStream(new File("output.txt"));
int code;
System.out.println("File
hamming.txt closed\n\n");
System.out.println("File
hamming.txt opened");
while (inFileDecode.hasNextInt())
{
code =
inFileDecode.nextInt();
Hamming ham =
new Hamming(code);
outFileDecode.print(ham.getLetter());
}
System.out.println("File output.txt
closed");
inFileDecode.close();
outFileDecode.close();
}
}
Output:
1) Both encode and decode
2) Only Hamming.txt No error bit
3) With error bit