Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
This video will present the solution to workbook 3.5.
Once again, we're going to visualize the runtime using VS code.
As always, make sure that you launch the exact folder that contains your Java file.
All right.
So here we have a variable that can be any day from 1 to 7 where one would be Monday and seven is Sunday.
And we need to write code that determines if you have work, depending on the day.
But if it happens to be a holiday, the no matter what day it is, you don't have work.
So some of you might start by saying, okay, if holiday is equal to true.
Then we will print.
Woo hoo!
No work.
And while technically this is correct, it's redundant.
Here we've got a Boolean variable that equals true.
And now here we're just comparing a boolean against another boolean.
True, obviously equals true.
So the comparison ends up producing another boolean if true, therefore executing the if statement.
But that's so redundant.
I can just say if holiday holiday already equals a boolean.
If true, which means the if statement will run.
Simple as that.
So back in our code holidays is boolean value will determine whether or not this if statement executes
and if it does, then we can print woohoo no work.
But if the if statement gets skipped, that means holiday was false.
There was no holiday.
So we can say else if.
Our next condition needs to check if it's Saturday or Sunday.
So we'll say if day is equal to six or day is equal to seven.
Even though this ended up being false, it's still a weekend so we can print.
It's the weekend.
No work.
But if this condition is also false, that means holiday was false.
It isn't the weekend.
So day obviously ranges from 1 to 5.
In that case, we can just say else systems dot out and we're going to print wake up at seven with a
big frown.
All right.
That is all.
All of our conditions are set up, so it's time to run some test cases.
Remember that when you want to visualize a series of if life and else what you want to do is just put
one breakpoint at the very beginning and then we're going to use the step over button to step over every
conditional.
So let's go ahead and do that right now.
You know what?
Let's put some breakpoints over here as well.
So the day starts off as three, which would be Wednesday.
Holiday equals true.
And because holiday equals true, this if statement is going to execute, it's a holiday.
So woohoo, no work.
And now you can press continue, which continues to the next breakpoint, but there are no other breakpoints,
so it just wraps everything up.
All right, Test case number two, day equals three, but holiday is false.
Let's visualize the runtime.
So now it's Wednesday and there is no holiday.
Holiday equals false.
So this if statement will not execute, please press step over, not continue, because continue continues
to the next breakpoint.
There are no other break points, so it's just going to terminate the runtime.
Let us step over this conditional.
This conditional checks if de equals six or if de equals seven, but our de variable equals three.
It's a weekday.
It is not the weekend.
So this evaluates the false.
Defaulting to the statement.
We have to wake up at seven.
All right.
Let us now put a day of six and reset everything.
So now day equals six.
It is not a holiday because holiday equals false.
This if statement is not going to execute.
But even though it's not a holiday, we still get the day off because this conditional checks, if day
equals six or if day equals seven, our day variable equals six, which satisfies this part of the comparison.
So this entire expression evaluates to true.
Therefore running it is the week end and we don't have work.
Press continue now and we are done.