Reputation: 41
I am currently working on an assignment for school but I'm lost when it comes to implementing into code.
The idea of the assignment is to create multiple True
and False
tables of (p
, q
, r
, Proposition
). The values p
, q
, r
, can be hard coded in meaning I can just create an array with the values and I do not have to worry about those. But when it comes to the last array proposition
I have to create all possible combinations out of an array length 8 and print them out.
The thing is I been searching and I can't find something related to this. I'm not asking to do the assignment for me but I want know if someone can explain to me how is it done. This is so far the code I have made it's not much but you can actually tell I'm stuck, also when I asked they told me it's possible to do it in 8 nested loops so I'm currently working on this feedback is appreciated. Thanks
public class truthTables {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
boolean[] p = {f, f, f, f, t, t, t, t};
boolean[] q = {f, f, t, t, f, f, f, f};
boolean[] r = {f, t, f, t, f, t, f, t};
boolean[] proposition = new boolean[8];
int tableNumber = 1;
while (tableNumber <= 256) {
System.out.println("Table " + tableNumber + " ");
System.out.println("P " + " q " + " r " + " proposition ");
System.out.println("________________");
int i = 0;
while (i <= 7) {
i = i;
if (proposition[i] = true) {
System.out.println(p[i] + " " + q[i] + " " + r[i] + " " + proposition[i]);
}
if (proposition[i] = false) {
System.out.println(p[i] + " " + q[i] + " " + r[i] + " " + proposition[i]);
}
i = i + 1;
}
tableNumber = tableNumber + 1;
}
}
}
Upvotes: 1
Views: 1571
Reputation: 2396
I think the first part that you are getting stuck on is the fact that you aren't editing your proposition
array with anything new. So within your while loop you need to have this
for (j = 0; j < 8; j++) {
if (((tableNumber >> i) & 1) == 1) {
proposition[i] = t;
} else {
proposition[i] = f;
}
}
which I got from this post. It will give you an array of
table 0: f, f, f, f, f, f, f, f
table 1: f, f, f, f, f, f, f, t
table 2: f, f, f, f, f, f, t, f
table 3: f, f, f, f, f, f, t, t
.
.
.
table 255: t, t, t, t, t, t, t, t
Then you have to have a table number start at 0, then you can display them as this
public class truthTables {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
boolean[] p = {f, f, f, f, t, t, t, t};
boolean[] q = {f, f, t, t, f, f, f, f};
boolean[] r = {f, t, f, t, f, t, f, t};
boolean[] proposition = new boolean[8];
int tableNumber = 0; // start at 0
while (tableNumber < 256) {
System.out.println("Table " + (tableNumber + 1) + " ");
System.out.println("P " + " q " + " r " + " proposition");
System.out.println("________________");
/** add code here **/
for (j = 0; j < 8; j++) {
if (((tableNumber >> j) & 1) == 1) {
proposition[j] = t;
} else {
proposition[j] = f;
}
}
/** to here **/
int i = 0;
while (i <= 7) {
System.out.println(p[i] + " " + q[i] + " " + r[i] + " " + proposition[i]);
i = i + 1;
}
tableNumber = tableNumber + 1;
}
}
}
Basically what you are doing is taking the table number, breaking it down into its bit components of 0 - 255 which in bits is 00000000 - 11111111 which is every table combination you are looking for. There are other ways you can code this that will save space, but I tried following your coding style.
EDIT: one of which is avoid the for loop all together and calculating the correct value in your while loop.
int i = 0;
while (i <= 7) {
boolean propositionBool = ((tableNumber >> i) & 1) == 1;
System.out.println(p[i] + " " + q[i] + " " + r[i] + " " + propositionBool);
i = i + 1;
}
Upvotes: 0
Reputation: 140319
I have to create all possible combinations out of an array length 8
You can create the tableNumber
-th table by checking its bits:
int b = 0; // "Bit index", the element of proposition you are going to set.
int t = tableNumber; // Copy of tableNumber that you can update.
while (t != 0) {
proposition[b] = (t & 1) != 0; // Is the least significant bit 1?
b++; // Increase the bit index.
t >>>= 1; // Right-shift by 1, chopping off the least significant bit.
}
You can write this as a for loop too:
for (int b = 0, t = tableNumber; t != 0; t >>>=1, b++) {
proposition[b] = (t & 1) != 0;
}
Upvotes: 2