Question

In this programming project you are required to develop a 16-bit CRC program using Java and...

In this programming project you are required to develop a 16-bit CRC program using Java and test your program with the crctest.txt file. The corresponding 16-bit CRC should be attached to the end of each line. You are asked to use a 16-bit polynomial - x^16+x^12+x^5+1. You are allowed to use the Java 16-bit CRC library - java.io* & java.util.zip.CRC, but your max points will be 20/25. The contents inside the crctest.txt file include:

Computer Science!
Fall Foliage around Poconos is beautiful.
Data Communications.
Computer Networking and Performance Analysis.
OSI and TCP/IP models.
Double major with Computer Security.
Modulo Math is being used for CRC algorithm.
FHSS and CDMA for wirless communications.
      ... ? / ~ *$$$ ?????
This is the last line to test CRC.

You are required to test each line, code word (data word + CRC) using your program whether your program decodes the CRC properly. Print your input with CRC on your screen line by line, but CRC should be printed with a Hexadecimal number;

You need to comment your source program in detail for full credit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.Scanner;
public class CRC
{
    //converts line to binary string
   public static String stringToBinary(String s){
        int[] word = new int[50];
        String binaryWord;
        String binaryString = "";
      
        for(int i= 0; i < s.length(); i++){
      
      
            word[i] = (int)s.charAt(i);
            binaryWord = Integer.toBinaryString(word[i]);
          
            //appends 0 to front of binary num if less than 8 char
            while(binaryWord.length() < 8){
                binaryWord = "0" + binaryWord;
            }
          
            binaryString += binaryWord;
      
        }
      
        return binaryString;
   }
  
   //convert binary to hex
   public static String binaryToHex(String s){
       s = Integer.toHexString(Integer.parseInt(s,2));
         
       //pad 0's on left
       while(s.length() < 4){
            s = "0" + s;
        }
          
       return s;
    }
  
    //convert hex to binary
    public static String hexToBinary(String s){
        int convert;
      
        convert = Integer.parseInt(s,16);
        s = Integer.toBinaryString(convert);
          
          
        //pad 0's on left
        while(s.length() < 16){
            s = "0" + s;
        }
      
        return s;
    }

  
   //compute crc given input in binary and place to start and end to xor with dividend
   public static String computeCRC(String inputWord, String div, int start, int end){
       String temp;
       String remainder = "";
       int xor;
     
        //gets first 17 chars
        temp = inputWord.substring(start,end);
      
        //moves to the first '1'
        if(temp.charAt(start) == '0'){
            start++;
            end++;
            temp = inputWord.substring(start,end);
        }
      
      
      
        //finds CRC for entire binary string
        while(end < inputWord.length()){
          
          
            //gets rid of preceding 0's
            while(temp.charAt(0) == '0'){
                if(temp.length() == 1){
                    break;
                 }
                temp = temp.substring(1);
              
            }
          
            //append char to make temp = 17 char
            while(temp.length() < 17){
                temp += Character.toString(inputWord.charAt(end));
              
                end++;
              
                //end if at end of binary string
                if (end >= inputWord.length()) {
                    break;
                }
              
              
            }
          
            //if temp is less than 17 char then END
            if(temp.length() != 17){
                break;
            }
          

          
          
            //xor temp and dividend, store in remainder
            for(int i = 0; i < temp.length(); i ++){
                xor = (int)temp.charAt(i) ^ (int)div.charAt(i);
                remainder += Integer.toString(xor);
            }
          
            temp = remainder;
            remainder = "";
          

        }
      
                     //append 0's if remainder is not 16 char
            while(temp.length() < 16){
                temp = "0" + temp;
            }
        return temp;
  
   }
  
   public static void main()throws FileNotFoundException{
       String line;
       String binaryString = "";
       String div = "10001000000100001";
       String append = "0000000000000000";
       String remainder = "";
       String[] CRC = new String [10];
       String[] CRCCHECK = new String [10];
       int j = 0;
     
     
       Scanner in = new Scanner(new FileReader("crctest.txt"));
     
       System.out.println("Program Output:\n~Line read in file~\n~Line in binary string~\n~CRC value in hex~");
       System.out.println();
       System.out.println();
     
       //iterates through whole file
       while(in.hasNextLine()){
           //initialize binaryString to null
           binaryString = "";
           // reads line
           line = in.nextLine();
           System.out.println(line);
         
           //converts line to binary string
           binaryString = stringToBinary(line);
         
            //binary string plus dividend-1 0's
            binaryString = binaryString + append;
            System.out.println(binaryString);
          

          
            remainder = computeCRC(binaryString, div, 0, div.length());

            //convert remainder to hex and store hex value in CRC[]
            CRC[j] = binaryToHex(remainder);
            System.out.println("CRC = " + CRC[j]);
            System.out.println();
            j++;
        }

        System.out.println();
        System.out.println();
     
        //prints all CRC
       for(int i = 0; i < CRC.length; i++){
           System.out.println("CRC[" + i + "] = " + CRC[i]);
        }
    
     
        /////////////////////////////////////////////////////////////////////////
        //error check
        System.out.println();
        System.out.println();
        System.out.println("**************CRC ERROR CHECK***************");
       //reset j to 0 to iterate through CRC
       j = 0;
     

       Scanner in2 = new Scanner(new FileReader("crctest.txt"));
     
       //iterates through whole file
       while(in2.hasNextLine()){
           //initialize binaryString to null
           binaryString = "";
         
           // reads line
           line = in2.nextLine();
           System.out.println(line);
         
           //converts line to binary string
           binaryString = stringToBinary(line);
         
            //convert CRC to binary
            append = hexToBinary(CRC[j]);

            //binary string plus CRC in binary
            binaryString = binaryString + append;
            System.out.println(binaryString);

          
            remainder = computeCRC(binaryString, div, 0, div.length());
          
            //store hex value in CRC[]
            CRCCHECK[j] = binaryToHex(remainder);
            //pad 0's on left
            while(CRCCHECK[j].length() < 4){
                CRCCHECK[j] = "0" + CRCCHECK[j];
            }
            System.out.println("CRCCHECK = "+ CRCCHECK[j]);
            System.out.println();
            j++;
        }
     

        System.out.println();
        System.out.println();     
        //prints all CRCCHECK
       for(int i = 0; i < CRCCHECK.length; i++){
           System.out.println("CRCCHECK[" + i + "] = " + CRCCHECK[i]);
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
In this programming project you are required to develop a 16-bit CRC program using Java and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • *USE JAVA In this project you will create program that calculate the number of times every...

    *USE JAVA In this project you will create program that calculate the number of times every word appears in a given text file. Your program will take in the name of the file as well as the number of threads to create to speed up the process. The numbers of threads must be greater than 0. The output will be saved into a file in alphabetical order. Besides creating the program itself, you are to run experiments to show how...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

    ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...

  • Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses:...

    Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses: Exception handling File Processing(text) Regular Expressions Prep Readings: Absolute Java, chapters 1 - 9 and Regular Expression in Java Project Overview: Create a Java program that allows a user to pick a cell phone and cell phone package and shows the cost. Inthis program the design is left up to the programmer however good object oriented design is required.    Project Requirements Develop a text...

  • Write a C program which will display the contents of a file in base-16 (hexadecimal) and...

    Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the following tasks: Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”. Open the file for binary input Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT