1 00:00:00,150 --> 00:00:03,840 Before we start, did you try solving the workbook yourself? 2 00:00:03,870 --> 00:00:09,630 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:11,140 --> 00:00:13,900 This video will present the solution to workbook 3.5. 4 00:00:13,930 --> 00:00:16,870 Once again, we're going to visualize the runtime using VS code. 5 00:00:16,900 --> 00:00:21,270 As always, make sure that you launch the exact folder that contains your Java file. 6 00:00:21,280 --> 00:00:21,670 All right. 7 00:00:21,670 --> 00:00:31,180 So here we have a variable that can be any day from 1 to 7 where one would be Monday and seven is Sunday. 8 00:00:31,980 --> 00:00:36,690 And we need to write code that determines if you have work, depending on the day. 9 00:00:36,810 --> 00:00:41,630 But if it happens to be a holiday, the no matter what day it is, you don't have work. 10 00:00:41,640 --> 00:00:47,190 So some of you might start by saying, okay, if holiday is equal to true. 11 00:00:48,100 --> 00:00:49,810 Then we will print. 12 00:00:49,840 --> 00:00:50,570 Woo hoo! 13 00:00:50,590 --> 00:00:51,460 No work. 14 00:00:54,280 --> 00:00:58,420 And while technically this is correct, it's redundant. 15 00:00:58,990 --> 00:01:02,200 Here we've got a Boolean variable that equals true. 16 00:01:02,290 --> 00:01:06,070 And now here we're just comparing a boolean against another boolean. 17 00:01:06,070 --> 00:01:08,200 True, obviously equals true. 18 00:01:08,200 --> 00:01:14,200 So the comparison ends up producing another boolean if true, therefore executing the if statement. 19 00:01:14,200 --> 00:01:16,060 But that's so redundant. 20 00:01:16,060 --> 00:01:20,740 I can just say if holiday holiday already equals a boolean. 21 00:01:20,740 --> 00:01:23,230 If true, which means the if statement will run. 22 00:01:23,350 --> 00:01:24,550 Simple as that. 23 00:01:24,550 --> 00:01:32,080 So back in our code holidays is boolean value will determine whether or not this if statement executes 24 00:01:32,350 --> 00:01:35,320 and if it does, then we can print woohoo no work. 25 00:01:35,320 --> 00:01:39,100 But if the if statement gets skipped, that means holiday was false. 26 00:01:39,100 --> 00:01:40,420 There was no holiday. 27 00:01:40,420 --> 00:01:42,250 So we can say else if. 28 00:01:43,720 --> 00:01:46,630 Our next condition needs to check if it's Saturday or Sunday. 29 00:01:46,630 --> 00:01:55,450 So we'll say if day is equal to six or day is equal to seven. 30 00:01:57,470 --> 00:02:02,030 Even though this ended up being false, it's still a weekend so we can print. 31 00:02:03,640 --> 00:02:04,690 It's the weekend. 32 00:02:04,690 --> 00:02:05,920 No work. 33 00:02:09,289 --> 00:02:13,290 But if this condition is also false, that means holiday was false. 34 00:02:13,310 --> 00:02:14,600 It isn't the weekend. 35 00:02:14,600 --> 00:02:17,520 So day obviously ranges from 1 to 5. 36 00:02:17,540 --> 00:02:25,670 In that case, we can just say else systems dot out and we're going to print wake up at seven with a 37 00:02:25,670 --> 00:02:26,760 big frown. 38 00:02:26,780 --> 00:02:27,230 All right. 39 00:02:27,230 --> 00:02:27,850 That is all. 40 00:02:27,860 --> 00:02:32,330 All of our conditions are set up, so it's time to run some test cases. 41 00:02:32,420 --> 00:02:37,550 Remember that when you want to visualize a series of if life and else what you want to do is just put 42 00:02:37,550 --> 00:02:43,460 one breakpoint at the very beginning and then we're going to use the step over button to step over every 43 00:02:43,460 --> 00:02:44,120 conditional. 44 00:02:44,120 --> 00:02:46,580 So let's go ahead and do that right now. 45 00:02:47,090 --> 00:02:47,480 You know what? 46 00:02:47,480 --> 00:02:49,310 Let's put some breakpoints over here as well. 47 00:02:49,820 --> 00:02:53,600 So the day starts off as three, which would be Wednesday. 48 00:02:53,630 --> 00:02:55,280 Holiday equals true. 49 00:02:56,100 --> 00:03:01,110 And because holiday equals true, this if statement is going to execute, it's a holiday. 50 00:03:01,110 --> 00:03:03,030 So woohoo, no work. 51 00:03:03,850 --> 00:03:09,220 And now you can press continue, which continues to the next breakpoint, but there are no other breakpoints, 52 00:03:09,220 --> 00:03:10,930 so it just wraps everything up. 53 00:03:12,980 --> 00:03:18,380 All right, Test case number two, day equals three, but holiday is false. 54 00:03:19,100 --> 00:03:20,690 Let's visualize the runtime. 55 00:03:23,290 --> 00:03:26,140 So now it's Wednesday and there is no holiday. 56 00:03:26,260 --> 00:03:27,760 Holiday equals false. 57 00:03:27,760 --> 00:03:35,260 So this if statement will not execute, please press step over, not continue, because continue continues 58 00:03:35,260 --> 00:03:36,580 to the next breakpoint. 59 00:03:36,580 --> 00:03:40,450 There are no other break points, so it's just going to terminate the runtime. 60 00:03:40,450 --> 00:03:42,850 Let us step over this conditional. 61 00:03:42,880 --> 00:03:50,050 This conditional checks if de equals six or if de equals seven, but our de variable equals three. 62 00:03:50,050 --> 00:03:51,130 It's a weekday. 63 00:03:51,160 --> 00:03:52,330 It is not the weekend. 64 00:03:52,330 --> 00:03:54,340 So this evaluates the false. 65 00:03:55,530 --> 00:03:57,180 Defaulting to the statement. 66 00:03:57,180 --> 00:03:58,830 We have to wake up at seven. 67 00:03:59,160 --> 00:04:00,420 All right. 68 00:04:00,900 --> 00:04:06,960 Let us now put a day of six and reset everything. 69 00:04:09,370 --> 00:04:11,290 So now day equals six. 70 00:04:11,290 --> 00:04:15,240 It is not a holiday because holiday equals false. 71 00:04:15,250 --> 00:04:18,010 This if statement is not going to execute. 72 00:04:18,130 --> 00:04:23,980 But even though it's not a holiday, we still get the day off because this conditional checks, if day 73 00:04:23,980 --> 00:04:30,430 equals six or if day equals seven, our day variable equals six, which satisfies this part of the comparison. 74 00:04:30,430 --> 00:04:34,150 So this entire expression evaluates to true. 75 00:04:34,180 --> 00:04:38,590 Therefore running it is the week end and we don't have work. 76 00:04:38,590 --> 00:04:41,650 Press continue now and we are done.