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.