import java.util.ArrayList; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.*; public class Graycode { public static void main(String args[]) throws FileNotFoundException { graycoding(9,6); } /** * generating graycode by giving number n as length of the graycode, k as base of graycode * @param n * @param k * @throws FileNotFoundException */ public static void graycoding(int n, int k) throws FileNotFoundException { int size = (int) Math.pow(k, n); int arr[][]= new int[size][n]; int previousBit = -1; boolean inc = true; for(int i = 1; i< size; i++) { //copy previous row to second row for(int s = 0; s =0;j--) { writer.print(arr[l][j]); } writer.println(""); } writer.close(); } /** * Check function check if the bit needs to be increment or decrement * @param num1 the current number at the bit before being flipped * @param num2 number k * @param num3 the "previous" number that was being flipped in same bit position * @return return if the number needs to be increment or decrement */ public static boolean check(int num1, int num2, int num3) { if(num1>num3) { if(num1+1>=num2) return false; else return true; } else if(num1<=num3) { if(num1-1<0) { return true; } else return false; } return true; } /** * Flip function return the position of the bit that needs to be flipped * @param num number of iteration * @param k base of graycode that was given * @param n length of graycode that was given * @return return which bit is going to be flipped compare to the previous one */ public static int flip(int num,int k,int n) { for(int i = n; i>=0; i--) { if ((num)%(Math.pow(k, i))==0) { return i; } } return 0; } }