# Review implementation of boolean triple

Let a,b,c be a boolean value. I need to print all values of expression a && b && c. I'm writing the class

public class BooleanTriple{
private boolean a,b,c;
public BooleanTriple(boolean a, boolean b, boolean c){ this.a =a; this.b=b; this.c=c;}

/**
*Increments the value of triple with lexicographical ordering
*/
public void incr(){
if (!a) a=true;
else if(!b){
a= false;
b= true;
else if (!c){
a= false;
b= false;
c=true;
}
}
public boolean logProduct(){ return a && b && c;}
}


And Main class:

public class Main{
BooleanTriple bTriple = new BooleanTriple(false,false,false);
public static void main(String[] args){
for (int i=0; i<8; i++){
System.out.println(bTriple.logProduct());
bTriple.incr();
}
}
}


But i think, that it's bad implementation. Can you correct me?

• Please correct the braces. I don't see a closing braces for the first else if block in the BooleanTriple class. Also please be more precise in what actually you are trying to do. I honestly don't understand your description and your question. Commented Nov 4, 2013 at 16:53
• can't understand what are you trying to achieve or furthermore what is the main application of your class and methods? Commented Nov 4, 2013 at 18:37

## 2 Answers

if I quite understand your problem, to do a proper incr() method I would do so :

public class BooleanTriple {
private boolean a,b,c;
private int i;
public BooleanTriple(boolean a, boolean b, boolean c){
this.a =a; this.b=b; this.c=c;
this.i = (a ? 1 : 0)*4+(b ? 1 : 0)*2+(c ? 1 : 0)*1;  // not sure if this compile, but the idea is here
}

public void incr(){
i = (i + 1) % 8;
a = (i & 0x4) != 0;
b = (i & 0x2) != 0;
c = (i & 0x1) != 0;
}
public boolean logProduct(){ return a && b && c;}
}


With i going from 0 (000 binary) to 7 (111 binary) you have all the possible values for a, b and c

• Or get rid of the fields a, b, and c entirely, and use logProduct() { return i == 7; } Commented Nov 4, 2013 at 21:44

Try this:

public class TribleBool {
public static void main(String[] args) {
boolean a, b, c;
for (int i = 0; i <= 7; i++) {
int x = i;
a = (x / 4) > 0;
x = x % 4;
b = (x / 2) > 0;
x = x % 2;
c = x > 0;

System.out.println(logProduct(a, b, c));
}
}

public static boolean logProduct(boolean a, boolean b, boolean c) {
return a && b && c;
}
}


0 = 0 + 0 + 0

1 = 0 + 0 + 1

2 = 0 + 2 + 0

3 = 0 + 2 + 1

4 = 4 + 0 + 0

5 = 4 + 0 + 1

6 = 4 + 2 + 0

7 = 4 + 2 + 1

Translate ever column into 0 or 1 and you will find required binary combinations