Results 1 to 8 of 8

Thread: Boolean methods

  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Question 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?
    :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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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.
    :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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You could count the true conditions and return "count==2".

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

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

  6. #6
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up 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 ;-)
    :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

  7. #7
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up simpler

    Quote Originally Posted by pbrockway2 View Post
    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);
    		}
    :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

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by sonny View Post
    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);
    }

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Boolean Return Value
    By devstarter in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 08:45 AM
  3. Boolean value not working?
    By zerkz in forum New To Java
    Replies: 3
    Last Post: 09-29-2009, 07:42 AM
  4. transfer boolean to 1's and 0's
    By Nikohw in forum Java Applets
    Replies: 5
    Last Post: 09-12-2009, 10:05 PM
  5. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 06:31 AM

Tags for this Thread

Posting Permissions

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