Results 1 to 8 of 8
Thread: Boolean methods
- 03-26-2010, 02:59 AM #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
what i want to do is find out if one returns false when two are true or vice versaJava Code:If (booleanA(i) && booleanB(i) && booleanC(i)) println(i+ "is Confirmed by A B & C")
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?:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-26-2010, 03:13 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How about nested if conditions. In each condition you can take different actions.
- 03-26-2010, 03:35 AM #3
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.:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-26-2010, 05:48 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You could count the true conditions and return "count==2".
- 03-26-2010, 12:02 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are testing only three conditions, right? Or you want to all the permutations for three conditions?
- 03-26-2010, 01:16 PM #6
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
and this much better.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");; } }
there are 8 possible outcomes and i think this code covers all of them
actually that might have workedYou could count the true conditions and return "count==2".
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 ;-):p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-26-2010, 02:43 PM #7
simpler
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); }:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-26-2010, 10:16 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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); }
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Boolean Return Value
By devstarter in forum New To JavaReplies: 5Last Post: 03-02-2010, 07:45 AM -
Boolean value not working?
By zerkz in forum New To JavaReplies: 3Last Post: 09-29-2009, 06:42 AM -
transfer boolean to 1's and 0's
By Nikohw in forum Java AppletsReplies: 5Last Post: 09-12-2009, 09:05 PM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks