# Thread: Boolean methods

1. ## Boolean methods

i have 3 boolean methods using different algorithms to calculate the same thing.

to try and check for accuracy against each other them i have
Java Code:
`If (booleanA(i) && booleanB(i) && booleanC(i)) println(i+ "is Confirmed by A B & C")`
what i want to do is find out if one returns false when two are true or vice versa
i could use a series of if (A && B !C) || B && C !A) and so forth but that involves doing the bollean checks again every time i have another statement and is not that helpful.

Is there an easier way?

2. Originally Posted by Eranga
How about nested if conditions. In each condition you can take different actions.
thanks eranga'

i dont think that gets around my problem, as such.

a series of,, or nested set of,, if statements still means i have to go away and check the boolean method each time.

if it helps to understand the problem the methods are checking for prime and perfect numbers upto 9999999. its an exercise in algorithms and efficency that im doing.. and i have to check the accuracy of the methods against each other

ive just found a couple of sentences in my study text about setting flags but there are no clues about syntax to set flags... i think this is what i need to do. so i can then do series of or nested if statements,, && and || etc

i think a series of Ifs or nested or whatever would be fine if such statements were determined by the flags rather than the methods (ie after the method had done its work)

i hope this makes things clearer.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,547
Rep Power
11
You could count the true conditions and return "count==2".

4. You are testing only three conditions, right? Or you want to all the permutations for three conditions?

5. ## my solution

okay I solved this i think!
i had missed a bit in my text about boolean flags:o
once i worked out the syntax it was fairly simple.

i have this
Java Code:
```private void printResults(int n) {
boolean a = methodOne(n);
boolean b = methodTwo(n);
boolean c = methodThree(n);
while((a || b || c)){
if (a && b && c){
//println  (n+" ALL  Confirmed ");
break;
}
if(a && b)println    ("   methodThree did not confirm "+n");
if(a && c)println    ("   methodTwo did not confirm "+n");
if(b && c)println    ("   methodOne did not confirm "+n");

if(!(a && b))println ("methodThree  confirmed "+n");
if(!(a && c))println ("methodTwo    confirmed "+n");
if(!(b && c))println ("methodOne    confirmed "+n");;
}
}```
and this much better.
there are 8 possible outcomes and i think this code covers all of them

You could count the true conditions and return "count==2".
actually that might have worked
if a correct add 1 tot total
if b correct add 3 to total
if c correct add 7 to total
the sum of the total tells you which the error is,,, I prefer the flags ;-)

6. ## simpler

Originally Posted by pbrockway2
You could count the true conditions and return "count==2".
actually counting is simpler it just doesn't tell me where the errors were

Java Code:
```private void printResults(int n) {
boolean a = methodOne(n);
boolean b = methodTwo(n);
boolean c = methodThree(n);
int total = 0;
int errors = 0;
if ((a || b || c)){
if (a && b && c){
total++;
}else{
errors++;
}
println (errors);
}```

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,547
Rep Power
11
Originally Posted by sonny
actually counting is simpler it just doesn't tell me where the errors were
Yes, I was just addressing what you wanted to do in your original post.

Java Code:
```    /** Returns true iff one method returns false when two return true or vice versa. */
boolean test(int n) {
int count = 0;
if(methodOne(n)) {
count++;
}
if(methodTwo(n)) {
count++;
}
if(methodThree(n)) {
count++;
}
return (count == 1) || (count == 2);
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•