Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java code: writing three methods about encoding a String of 4 bits to get a codeword of 7 bits with error detection properties, then decode

Java code: writing three methods about encoding a String of 4 bits to get a codeword of 7 bits with error detection properties, then decode the 7-bit codeword to get the original 4-bit message in the class Hamming. (Background information can be found here: https://en.wikipedia.org/wiki/Hamming(7,4) ).

For these three methods, always perform edge case checks before doing anything else. Such as, using if statement to check whether the length of the input is exactly 7 (7 for method filp() and method decode() or 4 (4 for method encode()). If it is not, output null. Check whether the input string contains characters that is not 0 or 1, for example, when inputting "1234!@#" or "10" the method should output null. If the input is null, output null.

Don't use throws java.lang.Exception in main, but basic if statements to perform edge case checks.

Don't use toCharArray() method in the Java String library to write the method.

1.

public static String filp(String text, int bitPosition)
// Example inputs and outputs
filp("0000000", 2) --> "0100000"
filp("1101011", 5) --> "1101111"

This method takes an String of "1" and "0" characters with a total length of 7 and changes a single bit (i.e. char) in the sequence to represent a one-bit error. This method then returns the modified String.

You should use the given bitPosition to determine which bit to flip. This bitPosition refers to the Hamming Code's bit position, which is one greater than the corresponding index in the String. For example, the value at bit postion 1 of the 7-bit text is the value at index 0 of the string.

Remember the edge case checks. 2.

public static String encode(String originalMessage)
// Example inputs and outputs
encode("0110") --> "1100110"
encode("1001") --> "0011001"

This method takes a String of 4 "bits", each represented by a character of "1" or "0", and encodes the given String with the algorithm below. This method returns the encoded String with a length of 7 to represent a Hamming codeword. You may assume that originalMessage will always be 4 bits long and output will be 7 bits long.

Remember the edge case checks.

Hints:

  1. Determine the values of your parity bits in originalMessage by seeing if the number of 1's in the corresponding bit positions is even or odd.
  2. Parity bits are placed in the bit positions 1, 2, and 4.
    • Counting the number of 1's: To test whether a value is even or odd, you can use the modulus operation (%) to get the remainder when the value is divided by 2 (i.e. what's the remainder when an even number is divided by 2?). Remember that '1' does not equal the integer 1.
  3. Insert these values in the corresponding positions in the String. Return your String of 7 encoded bits.

3.

public static String decode(String text)
// Example inputs and outputs
decode("1111111") --> "1111"
decode("0000000") --> "0000"

decode("1100110") --> "0110"

decode("1010001") --> "1101" //parity bit 1 and party bit 4 both have unexpected values, so the error happens at bit position 5 (1 + 4 = 5). Change the value 0 at bit position 5 into 1.

This method takes a String of 7 "bits", each represented by a character of "1" or "0", and decodes the String with the algorithm below. This method returns the decoded String of length 4. You may assume that text has at most only one bit flipped.

Remember the edge checks.

Hints:

  1. Determine the values of your parity bits in input text. For each parity bit, check that its value makes sense and follows expectations by seeing if the number of 1's in the corresponding bit positions is even or odd. The position of any flipped bit can be calculated by adding together the bit positions of any incorrect/inconsistent parity bits.
  2. Recall that parity bits are placed in the bit positions 1, 2, and 4.
  3. Counting the number of 1's: You may want to keep track of the incorrect parity bit positions with a local int variable. To test whether a value is even or odd, you can use the modulus operation (%) to get the remainder when the value is divided by 2 (i.e. what's the remainder when an even number is divided by 2?). Remember that '1' does not equal the integer 1.
  4. If there are any inconsistencies with the parity bits, fix input text by flipping the corresponding bit position.
  5. Isolate and concatenate the non-parity bits in ciphertext to return the original 4-bit message.
  6. You can use method filp() you have written to filp the wrong text into a correct one.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions