This discussion is archived
10 Replies Latest reply: Nov 10, 2012 12:29 PM by 973635 RSS

Creating a static method that checks things without if statements

970521 Newbie
Currently Being Moderated
The Method majority() takes three booleans. The method returns true if two or more booleans are true but otherwise returns false. The following code does this task easily enough, but how can I write the method without the if statements?
public class majority_program {
  public static void main(String[] args) {
    
    System.out.println(majority(true, false, true)); // Prints true on the screen since 2 out of 3 booleans are true
    
  }
  public static boolean majority(boolean a, boolean b, boolean c) {
    int amount = 0;
    
    if (a == true) amount++;
    if (b == true) amount++;
    if (c == true) amount++;
    
    if (amount >= 2) return true;
    else return false;
  }
} 
  • 1. Re: Creating a static method that checks things without if statements
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    967518 wrote:
    The Method majority() takes three booleans. The method returns true if two or more booleans are true but otherwise returns false.
    [...] but how can I write the method without the if statements?
    You can put all together in one bigger if but this does not make it more readable:
    return ((a&&b)||(a&&c)||(b&&c));
    bye
    TPD
  • 2. Re: Creating a static method that checks things without if statements
    JDScoot Explorer
    Currently Being Moderated
    If you don't want to or can't use IF statements, you can also use a SWITCH statement. It works in a similar way as the separate IF statements you have.
  • 3. Re: Creating a static method that checks things without if statements
    972409 Newbie
    Currently Being Moderated
    JDScoot wrote:
    If you don't want to or can't use IF statements, you can also use a SWITCH statement. It works in a similar way as the separate IF statements you have.
    SWITCH to value of what? We have 3 variables. IMHO it's impossible to have switch in this case.
  • 4. Re: Creating a static method that checks things without if statements
    sabre150 Expert
    Currently Being Moderated
    969406 wrote:
    JDScoot wrote:
    If you don't want to or can't use IF statements, you can also use a SWITCH statement. It works in a similar way as the separate IF statements you have.
    SWITCH to value of what? We have 3 variables. IMHO it's impossible to have switch in this case.
    Very very very clumsy and very very very silly but by nesting the switch statements definitely not impossible.
  • 5. Re: Creating a static method that checks things without if statements
    972748 Newbie
    Currently Being Moderated
    why the 'need' to not use if statements ?
  • 6. Re: Creating a static method that checks things without if statements
    gimbal2 Guru
    Currently Being Moderated
    user2045112 wrote:
    why the 'need' to not use if statements ?
    Generally if it doesn't seem to make sense in production usage, its a homework/test question. Such an assignment is not without merit, it forces the person answering it to venture off the beaten path and find alternatives.

    Of course then you have the people that dump the question in a forum in stead of trying to work it out themselves and actually learn something along the way :/
  • 7. Re: Creating a static method that checks things without if statements
    972409 Newbie
    Currently Being Moderated
    sabre150 wrote:
    969406 wrote:
    JDScoot wrote:
    If you don't want to or can't use IF statements, you can also use a SWITCH statement. It works in a similar way as the separate IF statements you have.
    SWITCH to value of what? We have 3 variables. IMHO it's impossible to have switch in this case.
    Very very very clumsy and very very very silly but by nesting the switch statements definitely not impossible.
    Uhm sure. I see everybody here has a problem understanding the question. I guess the author's task was to simplify the method and not use IF statements.

    And nesting the switch statements make it worse.
  • 8. Re: Creating a static method that checks things without if statements
    gimbal2 Guru
    Currently Being Moderated
    969406 wrote:
    Uhm sure. I see everybody here has a problem understanding the question.
    Luckily we have you now who instantly understands things perfectly where we all just blunder around in the dark.
    I guess
    Wait, what?
  • 9. Re: Creating a static method that checks things without if statements
    sabre150 Expert
    Currently Being Moderated
    969406 wrote:
    sabre150 wrote:
    969406 wrote:
    JDScoot wrote:
    If you don't want to or can't use IF statements, you can also use a SWITCH statement. It works in a similar way as the separate IF statements you have.
    SWITCH to value of what? We have 3 variables. IMHO it's impossible to have switch in this case.
    Very very very clumsy and very very very silly but by nesting the switch statements definitely not impossible.
    Uhm sure. I see everybody here has a problem understanding the question. I guess the author's task was to simplify the method and not use IF statements.

    And nesting the switch statements make it worse.
    If you had read my response more carefully you would have understood that I was commenting on your statement "SWITCH to value of what? We have 3 variables. IMHO it's impossible to have switch in this case" . I most definitely was not trying to suggest that switch statements were a simplification. I most definitely was not advocating the use of switch statements for this situation. Nobody in their right mind would use switch statements for this BUT it is most definitely possible to use them.
  • 10. Re: Creating a static method that checks things without if statements
    973635 Newbie
    Currently Being Moderated
    On a related note, if you want to make it simpler, never do this:
    if (a == true)
    Instead, just do
    if (a)
    It's pointless and cluttersome to use == or != with true or false.
    // wrong
    if (a == true)
    if (a != false)
    
    // right
    if (a)
    
    
    
    // wrong
    if (a == false)
    if (a != true)
    
    // right
    if (!a)
    Also, note that
    if (x) {
      return true;
    }
    else {
      return false;
    }
    can be simplified to
    return x;
    If you do that and change your variable to something a bit more meaningful, like trueCount or numTrue or something, your code would be
    int trueCount = 0;
    
    if (a) trueCount++;
    if (b) trueCount++;
    if (c) trueCount++;
    
    return trueCount >= 2;
    which is about as simple and clear as it can get, IMHO. Using less code doesn't necessarily make the code simpler or clearer. The idea is that someone reading the code can easily understand what it does.

    And note that the above can be easily generalized to "Are there at least M true values in this array/list of N booleans?" and still be concise and clear.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points