CodingBat | Java | Warmup 1 - monkeyTrouble

Hello again, today we'll jump right into the next problem (remember, we're going from right to left/across the rows, unless you make a specific request) which is titled "monkeyTrouble". Here it is:

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.

Remember what we did last time, unless you haven't been following - and that is OKAY.

First step is translating this into ENGLISH, or simply making this easier to understand.

We have two monkeys, one is named 'a', and the other is named 'b'. We are looking at their faces to see whether or not they are smiling, why? Because the problem states, if BOTH 'a' AND 'b' are smiling at the same time OR they are BOTH not smiling, then we are in trouble - what kind you ask? Maybe we will get hit with a pie full of molasses!!!!!

Let's make sure we understand this. If these two monkeys are smiling at the same time, we get hit with molasses pie. If these two monkeys are not smiling at the same time, we get hit with molasses pie. If one of the monkeys is smiling and the other is not, we are safe - and maybe we get to hit THEM with the pie!

So in the program, we have two parameters for the method that will check the current condition of the monkeys - who is smiling and who is not. These two paramaters are aSmile and bSmile - one represents whether 'a' is smiling or not, and the other is for monkey 'b'. The website will provide the values true or false to simulate whether 'a' or 'b' is smiling or not, what YOU have to do is make sure your method will check these given values and answer correctly - remember, molasses pie is on the line!!!!!

Here is a sample simulation given by the website:
  • true means the monkey is smiling and false means he is not smiling.

  1. monkeyTrouble(true, true) → true
  2. monkeyTrouble(false, false) → true
  3. monkeyTrouble(true, false) → false

The first example is stating that both 'a' and 'b' are smiling, as both values are true, therefore the method returns the value true meaning that WE ARE GETTING HIT WITH MOLASSES PIE, we are in BIG trouble!

The second example states that neither 'a' nor 'b' is smiling, as both values are false, but remember this will also mean the method MUST return the value true - why, you ask? Remember we are in trouble (the return value is true) if BOTH monkeys smile or BOTH do not smile. The return value must only be false when one monkey is smiling and the other is not. Therefore, because in this example, both monkeys are not smiling, we are STILL in trouble. Hope you enjoy molasses as much as I don't!

The third example above shows that one monkey is smiling (guess which one, let's make sure you remember which parameter represents who. ***I'll tell you the answer below***) and the other is not. What does this mean? The return value of this method should be false, meaning we aren't in trouble, and we won't get hit with that disgusting molasses pie!

Now for those of you who have completed the previous problem, sleepIn, or even just glanced at the post - you will know that we will be using the boolean data type again to determine whether or not the monkeys are smiling. Can you guess why? It's because the values being returned are only either true or false, and only the boolean data type has these possible values.

(***: Answer to the question from up above. The first parameter is for monkey 'a', remember it is named aSmile, the second parameter bSmile, is for monkey 'b'.)

CodingBat | Answer to monkeyTrouble

Now here is the statement we have to answer: We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.

Remember from the post before, we will try to see if we can gather any clues on what tools we can use to help answer this question. Here are the keywords that I have found to be useful, that you might want to pay attention to. I'll bring back that last statement and point out the keywords:

We are in trouble IF they are BOTH smiling OR if NEITHER of them is smiling. Return true IF we are in trouble.

Another reference from the post before, you might have correctly guessed that we are going to use the if-statement, the OR operator (which is ||), and now we are introduced to the NOT operator (!) and the AND operator (&&). I may have forgotten to tell you these last three (OR, AND, NOT) are comparison operators used to compare values and objects in Java.

Let's explain each briefly: