Created
January 15, 2018 19:40
-
-
Save m1k3yfoo/8d16f5a6a3a1c72dc3d7d30db9ba5b9b to your computer and use it in GitHub Desktop.
[Boolean Table] Generates all possible combinations of true/false for a boolean table #Java, #COMP671
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BooleanTable | |
{ | |
public static void main( String[] args ) { | |
final int n = 3; | |
final int col = (int) Math.pow( 2, n ); | |
ArrayList<char[]> charMatrix = new ArrayList<>(); | |
char[][] transposedMatrix = new char[n][col]; | |
ArrayList<char[]> charArr; | |
boolean[] boolArr; | |
for (int i = 0; i < Math.pow( 2, n ); i++) { | |
String bin = Integer.toBinaryString( i ); | |
// System.out.println(bin); | |
// Fill in the leading bits with 0. If the binary value of the | |
// integer is 1, insert leading 0s until the binary is n-bit | |
// long, i.e. 00001 if n = 5 | |
while (bin.length() < n) { | |
bin = "0" + bin; | |
// System.out.println("0 + bin: " + bin); | |
} | |
char[] chars = bin.toCharArray(); | |
charMatrix.add( chars ); | |
// System.out.println(Arrays.toString( chars )); | |
boolArr = new boolean[col]; | |
for (int j = 0; j < chars.length; j++) { | |
boolArr[j] = chars[j] == '0'; | |
} | |
// System.out.println( Arrays.toString( boolArr )); | |
} | |
// Transpose matrix and convert '0' to 'F' and '1' to 'T' | |
for (int j = 0; j < n; j++) { | |
for (int k = 0; k < col; k++) { | |
if (charMatrix.get( k )[j] == '0') { | |
transposedMatrix[j][k] = 'F'; | |
} else { | |
transposedMatrix[j][k] = 'T'; | |
} | |
} | |
} | |
for (char[] arr : transposedMatrix) { | |
System.out.println( Arrays.toString( arr ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment