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,740 --> 00:00:13,650 This video will present the solution to workbook 3.2. 4 00:00:13,680 --> 00:00:16,350 I'm also going to show you how to visualize the runtime in VS code. 5 00:00:16,350 --> 00:00:21,150 So please make sure that you launch the exact folder that contains your java file. 6 00:00:21,750 --> 00:00:25,840 Do not under any circumstance launch all of the resources at once. 7 00:00:25,860 --> 00:00:30,720 If your file explorer looks like this, you will not be able to visualize the runtime. 8 00:00:30,720 --> 00:00:36,300 So please close this atrocity and launch the exact folder that contains your file. 9 00:00:36,330 --> 00:00:37,810 Anyways, let us begin. 10 00:00:37,830 --> 00:00:43,290 You start with $100 and your child wants you to purchase them a toy car. 11 00:00:43,320 --> 00:00:47,220 So first we need to check if we have enough money to purchase this car. 12 00:00:47,250 --> 00:00:48,320 Can we afford it? 13 00:00:48,330 --> 00:00:56,730 So we'll say if the wallet if the money I have in my wallet is greater than or equal to the price of 14 00:00:56,730 --> 00:01:01,800 the toy car, then we are going to print. 15 00:01:02,510 --> 00:01:03,320 Sure. 16 00:01:04,269 --> 00:01:05,590 Let's add a new line. 17 00:01:06,970 --> 00:01:09,300 And then we need to pay for the toy car. 18 00:01:09,310 --> 00:01:13,900 So we need to say wallet minus equal toy car. 19 00:01:14,350 --> 00:01:19,360 So we're updating the variable on the left by subtracting from it the value on the right. 20 00:01:20,070 --> 00:01:20,940 From the wallet. 21 00:01:20,940 --> 00:01:25,230 We are subtracting the toy car and now we can say otherwise. 22 00:01:25,230 --> 00:01:28,920 If we cannot afford the toy car, we're going to print. 23 00:01:30,370 --> 00:01:31,620 Whatever this message is. 24 00:01:31,630 --> 00:01:34,390 Sorry, I only have wallet left. 25 00:01:40,230 --> 00:01:41,820 And I think we're good to go. 26 00:01:44,570 --> 00:01:46,580 Let us visualize the runtime. 27 00:01:47,780 --> 00:01:50,060 Let's put breakpoints everywhere. 28 00:01:51,920 --> 00:01:52,580 All right. 29 00:01:52,820 --> 00:01:53,600 Debug. 30 00:01:58,660 --> 00:01:59,680 So first. 31 00:02:01,220 --> 00:02:07,550 It creates two variables wallet, which equals 100 and toy car which equals 599. 32 00:02:09,789 --> 00:02:15,970 Now we reach the if statement which will only execute if wallet is greater than or equal to toy car. 33 00:02:16,300 --> 00:02:19,970 Wallet is 100, Toy car is 599. 34 00:02:19,990 --> 00:02:21,940 This condition is true. 35 00:02:21,940 --> 00:02:26,170 So the if statement is indeed going to execute it prints. 36 00:02:26,170 --> 00:02:26,800 Sure. 37 00:02:28,480 --> 00:02:29,890 Followed by a new line. 38 00:02:31,670 --> 00:02:37,310 And here we're updating the variable on the left by subtracting from it the value on the right. 39 00:02:37,760 --> 00:02:40,790 So $100 -599. 40 00:02:40,790 --> 00:02:42,320 But if I press continue. 41 00:02:43,450 --> 00:02:48,280 It terminates the runtime and we don't get to see the updated variable here. 42 00:02:48,460 --> 00:02:49,410 That is a shame. 43 00:02:49,420 --> 00:02:50,740 Let's do it again. 44 00:02:53,110 --> 00:02:53,560 All right. 45 00:02:53,560 --> 00:02:56,230 Stepping through it again, it creates two variables here. 46 00:02:56,230 --> 00:02:58,120 It's just going to print a message. 47 00:02:58,120 --> 00:03:00,280 And this condition is true. 48 00:03:00,280 --> 00:03:02,290 So the if statement executes. 49 00:03:03,220 --> 00:03:04,480 Prince Sure. 50 00:03:04,870 --> 00:03:10,600 And now, because this is the last thing that is going to run, we don't want to press continue. 51 00:03:10,630 --> 00:03:13,120 We want to just step over this line. 52 00:03:13,120 --> 00:03:14,980 So press the step over button. 53 00:03:16,220 --> 00:03:18,530 And now we can see the updated variable. 54 00:03:20,000 --> 00:03:22,190 First, we had $100. 55 00:03:22,860 --> 00:03:32,580 Now we have $94.01 and toy car obviously is still 599 Press continue and it terminates the runtime. 56 00:03:34,630 --> 00:03:39,730 So what I'm going to do is let you pause the video and write the code for this one yourself. 57 00:03:45,290 --> 00:03:45,800 All right. 58 00:03:45,800 --> 00:03:46,990 I hope you figured it out. 59 00:03:47,000 --> 00:03:48,860 Let's walk through the whole runtime. 60 00:03:48,860 --> 00:03:51,680 Let me just add some breakpoints over here as well. 61 00:03:53,110 --> 00:03:53,800 OC. 62 00:03:56,950 --> 00:03:59,500 So over here, let's just step through everything. 63 00:03:59,650 --> 00:04:04,130 Wallet 100 is clearly bigger than a toy car of 599. 64 00:04:04,150 --> 00:04:05,530 This condition is true. 65 00:04:05,530 --> 00:04:07,900 So the if statement executes prints? 66 00:04:07,900 --> 00:04:08,560 Sure. 67 00:04:08,710 --> 00:04:14,450 And here the variable on the left is going to update by subtracting the value on the right. 68 00:04:14,470 --> 00:04:16,029 Let's see it in action. 69 00:04:18,000 --> 00:04:20,940 Now wallet is $94.01. 70 00:04:22,110 --> 00:04:24,630 And now it continues to the next breakpoint. 71 00:04:24,960 --> 00:04:27,240 So now if I continue here. 72 00:04:28,290 --> 00:04:32,890 The Nike shoes are 95.99 here at prints. 73 00:04:32,910 --> 00:04:34,380 Can I get these Nike shoes? 74 00:04:34,410 --> 00:04:35,490 All right. 75 00:04:35,520 --> 00:04:40,980 Now we reach the if statement, which will only execute if wallet is greater than or equal to the Nike 76 00:04:40,980 --> 00:04:41,580 shoes. 77 00:04:41,580 --> 00:04:47,760 But here we can see that wallet is $94.01, whereas the Nike shoes are 9599. 78 00:04:47,760 --> 00:04:50,910 So this condition is going to turn out to be false. 79 00:04:51,740 --> 00:04:53,570 The statement will not execute. 80 00:04:53,570 --> 00:04:57,270 So we default to the statement and it's going to print. 81 00:04:57,290 --> 00:05:00,080 Sorry, I only have wallet left. 82 00:05:00,080 --> 00:05:02,240 $94.01. 83 00:05:06,330 --> 00:05:10,050 Now let's run a test case where this is 8999. 84 00:05:10,260 --> 00:05:12,510 I'll remove all of these breakpoints. 85 00:05:14,660 --> 00:05:17,780 And we will start by visualizing the runtime over here. 86 00:05:18,860 --> 00:05:25,460 This time, the Nike shoes are only $89, but we have $94 in $0.01 in our wallet. 87 00:05:25,460 --> 00:05:28,490 So this is indeed going to execute. 88 00:05:29,900 --> 00:05:34,730 And after we purchase the Nike shoes, we're going to step over this line because it's the last thing 89 00:05:34,730 --> 00:05:35,270 that will run. 90 00:05:35,270 --> 00:05:38,330 And we want to visualize the update in our wallet variable. 91 00:05:38,360 --> 00:05:41,330 It's going to update by subtracting the value on the right. 92 00:05:41,360 --> 00:05:46,460 94 -89.99 is $4.02. 93 00:05:46,460 --> 00:05:53,720 So after purchasing a toy car and Nike shoes, we have $4.02 left in our wallet. 94 00:05:54,380 --> 00:05:55,070 Beautiful. 95 00:05:56,430 --> 00:05:57,300 Let's clean up. 96 00:05:57,300 --> 00:06:00,360 If you want to remove the breakpoints, press the x button. 97 00:06:01,230 --> 00:06:07,890 And if you want to rerun your code normally using terminal commands, of course we can use the familiar 98 00:06:07,890 --> 00:06:15,330 Java Sea Java Java Command to compile our code followed by Java Java Mart. 99 00:06:15,360 --> 00:06:16,650 All right.