# Boolean methods

• 03-26-2010, 03:59 AM
sonny
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
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?
• 03-26-2010, 04:13 AM
Eranga
How about nested if conditions. In each condition you can take different actions.
• 03-26-2010, 04:35 AM
sonny
Quote:

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.
• 03-26-2010, 06:48 AM
pbrockway2
You could count the true conditions and return "count==2".
• 03-26-2010, 01:02 PM
Eranga
You are testing only three conditions, right? Or you want to all the permutations for three conditions?
• 03-26-2010, 02:16 PM
sonny
my solution
okay I solved this i think!
once i worked out the syntax it was fairly simple.

i have this
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

Quote:

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 ;-)
• 03-26-2010, 03:43 PM
sonny
simpler
Quote:

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

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);                 }```
• 03-26-2010, 11:16 PM
pbrockway2
Quote:

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.

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); }```