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,930 --> 00:00:12,790 Welcome to Work Book 4.5. 4 00:00:12,790 --> 00:00:19,210 In this workbook, we're going to print the areas for a square rectangle, triangle and circle and task 5 00:00:19,210 --> 00:00:19,600 one. 6 00:00:19,600 --> 00:00:22,510 We have to define all the functions been defined. 7 00:00:22,510 --> 00:00:24,760 We just have to write the logic for it first. 8 00:00:24,760 --> 00:00:29,470 Inside the function, we check if the side that was passed in is negative, in which case we need to 9 00:00:29,470 --> 00:00:33,010 print impossible and terminate the Java program. 10 00:00:33,010 --> 00:00:34,810 We cannot let them proceed. 11 00:00:34,810 --> 00:00:38,710 So we'll say if side is smaller than zero. 12 00:00:39,510 --> 00:00:43,230 Then print error and possible. 13 00:00:50,490 --> 00:00:55,630 And then terminate the runtime system Exit zero OC. 14 00:00:55,730 --> 00:01:02,010 But otherwise, if the side that they pass in is good, then we're going to calculate the area of the 15 00:01:02,010 --> 00:01:03,480 square and return it. 16 00:01:03,510 --> 00:01:10,170 Now, if you want to do something to the power of something else, you can use the math power function. 17 00:01:10,920 --> 00:01:17,190 Power is a built in Java function that we can get from the math class and it expects to values. 18 00:01:17,580 --> 00:01:19,200 The first value is the base. 19 00:01:19,200 --> 00:01:21,540 The second value is the exponent. 20 00:01:21,660 --> 00:01:25,910 Our base is side and the exponent is going to be two. 21 00:01:25,920 --> 00:01:32,220 So this function is going to return the double result from doing side to the power of two. 22 00:01:32,430 --> 00:01:33,540 And that's all. 23 00:01:33,990 --> 00:01:34,440 All right. 24 00:01:34,440 --> 00:01:39,000 And now the remaining functions are going to follow a very similar logic. 25 00:01:39,000 --> 00:01:43,230 So I'll give you the chance to pause the video and try to complete them on your own. 26 00:01:48,900 --> 00:01:49,290 All right. 27 00:01:49,290 --> 00:01:50,010 Welcome back. 28 00:01:50,040 --> 00:01:51,740 So this one is fairly easy. 29 00:01:51,750 --> 00:01:55,130 First, you check if the length or width are negative. 30 00:01:55,140 --> 00:01:58,830 If they are, you say not possible and terminate the application. 31 00:01:58,830 --> 00:02:04,200 But if this doesn't execute, then runtime will keep going, in which case you're going to return the 32 00:02:04,200 --> 00:02:07,310 area of a rectangle which is length times width. 33 00:02:07,320 --> 00:02:09,419 I will delete the dot comment. 34 00:02:10,250 --> 00:02:16,750 And moving on to task number three, which was to calculate the area of a triangle. 35 00:02:16,760 --> 00:02:18,530 Again, we do a safeguard. 36 00:02:18,530 --> 00:02:22,880 If they pass in an invalid base or height, we terminate the application. 37 00:02:22,880 --> 00:02:27,890 Otherwise, runtime continues and we return the area of a triangle. 38 00:02:28,700 --> 00:02:32,560 And now in task number four, your function needs to return the area of a circle. 39 00:02:32,570 --> 00:02:35,200 First, you check if the radius is negative. 40 00:02:35,210 --> 00:02:40,580 If that happens to be the case, we print not possible and terminate the application. 41 00:02:40,700 --> 00:02:43,790 Otherwise we return pi. 42 00:02:43,790 --> 00:02:51,560 So pi is a field from the math class that equals 3.14159, etc. It is better practice to reference pie 43 00:02:51,560 --> 00:02:55,820 from the math class as opposed to actually writing 3.1 for yourself. 44 00:02:56,000 --> 00:03:01,880 And then all we're doing is multiplying that by radius to the power of two as you were instructed to 45 00:03:01,880 --> 00:03:03,860 do so inside of learn the part. 46 00:03:03,860 --> 00:03:08,270 The area of a circle is equal to pi times radius to the power of two. 47 00:03:08,480 --> 00:03:10,100 And that is all. 48 00:03:10,610 --> 00:03:11,570 All right. 49 00:03:11,810 --> 00:03:14,780 Task number five is to call the area functions. 50 00:03:14,780 --> 00:03:16,910 So here we'll say, Well, you know what? 51 00:03:16,910 --> 00:03:19,400 Let's just uncomment these here. 52 00:03:19,400 --> 00:03:21,650 We can calculate the area of a square. 53 00:03:23,310 --> 00:03:25,320 With a side of two. 54 00:03:29,700 --> 00:03:36,660 Here we calculate the area of a rectangle with a length of one and a width of two. 55 00:03:40,530 --> 00:03:43,320 Here we calculate the area of a triangle. 56 00:03:44,560 --> 00:03:48,580 With a base of one and a height of two. 57 00:03:52,170 --> 00:03:57,630 And here we calculate the area of a circle with a radius of what was it again? 58 00:03:57,750 --> 00:03:58,890 A radius of two. 59 00:04:02,780 --> 00:04:03,230 All right. 60 00:04:03,230 --> 00:04:05,510 That was task number five. 61 00:04:06,300 --> 00:04:10,470 Task number six was to define a function that prints every single area. 62 00:04:11,340 --> 00:04:15,360 So the function is going to be public static. 63 00:04:15,360 --> 00:04:21,630 As always, it's going to be void because no return type was specified in the doc comment and we're 64 00:04:21,630 --> 00:04:25,830 not returning anything because all we'll be doing is printing a bunch of areas. 65 00:04:26,490 --> 00:04:29,010 It's going to receive four parameters. 66 00:04:30,530 --> 00:04:31,850 Double square. 67 00:04:32,510 --> 00:04:34,130 Double rectangle. 68 00:04:36,270 --> 00:04:37,620 Double triangle. 69 00:04:38,360 --> 00:04:40,130 And double circle. 70 00:04:40,550 --> 00:04:47,360 So when called this function expects to receive four values, we're going to print all of these values 71 00:04:47,360 --> 00:04:51,380 in accordance with what we have specified over here. 72 00:04:57,900 --> 00:04:58,680 Looking good. 73 00:05:01,110 --> 00:05:02,250 Deleting this. 74 00:05:03,420 --> 00:05:05,490 And I'm pretty sure we're all done. 75 00:05:06,410 --> 00:05:12,530 The next step is to call a function that prints all the areas, and that function is print areas. 76 00:05:13,300 --> 00:05:14,530 It's time to run the code. 77 00:05:14,530 --> 00:05:17,980 But as always, I would prefer to visualize the runtime. 78 00:05:17,980 --> 00:05:23,190 So I will place breakpoints over here and that will be all. 79 00:05:23,200 --> 00:05:24,820 Let us now visualize it. 80 00:05:26,730 --> 00:05:29,160 Dismissing this debug console. 81 00:05:30,840 --> 00:05:32,400 Minimizing this window. 82 00:05:32,580 --> 00:05:39,330 So first, we're calling the function area square, passing in a side of to the parameter side stores. 83 00:05:39,330 --> 00:05:43,200 The value that was passed in two is not smaller than zero. 84 00:05:43,200 --> 00:05:48,900 So this gets skipped and two to the power of two is going to return for. 85 00:05:50,860 --> 00:05:54,400 The square variable equals the return value of four. 86 00:05:56,040 --> 00:05:56,580 Beautiful. 87 00:05:56,580 --> 00:06:00,700 And now we're calling area rectangle, passing in two values. 88 00:06:00,720 --> 00:06:05,370 The first parameter store is the first value, lowest pass, then the second parameter store is the 89 00:06:05,370 --> 00:06:07,020 second value that was passed in. 90 00:06:07,530 --> 00:06:10,050 None of the parameters are less than zero. 91 00:06:10,050 --> 00:06:16,920 So this statement is going to get skipped and here it returns the area of a rectangle length times width. 92 00:06:17,460 --> 00:06:22,890 And now we're setting the rectangle variable equal to the return value of two. 93 00:06:24,480 --> 00:06:24,810 All right. 94 00:06:24,810 --> 00:06:29,550 Now, we've calculated two areas, one for the square, one for the rectangle. 95 00:06:29,580 --> 00:06:34,590 Now we're calling a function that calculates the area of a triangle passing in two values. 96 00:06:36,640 --> 00:06:39,490 The first parameter store is the first value that was passed in. 97 00:06:39,520 --> 00:06:41,020 The second parameter stores. 98 00:06:41,020 --> 00:06:42,850 The second value that was passed in. 99 00:06:43,180 --> 00:06:46,210 Here we're checking if either value is less than zero. 100 00:06:46,240 --> 00:06:47,290 That is not the case. 101 00:06:47,290 --> 00:06:52,360 So this if statement gets skipped and here we return the area of a triangle. 102 00:06:53,740 --> 00:06:56,770 The function call retains this return value. 103 00:06:56,770 --> 00:07:01,360 And here we set our variable equal to an area of one. 104 00:07:02,660 --> 00:07:04,760 All right, Now we've got three areas. 105 00:07:05,390 --> 00:07:08,270 Stepping inside the function area circle. 106 00:07:10,160 --> 00:07:10,460 Here. 107 00:07:10,460 --> 00:07:13,250 We check if the radius that was passed in is smaller than zero. 108 00:07:13,250 --> 00:07:14,180 That is not the case. 109 00:07:14,180 --> 00:07:15,920 So the if statement gets skipped. 110 00:07:16,070 --> 00:07:23,690 And now here we're doing 3.1, four times two to the power of two, which is going to result in a return 111 00:07:23,690 --> 00:07:26,690 value of 12.56. 112 00:07:26,690 --> 00:07:29,180 And then we're setting the variable equal to it. 113 00:07:29,840 --> 00:07:30,790 All right. 114 00:07:30,800 --> 00:07:34,640 And then print areas is just going to print all of these areas. 115 00:07:35,830 --> 00:07:36,640 Beautiful. 116 00:07:37,340 --> 00:07:42,800 This was the last workbook for the function section, and I really hope you've been enjoying these breakpoint 117 00:07:42,800 --> 00:07:43,640 sessions.