MonkeyTrouble
//
// MonkeyTrouble.java
//
// This exercise is from CodingBat by Nick Parlante
// https://codingbat.com/prob/p181646
// We have two monkeys, a and b, and the parameters aSmile and
// bSmile indicate if each is smiling. We are in trouble if they
// are both smiling or if neither of them is smiling. Return true
// if we are in trouble.
// monkeyTrouble(true, true) → true
// monkeyTrouble(false, false) → true
// monkeyTrouble(true, false) → false
public class MonkeyTrouble
{
// The function you want to implement
public static boolean monkeyTrouble(boolean aSmile,
boolean bSmile)
{
// Remember that a comparison returns a boolean.
return aSmile == bSmile;
}
// The test function, which takes as input:
// 1) arguments for the monkeyTrouble() function
// 2) the expected output from the function
public static void testMonkeyTrouble(boolean aSmile,
boolean bSmile,
boolean expected)
{
boolean result = monkeyTrouble(aSmile, bSmile);
System.out.print("aSmile:" + aSmile +
" bSmile:" + bSmile +
" expected:" + expected +
" result:" + result + " ");
if (result == expected)
System.out.println("YAY!");
else
System.out.println("Boohoo!");
}
// Run several unit tests in the main() function.
public static void main(String[] args)
{
testMonkeyTrouble(true, true, true);
testMonkeyTrouble(false, false, true);
testMonkeyTrouble(true, false, false);
testMonkeyTrouble(false, true, false);
}
}Output:
aSmile:true bSmile:true expected:true result:true YAY!
aSmile:false bSmile:false expected:true result:true YAY!
aSmile:true bSmile:false expected:false result:false YAY!
aSmile:false bSmile:true expected:false result:false YAY!