HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Simpler boolean truth table?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
booleantablesimplertruth

Problem

I'm doing a CodingBat exercise and would like to learn to write code in the most efficient way. On this exercise, I was just wondering if there's a shorter way to write this code.

monkeyTrouble(true, true) → true
monkeyTrouble(false, false) → true
monkeyTrouble(true, false) → false


public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {

  if (aSmile && bSmile) {
      return true;
  }

  if (!aSmile && !bSmile) {
      return true;
  }

  return false; 

}

Solution

Sometimes it is easy to forget that the simplest logical constructs like boolean are comparable with the == operator, and that, in Java, (false == false) is true.

With this in mind, your code could become:

public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return aSmile == bSmile;
}


It may be easier to see how to get there if you first transform your original code into

public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    if ((aSmile && bSmile) || (!aSmile && !bSmile)) {
        return true;
    } else {
        return false; 
    }
}


… which could become

public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile && bSmile) || (!aSmile && !bSmile);
}


From there, you may come to the realization that "both true or both false" is equivalent to "both the same".

Here is a verification of the output:

public static boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return aSmile == bSmile;
}

private static void testTruth(boolean a, boolean b) {
    System.out.printf("monkeyTrouble(%s, %s) = %s\n", a, b, monkeyTrouble(a, b));
}

public static void main(String[] args) {
    testTruth(true, true);
    testTruth(true, false);
    testTruth(false, true);
    testTruth(false, false);
}


This produces:

monkeyTrouble(true, true) = true
monkeyTrouble(true, false) = false
monkeyTrouble(false, true) = false
monkeyTrouble(false, false) = true

Code Snippets

public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return aSmile == bSmile;
}
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    if ((aSmile && bSmile) || (!aSmile && !bSmile)) {
        return true;
    } else {
        return false; 
    }
}
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return (aSmile && bSmile) || (!aSmile && !bSmile);
}
public static boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
    return aSmile == bSmile;
}

private static void testTruth(boolean a, boolean b) {
    System.out.printf("monkeyTrouble(%s, %s) = %s\n", a, b, monkeyTrouble(a, b));
}

public static void main(String[] args) {
    testTruth(true, true);
    testTruth(true, false);
    testTruth(false, true);
    testTruth(false, false);
}
monkeyTrouble(true, true) = true
monkeyTrouble(true, false) = false
monkeyTrouble(false, true) = false
monkeyTrouble(false, false) = true

Context

StackExchange Code Review Q#38570, answer score: 26

Revisions (0)

No revisions yet.