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:10,920 --> 00:00:14,280 This video will present the solution to workbook 3.1. 4 00:00:14,580 --> 00:00:20,430 I'm also going to show you how to visualize the runtime in VS code, so please make sure that you launch 5 00:00:20,430 --> 00:00:23,340 the exact folder that contains your java file. 6 00:00:23,370 --> 00:00:27,760 Do not under any circumstance launch all of the folders at once. 7 00:00:27,780 --> 00:00:29,190 All of the resources. 8 00:00:29,190 --> 00:00:33,360 If your file explorer looks like this, please launch the correct folder. 9 00:00:33,390 --> 00:00:37,170 Otherwise you will not be able to visualize the runtime like this. 10 00:00:41,320 --> 00:00:43,660 All right, let me zoom out a little. 11 00:00:44,960 --> 00:00:47,240 The application starts by asking a question. 12 00:00:47,240 --> 00:00:50,330 Hey, Java, do I have enough change to buy chips? 13 00:00:50,720 --> 00:00:53,690 We can only say true if they have enough money. 14 00:00:53,690 --> 00:00:55,940 So if the change that they have. 15 00:00:55,940 --> 00:00:57,470 If the money that they have. 16 00:00:58,450 --> 00:01:01,690 Is greater than or equal to price. 17 00:01:02,490 --> 00:01:03,930 Then this will return true. 18 00:01:03,960 --> 00:01:06,150 Otherwise it will return false. 19 00:01:07,900 --> 00:01:08,350 All right. 20 00:01:08,350 --> 00:01:12,700 And now, instead of just running our code, let's visualize the runtime. 21 00:01:12,850 --> 00:01:19,660 Again, I repeat, this will only work if you launch the exact folder that contains your Java File workbook 22 00:01:19,660 --> 00:01:20,620 3.1. 23 00:01:20,620 --> 00:01:23,310 So please make sure your file explorer looks like mine. 24 00:01:23,320 --> 00:01:27,490 You should never under any circumstance launch all of the folders at once. 25 00:01:28,120 --> 00:01:33,250 So first put a breakpoint beside each line of code that we worked with so far. 26 00:01:35,350 --> 00:01:40,720 We're essentially putting a breakpoint beside each line of code where we want the runtime to stop. 27 00:01:41,110 --> 00:01:43,720 All right, now go ahead and press debug. 28 00:01:46,100 --> 00:01:46,790 Looking good. 29 00:01:48,110 --> 00:01:52,370 The only buttons we will use in this video are continue and stop. 30 00:01:52,910 --> 00:01:55,600 We will cover the rest of these buttons throughout the course. 31 00:01:55,610 --> 00:01:56,450 Don't worry. 32 00:01:56,690 --> 00:02:00,380 So first I will press continue to execute this line of code. 33 00:02:01,470 --> 00:02:07,960 And by doing so here, you can clearly see that Java creates a variable that equals 3.5. 34 00:02:07,980 --> 00:02:09,930 Change equals 3.5. 35 00:02:11,150 --> 00:02:15,140 Press continue again and in executes the next line of code. 36 00:02:16,110 --> 00:02:21,510 Here you can see Java created another variable called price that equals a decimal of 2.5. 37 00:02:23,030 --> 00:02:25,940 Step through this line and it prints the question. 38 00:02:25,970 --> 00:02:26,690 Hi, Java. 39 00:02:26,720 --> 00:02:28,700 Do I have enough chains to buy chips? 40 00:02:29,390 --> 00:02:32,930 And this line is going to print the resulting boolean. 41 00:02:33,200 --> 00:02:37,850 So right beside the comparison, it shows us the variables involved. 42 00:02:37,850 --> 00:02:46,880 Change and price change of 3.5 is clearly bigger than the price of 2.5, which satisfies the greater 43 00:02:46,880 --> 00:02:48,470 than or equal to comparison. 44 00:02:49,610 --> 00:02:52,040 Which will result in a value of true. 45 00:02:52,280 --> 00:02:56,570 Now, if I press next, because this is the last breakpoint, it's going to print everything else. 46 00:02:56,570 --> 00:03:01,240 So we'll ignore everything and we will just focus on this one. 47 00:03:01,250 --> 00:03:04,280 It clearly printed true in line number seven. 48 00:03:04,790 --> 00:03:05,620 All right. 49 00:03:05,630 --> 00:03:07,730 What if I change this to 250? 50 00:03:08,690 --> 00:03:10,430 Let's visualize the runtime again. 51 00:03:13,280 --> 00:03:14,750 Step through each line. 52 00:03:17,240 --> 00:03:17,570 Again. 53 00:03:17,570 --> 00:03:20,690 Here we see the variables involved in the comparison. 54 00:03:21,780 --> 00:03:29,220 Change of 2.5 is equal to a price of 2.5, which satisfies once again the greater than or equal to comparison. 55 00:03:29,220 --> 00:03:31,770 They clearly have enough to buy chips. 56 00:03:35,260 --> 00:03:35,860 True. 57 00:03:36,750 --> 00:03:43,170 Now, a lot of you have been asking me, when do I use greater than or equal to and when do I just use 58 00:03:43,170 --> 00:03:44,040 greater then? 59 00:03:44,940 --> 00:03:48,600 Well, in this case, if I put greater, then that would be incorrect. 60 00:03:48,690 --> 00:03:49,980 Why do you ask? 61 00:03:50,070 --> 00:03:51,150 Let's visualize it. 62 00:03:53,810 --> 00:03:57,860 So if I step through the runtime change of 250. 63 00:03:59,790 --> 00:04:03,450 First it creates the variables, prints a message. 64 00:04:03,450 --> 00:04:07,470 And now here change of 250 is equal to the price of 250. 65 00:04:07,500 --> 00:04:12,900 They clearly have enough to buy chips, but not according to our comparison. 66 00:04:13,050 --> 00:04:20,310 It returns false, as you will see in just a moment, because change is not greater than price. 67 00:04:20,310 --> 00:04:21,329 They are equal. 68 00:04:22,370 --> 00:04:23,140 That's not good. 69 00:04:23,150 --> 00:04:25,140 We're using the wrong operator. 70 00:04:25,160 --> 00:04:30,560 We should be returning true if the change is greater than or equal to the price. 71 00:04:31,160 --> 00:04:32,000 Perfect. 72 00:04:32,030 --> 00:04:36,170 Now, I'm going to let you pause the video so that you can finish up the rest, and then we're going 73 00:04:36,170 --> 00:04:38,630 to visualize the entire runtime together. 74 00:04:44,550 --> 00:04:45,120 All right. 75 00:04:45,120 --> 00:04:46,890 This is going to be really fun. 76 00:04:46,920 --> 00:04:49,650 I went ahead and put breakpoints everywhere. 77 00:04:49,680 --> 00:04:51,890 Feel free to pause the video and do that. 78 00:04:51,900 --> 00:04:53,850 And let's walk through the runtime. 79 00:04:55,180 --> 00:04:56,110 Let's debug. 80 00:04:57,420 --> 00:04:59,910 I'll step through the first two lines here. 81 00:05:00,000 --> 00:05:05,610 Java creates two variables change equaling 2.5 price in 2.5. 82 00:05:05,610 --> 00:05:08,130 They clearly have enough to buy chips. 83 00:05:09,100 --> 00:05:16,240 The variables involved in this comparison are change and price, which are definitely equal, which 84 00:05:16,240 --> 00:05:19,560 satisfies once again the greater than or equal to comparison. 85 00:05:19,570 --> 00:05:22,090 They clearly have enough to buy chips. 86 00:05:22,090 --> 00:05:23,860 So Java replies true. 87 00:05:24,190 --> 00:05:27,370 Here are the capacity is 12, but there are 15 people. 88 00:05:30,600 --> 00:05:37,440 And here we can only return true if the capacity is greater than or equal to the number of people inside. 89 00:05:37,500 --> 00:05:41,000 But 12, the capacity is smaller than 15. 90 00:05:41,010 --> 00:05:44,910 So this comparison is going to, rightfully so, return false. 91 00:05:47,050 --> 00:05:49,450 That would be the correct answer to give them. 92 00:05:50,650 --> 00:05:56,500 So here the request was for a PlayStation five, but we got them a toy car. 93 00:05:56,860 --> 00:05:57,930 Will they be happy? 94 00:05:57,940 --> 00:06:01,420 In other words, will the request equal what they purchased? 95 00:06:04,010 --> 00:06:08,750 Looking at the variables involved over here in the comparison, they do not equal each other. 96 00:06:08,990 --> 00:06:11,120 So this will return false. 97 00:06:13,020 --> 00:06:13,770 Beautiful. 98 00:06:15,120 --> 00:06:16,980 By the way, before I continue. 99 00:06:17,760 --> 00:06:22,490 Never, ever use the equal equal operator with strings. 100 00:06:22,500 --> 00:06:25,560 I know I talked about this before, but I want to really emphasize it. 101 00:06:25,560 --> 00:06:28,160 This compares internal references. 102 00:06:28,170 --> 00:06:30,380 It doesn't actually compare the two values. 103 00:06:30,390 --> 00:06:34,200 You'll learn more about references in module to object oriented programming. 104 00:06:34,200 --> 00:06:38,290 But by using equal equal with strings, you're going to get bugs. 105 00:06:38,310 --> 00:06:41,430 Please use dot equals with strings. 106 00:06:41,430 --> 00:06:44,200 Always use dot equals with strings. 107 00:06:44,220 --> 00:06:45,450 That's the golden rule. 108 00:06:45,630 --> 00:06:46,470 All right. 109 00:06:46,980 --> 00:06:52,830 Anyways, here we can only return true if there is enough space for all the guests. 110 00:06:52,830 --> 00:07:00,450 So if space is greater than or equal to guests looking at the variables involved, nine is clearly greater 111 00:07:00,450 --> 00:07:01,500 than eight. 112 00:07:01,500 --> 00:07:03,240 So that's going to return true. 113 00:07:03,240 --> 00:07:04,740 And indeed it does. 114 00:07:05,710 --> 00:07:06,590 All right. 115 00:07:08,260 --> 00:07:09,130 And now here. 116 00:07:09,130 --> 00:07:14,950 I really hope you didn't use the greater than or equal to operator, because that would be wrong. 117 00:07:15,040 --> 00:07:21,490 We can only return true if your votes is greater than your competitors votes. 118 00:07:21,490 --> 00:07:28,240 But if we compare them side by side, you only have 24 votes and your competitor has 43 votes. 119 00:07:28,240 --> 00:07:31,950 And accordingly this comparison is going to return false. 120 00:07:31,960 --> 00:07:33,730 You lose the election. 121 00:07:34,360 --> 00:07:43,990 Now, if you happen to have, let's say, 43 votes and your competitor also had 43 votes, let us remove 122 00:07:43,990 --> 00:07:45,460 all of the breakpoints. 123 00:07:46,350 --> 00:07:51,840 Using the x button I showed you and only put breakpoints here so that we can just start over here. 124 00:07:56,040 --> 00:08:00,150 Because I have 43 votes, my competitor has 43 votes. 125 00:08:00,150 --> 00:08:01,920 I do not win the election. 126 00:08:01,920 --> 00:08:02,940 It's a tie. 127 00:08:03,030 --> 00:08:09,000 So it's really important that you use the greater than operator, not greater than or equal to. 128 00:08:09,270 --> 00:08:12,960 I hope you were able to make that distinction when solving this workbook. 129 00:08:14,400 --> 00:08:15,690 And rightfully so. 130 00:08:15,690 --> 00:08:17,010 It prints false. 131 00:08:17,430 --> 00:08:18,900 All right, now let's clean up. 132 00:08:18,900 --> 00:08:22,920 If you want to remove the breakpoints, press the X button. 133 00:08:25,950 --> 00:08:27,960 And now we can write Java Sea. 134 00:08:28,770 --> 00:08:31,350 Java Java as we would anyways. 135 00:08:31,560 --> 00:08:32,280 Java. 136 00:08:32,280 --> 00:08:33,330 Ask Java. 137 00:08:36,419 --> 00:08:39,000 So yeah, we can still compile and run our code as normal. 138 00:08:39,000 --> 00:08:41,159 We don't always have to visualize the runtime. 139 00:08:41,190 --> 00:08:41,880 All right. 140 00:08:41,880 --> 00:08:44,400 I hope you enjoyed this breakpoint session. 141 00:08:44,430 --> 00:08:46,240 There will be plenty more to come. 142 00:08:46,260 --> 00:08:48,960 I will keep repeating this for the remaining workbooks.