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:

OR:

CodingBat Answers | Warmup 1 - sleepIn

Welcome to the site once again, we'll be kicking off with the 1st problem (going from right-to-left, top-to-bottom). If you have any special and urgent requests, feel free to drop a comment or send an email and a post will be up in no time.

Alright, let's start. The problem of today, is found in the Warmup 1 section, and it's named "sleepIn". Click here for fast access if you haven't loaded it up already.

CodingBat Answers | Warmup 1 - sleepIn


The problem reads "The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in."

Before we go on, if you have questions on any of the terms being used in the sentence above, I recommend you check out this site that has helped me in the past to understand certain words used in the java programming language. I hope it does the same for you, but if it doesn't - please send a comment or email me and I will help you.


Alright, the problem states that this method takes in two parameters, one called weekday and the other vacation. These are variables, and their type is boolean. This method is also of the boolean type, and what that means is that those two variables can only store the values true OR false, and that this method can only return the same two values - true OR false.

The way this method works is that you choose either true or false to be the value stored in weekday, as well as in vacation. You do this by substituting the words true or false in the place where you see weekday and vacation - this is shown in the test that will be run on the page.

You can go from this:
sleepIn(boolean weekday, boolean vacation)

to this:
sleepIn(false, false)


There are more combinations, as both variables can be either true OR false, they don't have to be the same as the other.

If you don't understand what's going on, send me a comment or an email.

Now, what do these values mean for the variables weekday and vacation?

As it says in the problem statement, when the variable weekday is true, this means that the day is a part of the days of the week (sunday to saturday).

When it is false, this means the day is NOT a part of the week (anything that is not sunday - saturday, like 'javaday' or 'apple juice')


For the variable vacation, the value true means that we are on vacation, and we are not in vacation when the value is false.

Alright?

CodingBat Solution


Now let's get to solving the problem.

Here is what we have to answer.

"We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in."

This means that the method returns true as a value, when we are sleeping in, and will return false when we are not.

Quick explanation:
1) Are we sleeping in? Then true
2) We do not sleep in? Then false

Okay?

We must come up with an answer that handles all the combinations where the variables weekday and vacation are either true or false

All the combinations:

weekday=true
vacation=true

weekday=true
vacation=false

weekday=false
vacation=false

weekday=false
vacation=true


But these are the only ones tested in the site:

Remember: The first parameter is for weekday, the second is for vacation.

sleepIn(false, false)
sleepIn(true, false)
sleepIn(false, true)



Now we're closing to actually figuring out the solution. I want you to go back and see something in the part we have to answer.

"We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in."

There are keywords that will tell you what you have to do, and what you can use to answer this problem. In time you will learn how to translate these statements into pseudocode, do not worry if you are unable to see it at this moment!

Pseudocode is not the real deal, so it will not compile. It just helps to translate everyday language into something closer to the programming language you're dealing with (in this case, it's Java).


The keywords are if and or. Here is the statement again so you can see where I got it from.

"We sleep in IF it is not a weekday OR we're on vacation."

Here is a quick translation of the statement above.

1) We sleep in? the return value of the method is true
2) IF : the if statement
3) not a weekday? the value of weekday is false
4) OR : the symbol ||, which stands for or
5) we're on vacation : the value of vacation is true


We have been through some of this already, namely - what the return values will be if we are sleeping in, or we do not sleep in. Also, what the variables weekday and vacation mean when their values are true OR false.

The new things here take part in the coding process itself, which is using the if-statement along with the operator ||, which stands for 'or'.

Again, if you do not know these terms, visit this site for explanation. If that is not good enough, send me a comment or an email, and I will do it for you. The reason I say that is because I want this site to be about codingbat and coming up with answers. I do not want to spend time over the meaning of programming terms unless I believe they would be new to a not-so beginner of java.

Truthfully speaking, I believe you should already know the basic terms, but if you have the misfortune of being with misguided teachers or still cannot understand the terms, I have no problem helping you at all.



So I think you are all set to solve this if you know how the if-statement works, along with where to put the || operator.

Here is the pseudocode that you can use as a guide if you get stuck.


IF weekday is false OR vacation is true
return true
otherwise return false



That's all you need to do. I will post one version of the answer (there are more than one) soon enough.

Feel free to send in a comment if you feel confused or frustrated with what I have here, I only want to help you and can only do so after you let me know!

CodingBat Solutions for Java | Welcome

(This site is for the java section of codingbat, a python version may come in the future, depending on the success of this one)

Welcome to the site. If you've been wondering as to why there's yet another site offering codingbat answers, the reason is simply because this site has something those other guys don't - answers to your questions, more specifically those that start with why, what, how, and where.

CodingBat Answers, the right way


The other sites are simply not doing you any good by providing the answer to the codingbat problems upfront. You can only go so far by copying others blindly without knowing exactly what the problem is about, and it's quite embarrassing to be unable to explain your own solution to a problem to someone else.


This site hopes to change that for you.

Here, the focus is less on the answer and more on how you'll be able to provide the solution on your own. After all, the site codingbat was created to assist people in practicing their programming skills.
Copying answers only strengthens your ability to imitate, you can visit the other guys if that's your goal.

For everyone else who really wants to be a programmer and would like to take on codingbat as a means of practicing their skills, look no further for an unofficial guide to answers to each and every one of those problems.


With tips, clues, and tricks on how to get there of course.