Question

In: Computer Science

Can this code be rewritten but keeping the same functions as the original and with detailed...

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");
        }
}

Solutions

Expert Solution

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


Related Solutions

What is the purpose of the following code and a detailed testing scope that can be...
What is the purpose of the following code and a detailed testing scope that can be made by looking at the following code to check for bugs or software testing measures  such as statement coverage or branch coverage checks in the code. import java.io.*; public class Cal { public static int cal (int month1, int day1, int month2, int day2, int year) { //*********************************************************** // Calculate the number of Days between the two given days in // the same year. //...
What is the role of religion in the health? Give a detailed perspective on it keeping...
What is the role of religion in the health? Give a detailed perspective on it keeping in view the role of religion in the practice and organization of medicine, in the lives of patients, their families and social networks, health professionals and the institutions in which they intera?ct
Hi, I would like the following python code rewritten in a different way/structure etc. I got...
Hi, I would like the following python code rewritten in a different way/structure etc. I got it from a question I posted but am worried fellow classmates will also be using it so am covering bases. her it is. #threeUniqueSongs.py #Simulation of professor listening to 3 songs out of playlist of 4 songs 10000 times sampling with replacement possible import random import math #Here playlist is the list of 4 songs i.e. "Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster,...
Original C code please. Part 1: You can do A, B, and C in one program...
Original C code please. Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7. 14        28       ...
How can two different benefit functions reflect exactly the same preferences? Explain.
How can two different benefit functions reflect exactly the same preferences? Explain.
How these two (2) objectives can be rewritten in a SMART APPROACH (Specific, Measurable, Achievable, Realistic,...
How these two (2) objectives can be rewritten in a SMART APPROACH (Specific, Measurable, Achievable, Realistic, and Timely). By 2021, to determine the policies needed that are effective in preventing adolescent drug abuse and addiction. To prevent the relapse of these 8th to 12th graders adolescents to drug abuse and addiction using programs by the NIDA and CDC.
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT