Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need the program below in JAVA programming language with Documentation Comment

ID: 3798777 • Letter: I

Question

I need the program below in JAVA programming language with Documentation Comments (JavaDOC, XML Comments)..

Wiki article link: https://en.wikipedia.org/wiki/Gray_code

Reflected Binary Code or Gray Code binary coding system where two successive values differ in only one bit contrary to classical binary coding. It is used in many different areas (see the Wikipedia article) In this assignment, you are required to generate the Gray code for n bits using the recursive reflex-and-prefix method as described in the article. Your application should work for any value of n Your code should be formatted proper Provide implementation and documentation comments (e JavaDoc, XML comments You are free to use any programming language you like. Design carefully and try to write your code in the most clear, understandable and efficient way you can.

Explanation / Answer

Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.

Following is an interesting pattern in Gray Codes.

n-bit Gray Codes can be generated from list of (n-1)-bit Gray codes using following steps.
1) Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is reverse of L1.
2) Modify the list L1 by prefixing a ‘0’ in all codes of L1.
3) Modify the list L2 by prefixing a ‘1’ in all codes of L2.
4) Concatenate L1 and L2. The concatenated list is required list of n-bit Gray codes.

For example, following are steps for generating the 3-bit Gray code list from the list of 2-bit Gray code list.
L1 = {00, 01, 11, 10} (List of 2-bit Gray Codes)
L2 = {10, 11, 01, 00} (Reverse of L1)
Prefix all entries of L1 with ‘0’, L1 becomes {000, 001, 011, 010}
Prefix all entries of L2 with ‘1’, L2 becomes {110, 111, 101, 100}
Concatenate L1 and L2, we get {000, 001, 011, 010, 110, 111, 101, 100}

To generate n-bit Gray codes, we start from list of 1 bit Gray codes. The list of 1 bit Gray code is {0, 1}. We repeat above steps to generate 2 bit Gray codes from 1 bit Gray codes, then 3-bit Gray codes from 2-bit Gray codes till the number of bits becomes equal to n.

C++ program to generate n-bit Gray codes

#include <iostream>

#include <string>

#include <vector>

using namespace std;

// This function generates all n bit Gray codes and prints the

// generated codes

void generateGrayarr(int n)

{

    // base case

    if (n <= 0)

        return;

    // 'arr' will store all generated codes

    vector<string> arr;

    // start with one-bit pattern

    arr.push_back("0");

    arr.push_back("1");

    // Every iteration of this loop generates 2*i codes from previously

    // generated i codes.

    int i, j;

    for (i = 2; i < (1<<n); i = i<<1)

    {

        // Enter the prviously generated codes again in arr[] in reverse

        // order. Nor arr[] has double number of codes.

        for (j = i-1 ; j >= 0 ; j--)

            arr.push_back(arr[j]);

        // append 0 to the first half

        for (j = 0 ; j < i ; j++)

            arr[j] = "0" + arr[j];

        // append 1 to the second half

        for (j = i ; j < 2*i ; j++)

            arr[j] = "1" + arr[j];

    }

    // print contents of arr[]

    for (i = 0 ; i < arr.size() ; i++ )

        cout << arr[i] << endl;

}

// Driver program to test above function

int main()

{

    generateGrayarr(4);

    return 0;

}

Output:-

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote