1 00:00:00,450 --> 00:00:01,560 ‫Welcome back. 2 00:00:01,590 --> 00:00:04,080 ‫So far you have seen how to use a race. 3 00:00:04,080 --> 00:00:07,410 ‫You have seen how to use methods with parameters. 4 00:00:07,410 --> 00:00:14,670 ‫And now it's time to combine them and use methods with parameters which are of type array. 5 00:00:14,700 --> 00:00:19,530 ‫So let's go ahead and create a method which does just that. 6 00:00:19,530 --> 00:00:25,920 ‫So I'm going to create a static double get average method. 7 00:00:26,070 --> 00:00:34,530 ‫And here as a parameter, I use an array of ints and this is going to be called grade array. 8 00:00:34,530 --> 00:00:41,610 ‫So it's pretty much just going to calculate the average of all the entries within an array that is handled 9 00:00:41,610 --> 00:00:42,750 ‫as an argument. 10 00:00:42,750 --> 00:00:53,070 ‫So what I need to do now is I need to create an integer which is the size of the great array. 11 00:00:53,070 --> 00:01:02,640 ‫So grades, array, dot length and then I need to double, which can carry the average, average and 12 00:01:02,640 --> 00:01:05,070 ‫an integer which has the sum. 13 00:01:06,000 --> 00:01:14,580 ‫And finally I need to run a for loop which runs through the whole array and add the values to the sum. 14 00:01:14,580 --> 00:01:24,150 ‫So I'm just going to use int i equals zero I lower and now it's important to get the size size I plus 15 00:01:24,150 --> 00:01:32,280 ‫plus of course you could use this great array, that length here as a comparison, but we'll need the 16 00:01:32,280 --> 00:01:40,260 ‫size later on in order to calculate the average, because the size is simply the amount of values that 17 00:01:40,260 --> 00:01:41,550 ‫we have in an array. 18 00:01:41,550 --> 00:01:46,440 ‫And in order to calculate the average, you take the sum and divide it by the size and that's why we 19 00:01:46,440 --> 00:01:48,210 ‫need the size later on as well. 20 00:01:48,300 --> 00:02:00,720 ‫So here we just add the current value of grade's array to the sum, and once that is done, we can finally 21 00:02:00,720 --> 00:02:10,290 ‫calculate the average by doing so some divided by size and while some in size are both integers and 22 00:02:10,290 --> 00:02:18,180 ‫average is a double, I want to cast that into a double just to make sure that we get it precise value 23 00:02:18,180 --> 00:02:18,630 ‫here. 24 00:02:18,900 --> 00:02:27,750 ‫And finally, I can now go ahead and return my average that we are return average. 25 00:02:27,750 --> 00:02:33,630 ‫So this method here is generally the method that I can always use whenever I want to calculate the average 26 00:02:33,630 --> 00:02:36,420 ‫of an array that is handled as an argument. 27 00:02:36,420 --> 00:02:40,650 ‫So now let's just call this method and hand over an array. 28 00:02:40,860 --> 00:02:42,810 ‫Of course we need to create an array for that. 29 00:02:42,810 --> 00:02:48,750 ‫So I'm just going to create an array of integers because that's what we have here as the paramount parameter, 30 00:02:48,780 --> 00:02:59,430 ‫and we simply call it students grades, which is going to be a new int array with plenty of data. 31 00:02:59,430 --> 00:03:08,520 ‫So let's say one student had 15 points, the other one, 13, eight, 12, six, whatever, up to 20 32 00:03:08,520 --> 00:03:14,850 ‫points based on the pointing system or the grading system that you have in your country, you could, 33 00:03:14,850 --> 00:03:16,890 ‫of course, adjust that accordingly. 34 00:03:17,460 --> 00:03:17,790 ‫All right. 35 00:03:17,790 --> 00:03:26,730 ‫And then I want to have the average result as a double, and that's just going to be my average get 36 00:03:26,730 --> 00:03:27,600 ‫average. 37 00:03:28,710 --> 00:03:38,850 ‫And I use students grades as its argument and we finally can go ahead, which is going to be the average 38 00:03:40,380 --> 00:03:41,130 ‫is. 39 00:03:41,400 --> 00:03:45,780 ‫And then here it's simply the average result. 40 00:03:48,980 --> 00:03:55,910 ‫And here console the red key so we can even see or actually see what we have calculated here. 41 00:03:55,910 --> 00:04:01,040 ‫So it should be something around, I guess 12. 42 00:04:01,040 --> 00:04:02,570 ‫So let's see if that's true. 43 00:04:03,410 --> 00:04:04,550 ‫Let's run the code. 44 00:04:06,070 --> 00:04:07,360 ‫And we are. 45 00:04:07,690 --> 00:04:11,080 ‫It's 11.666. 46 00:04:11,110 --> 00:04:11,740 ‫Great. 47 00:04:11,740 --> 00:04:13,020 ‫So it's close to 12. 48 00:04:13,030 --> 00:04:14,290 ‫Not precisely 12. 49 00:04:14,290 --> 00:04:16,360 ‫But in the end, it's totally fine. 50 00:04:16,360 --> 00:04:17,110 ‫So there we are. 51 00:04:17,140 --> 00:04:18,800 ‫We calculated the average. 52 00:04:18,820 --> 00:04:23,110 ‫Now, you could, of course, also write of what this is the average. 53 00:04:23,110 --> 00:04:28,240 ‫So we could go ahead and make it a bit more I'd say a bit more informative. 54 00:04:29,090 --> 00:04:34,630 ‫So let's use a for loop here or actually this time for each loop. 55 00:04:34,640 --> 00:04:37,030 ‫And I want to go through all the students grades. 56 00:04:37,030 --> 00:04:48,680 ‫So I'm just going to say grade in this grade is an integer grade in students grades and here it's important 57 00:04:48,680 --> 00:05:01,540 ‫to use in is going to be C W so console right line actually I'm just going to do like that zero and 58 00:05:01,550 --> 00:05:02,780 ‫it will be the grade. 59 00:05:03,800 --> 00:05:05,090 ‫Now let's run it again. 60 00:05:07,010 --> 00:05:14,210 ‫And we see those values 15, 13, eight, 12, six, 16 and the average of 11 and so forth. 61 00:05:14,660 --> 00:05:15,230 ‫All right. 62 00:05:15,230 --> 00:05:23,630 ‫So now that you have seen how to use methods that require an array as their argument when called, I 63 00:05:23,630 --> 00:05:28,400 ‫have a little challenge for you, so please go ahead and create an array of happiness in the main method 64 00:05:28,400 --> 00:05:30,200 ‫and assign five values to it. 65 00:05:30,230 --> 00:05:34,760 ‫Create a method which has an array of type int as its parameter. 66 00:05:34,790 --> 00:05:39,320 ‫This method should increase the argument given by two for each entry. 67 00:05:39,560 --> 00:05:44,620 ‫Call this method in the main method and use happiness as the argument. 68 00:05:44,630 --> 00:05:49,700 ‫Create a for each loop in the main method to write all the values onto the console. 69 00:05:50,030 --> 00:05:59,150 ‫And by the way, this method could be something like Sun is shining, because I really like that. 70 00:05:59,870 --> 00:06:01,460 ‫I like it when the sun is shining. 71 00:06:01,460 --> 00:06:03,300 ‫That actually makes me happier. 72 00:06:03,320 --> 00:06:07,840 ‫So create a little method sun shining and do just that. 73 00:06:07,850 --> 00:06:11,540 ‫So please go ahead and pause the video and try to solve this issue. 74 00:06:13,840 --> 00:06:14,260 ‫All right. 75 00:06:14,260 --> 00:06:15,430 ‫I hope you tried it. 76 00:06:15,610 --> 00:06:19,360 ‫So I create a new static void. 77 00:06:20,620 --> 00:06:28,300 ‫Sun is shining, and it needs an array of ints, which I'm just going to call X. 78 00:06:28,300 --> 00:06:29,560 ‫I'm going to keep it simple. 79 00:06:29,920 --> 00:06:46,240 ‫And what I do is I loop through my int x, so x, so until y is higher than x total length and I plus 80 00:06:46,240 --> 00:06:46,720 ‫plus. 81 00:06:46,720 --> 00:06:53,860 ‫And the only thing I do here is I increase the value at the position by two. 82 00:06:53,860 --> 00:07:00,340 ‫So plus equal to it's going to add to each of the values of the array too. 83 00:07:00,340 --> 00:07:04,360 ‫So that's pretty much the only thing that the sun is shining thing is going to do. 84 00:07:04,510 --> 00:07:06,910 ‫And now I just need an array. 85 00:07:07,060 --> 00:07:15,820 ‫So I'm going to go ahead and create a new array int happiness, happy ness. 86 00:07:16,300 --> 00:07:19,060 ‫And this is actually an array of ints. 87 00:07:19,390 --> 00:07:26,530 ‫So we need the square brackets which is equal to two, three, four, five, six. 88 00:07:27,400 --> 00:07:29,920 ‫Very basic, nothing fancy. 89 00:07:30,040 --> 00:07:38,320 ‫And now I just call the sun is shining method in order to increase happiness. 90 00:07:38,680 --> 00:07:40,660 ‫So this could be different people. 91 00:07:40,660 --> 00:07:45,760 ‫So one person has a happiness of two and another one of three and the one of four and so forth. 92 00:07:45,760 --> 00:07:50,650 ‫So now once the sun is shining, all of those people, people are going to be happier. 93 00:07:50,650 --> 00:07:54,520 ‫They're going to be super happy in comparison to their state before. 94 00:07:54,730 --> 00:07:58,390 ‫So this is just the sun is shining where we are making people happier. 95 00:07:58,420 --> 00:08:04,450 ‫Now, we also want to write that onto the console so that we know the new values because we want to 96 00:08:04,450 --> 00:08:06,910 ‫see how the people are happy. 97 00:08:06,910 --> 00:08:12,730 ‫So I'm just going to do that with a for each loop and why in happiness. 98 00:08:12,730 --> 00:08:22,240 ‫And what that will do is it will simply write onto the console, so see W and write just y and finally 99 00:08:22,240 --> 00:08:25,180 ‫console the read line. 100 00:08:26,520 --> 00:08:27,660 ‫So let's go ahead. 101 00:08:28,560 --> 00:08:29,460 ‫That we are. 102 00:08:29,730 --> 00:08:31,740 ‫This is the new happiness. 103 00:08:31,740 --> 00:08:38,130 ‫So you see, people are much happier now, now that the sun is shining for five, six, seven, eight 104 00:08:38,130 --> 00:08:39,180 ‫and so forth. 105 00:08:39,660 --> 00:08:40,260 ‫Great. 106 00:08:40,500 --> 00:08:44,880 ‫So there is plenty to know about a race and you know a lot about it already. 107 00:08:44,880 --> 00:08:51,930 ‫And you are well equipped to use a race for pretty much all applications that you can think of. 108 00:08:51,960 --> 00:08:56,220 ‫There might be situations where it's going to be even more complicated, but in most cases what you 109 00:08:56,220 --> 00:09:00,420 ‫know already should be totally sufficient to get things done.