‫Welcome back.
‫So far you have seen how to use a race.
‫You have seen how to use methods with parameters.
‫And now it's time to combine them and use methods with parameters which are of type array.
‫So let's go ahead and create a method which does just that.
‫So I'm going to create a static double get average method.
‫And here as a parameter, I use an array of ints and this is going to be called grade array.
‫So it's pretty much just going to calculate the average of all the entries within an array that is handled
‫as an argument.
‫So what I need to do now is I need to create an integer which is the size of the great array.
‫So grades, array, dot length and then I need to double, which can carry the average, average and
‫an integer which has the sum.
‫And finally I need to run a for loop which runs through the whole array and add the values to the sum.
‫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
‫plus of course you could use this great array, that length here as a comparison, but we'll need the
‫size later on in order to calculate the average, because the size is simply the amount of values that
‫we have in an array.
‫And in order to calculate the average, you take the sum and divide it by the size and that's why we
‫need the size later on as well.
‫So here we just add the current value of grade's array to the sum, and once that is done, we can finally
‫calculate the average by doing so some divided by size and while some in size are both integers and
‫average is a double, I want to cast that into a double just to make sure that we get it precise value
‫here.
‫And finally, I can now go ahead and return my average that we are return average.
‫So this method here is generally the method that I can always use whenever I want to calculate the average
‫of an array that is handled as an argument.
‫So now let's just call this method and hand over an array.
‫Of course we need to create an array for that.
‫So I'm just going to create an array of integers because that's what we have here as the paramount parameter,
‫and we simply call it students grades, which is going to be a new int array with plenty of data.
‫So let's say one student had 15 points, the other one, 13, eight, 12, six, whatever, up to 20
‫points based on the pointing system or the grading system that you have in your country, you could,
‫of course, adjust that accordingly.
‫All right.
‫And then I want to have the average result as a double, and that's just going to be my average get
‫average.
‫And I use students grades as its argument and we finally can go ahead, which is going to be the average
‫is.
‫And then here it's simply the average result.
‫And here console the red key so we can even see or actually see what we have calculated here.
‫So it should be something around, I guess 12.
‫So let's see if that's true.
‫Let's run the code.
‫And we are.
‫It's 11.666.
‫Great.
‫So it's close to 12.
‫Not precisely 12.
‫But in the end, it's totally fine.
‫So there we are.
‫We calculated the average.
‫Now, you could, of course, also write of what this is the average.
‫So we could go ahead and make it a bit more I'd say a bit more informative.
‫So let's use a for loop here or actually this time for each loop.
‫And I want to go through all the students grades.
‫So I'm just going to say grade in this grade is an integer grade in students grades and here it's important
‫to use in is going to be C W so console right line actually I'm just going to do like that zero and
‫it will be the grade.
‫Now let's run it again.
‫And we see those values 15, 13, eight, 12, six, 16 and the average of 11 and so forth.
‫All right.
‫So now that you have seen how to use methods that require an array as their argument when called, I
‫have a little challenge for you, so please go ahead and create an array of happiness in the main method
‫and assign five values to it.
‫Create a method which has an array of type int as its parameter.
‫This method should increase the argument given by two for each entry.
‫Call this method in the main method and use happiness as the argument.
‫Create a for each loop in the main method to write all the values onto the console.
‫And by the way, this method could be something like Sun is shining, because I really like that.
‫I like it when the sun is shining.
‫That actually makes me happier.
‫So create a little method sun shining and do just that.
‫So please go ahead and pause the video and try to solve this issue.
‫All right.
‫I hope you tried it.
‫So I create a new static void.
‫Sun is shining, and it needs an array of ints, which I'm just going to call X.
‫I'm going to keep it simple.
‫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
‫plus.
‫And the only thing I do here is I increase the value at the position by two.
‫So plus equal to it's going to add to each of the values of the array too.
‫So that's pretty much the only thing that the sun is shining thing is going to do.
‫And now I just need an array.
‫So I'm going to go ahead and create a new array int happiness, happy ness.
‫And this is actually an array of ints.
‫So we need the square brackets which is equal to two, three, four, five, six.
‫Very basic, nothing fancy.
‫And now I just call the sun is shining method in order to increase happiness.
‫So this could be different people.
‫So one person has a happiness of two and another one of three and the one of four and so forth.
‫So now once the sun is shining, all of those people, people are going to be happier.
‫They're going to be super happy in comparison to their state before.
‫So this is just the sun is shining where we are making people happier.
‫Now, we also want to write that onto the console so that we know the new values because we want to
‫see how the people are happy.
‫So I'm just going to do that with a for each loop and why in happiness.
‫And what that will do is it will simply write onto the console, so see W and write just y and finally
‫console the read line.
‫So let's go ahead.
‫That we are.
‫This is the new happiness.
‫So you see, people are much happier now, now that the sun is shining for five, six, seven, eight
‫and so forth.
‫Great.
‫So there is plenty to know about a race and you know a lot about it already.
‫And you are well equipped to use a race for pretty much all applications that you can think of.
‫There might be situations where it's going to be even more complicated, but in most cases what you
‫know already should be totally sufficient to get things done.