Hey guys I was wondering if there was any way to return a certain fraction of a bunch of booleans in java. Simply put I would like to find out if there is a way to create a method that when given four booleans if three of them are true it returns true but if less than three are true it returns false. I know this might be hard to understand and if you don't understand just post a comment saying so.
-
2Actually, you really should first post your attempt with your question both to better clarify the problem and also to show the fruits of your efforts. – Hovercraft Full Of Eels May 19 '12 at 23:17
8 Answers
Count the true's in the array...
public boolean checkBools(boolean[] bools){
int cnt=0;
for(boolean b : bools)
if(b) cnt++;
return cnt < 3 ? false : true;
}

- 471
- 2
- 8
-
I would use varargs. Its a little bit more user friendly. However +1 – Martijn Courteaux May 20 '12 at 00:13
-
aioobe, he wanted to know if there were 3 or more true in the array, so if its less than 3, return false. if its 3 or more, return true :) – GHz Nov 11 '14 at 04:49
Just create an int that is incremented by 1 for every true. If it is greater than or equal to 3, return true. Having said this, I'm not sure where your stuck as this seems too rudimentary to even mention.

- 283,665
- 25
- 256
- 373
Weird question ... anyway, here's a possible solution for just 4 booleans:
public boolean booleanFraction(boolean a, boolean b, boolean c, boolean d) {
int ia = a ? 1 : 0;
int ib = b ? 1 : 0;
int ic = c ? 1 : 0;
int id = d ? 1 : 0;
return ia + ib + ic + id == 3;
}
For a more general solution, here's a method that receives as parameters the number of booleans that need to be true
for considering true
the whole expression, and a greater than zero variable number of boolean values:
public static boolean booleanFraction(int number, boolean... bools) {
int acc = 0;
for (boolean b : bools)
acc += b ? 1 : 0;
return acc == number;
}
Call it like this, for the example in the question:
booleanFraction(3, true, true, true, false);
> true
booleanFraction(3, false, false, true, false);
> false

- 232,561
- 37
- 312
- 386
-
2@HunterMcMillen The question mentions only 4 booleans. If more were possible, OP should specify the criteria for determining what fraction of the booleans need to be true to consider the whole bunch of booleans true. – Óscar López May 19 '12 at 23:23
-
@HunterMcMillen Anyway. I modified my answer, now is as generic as it can be. – Óscar López May 19 '12 at 23:32
public boolean checkBooleans(boolean b1, boolean b2, boolean b3, boolean b4) {
boolean[] array = new boolean[4];
boolean[0] = b1;
boolean[1] = b2;
boolean[2] = b3;
boolean[3] = b4;
int j = 0;
for(int i = 0; i < array.length; i++) {
if(array[i]) {
j++;
}
if(j == 3) {
return true;
}
}
return false;
}

- 5,477
- 17
- 67
- 116
If you want your answer as a boolean
expression, you can try,
boolean atLeastThree(boolean a, boolean b, boolean c, boolean d) {
return a ? (b && (c || d) || (c && d)) : (b && c && d);
}
Counting the number of true
s is a little more elegant and easy to understand,
boolean atLeastThree(boolean a, boolean b, boolean c, boolean d) {
return (a ? 1 : 0) +
(b ? 1 : 0) +
(c ? 1 : 0) +
(d ? 1 : 0) > 2;
}

- 83,063
- 39
- 206
- 250
boolean nOfM(int n, boolean... m) {
for (boolean mi : m) if (mi) n--;
return n < 1;
}

- 55
- 6
Using streams:
static boolean threeOrMore(Boolean... bools) {
return Stream.of(bools).filter(x -> x).count() >= 3;
}

- 413,195
- 112
- 811
- 826
A variation of my answer for the 2-out-of-3 problem:
boolean atLeastThree(boolean a, boolean b, boolean c, boolean d)
{
int n = -3;
if (a) n++;
if (b) n++;
if (c) n++;
if (d) n++;
return (n >= 0);
}

- 1
- 1

- 11,918
- 5
- 42
- 52