 Hi guys, so in this video, we're going to be taking a look at an accumulation problem with an array in Java now the structure of Accumulation problems is very similar to the structure of counting problems, which I discussed in my previous video So if you haven't seen that I'll put a link to that blur because that's a good foundation for this video so for this video the method we're going to be working on is This sum in range method It takes three parameters and integer array data integer blur and integer high and we want to return the sum of all items in data That are in the range given by the two parameters low and high So generally you can recognize an accumulation problem because it follows this basic structure Where we're given some criteria We're asked to add together or maybe multiply together all of the items in an array that meets said criteria And then at the end we want to return The value of all of the items that were added or multiplied together So let's go ahead and start by writing some suitor code so we can figure out a general structure for how we're going to approach this problem now the first thing that we're going to need to do is we're going to need to Obviously initialize a variable that's going to hold our Total as we calculate it in this problem and since we're Calculating a sum obviously the value of that sum will begin at zero Before we've added any values to it. So we'll initialize a Variable total and it will start with a value of zero Next we're going to need to look through each item in our array and then if that item falls within the given range We're going to add the value of that item to total. So we'll say examine each Item in array and we'll do that using a loop And then we'll say if current item is within range Then we want to add value of current item to Turtle Eventually we're going to reach out end condition and get to the end of our array We're going to exit our loop and finally at the end. We're going to return Turtle All right, so now we can go ahead and get started with writing Our actual method in Java Now before I start the first thing that I'm asking myself is Okay, so we're given the value low and the value high And we're asked to find all items in the array that are within the range given by the two parameters, but what this Task here doesn't really specify is it doesn't tell us if we're looking at the range as Being between low and high inclusive or exclusive in other words Are we meant to be including the values low and high? in Our calculation of the range So the way that maybe we can figure this out Since it's not made clear from the problem is we can have a look down here at our J unit tests And often if you do this, it'll give you a much better idea and Clear up any confusion you might have regarding how to approach the problem So if I just take a look here for example, I can see that this test here Assert equals 190 sum in range this array with a Value 10 for low and 90 for high so Here we have an array with four items 10 is the low end of the range 90 is the high end of the range And I can see here that if I add up all of these numbers together manually 10 plus 70 is 80 plus 20 is 100 plus 90 190 and Then I look over here. It says oh well if I Pop all these parameters into sum in range. I expect to get 190. I can now see oh, okay So therefore for this method We're looking at the range as being from low to high Inclusive since in this case we can see that we have included the values 10 and 90 in our calculation of the range All right, so now that I know that that's going to help me to write my method correctly So we can go ahead and start with this using our pseudocode as a guide So first we'll initialize a turtle To zero We'll go through each item in the array using a for loop. So we'll say 4 I Equal zero I is less than data I plus plus and then we're going to have out if statement and We're looking to see if the current item is within the range from low to high. So we'll say if data I Is greater than or equal to low and we know now that it's greater than or equal to because we checked to see that We're looking at low and high as We're looking at the range is being inclusive of low and high. So that's why we include that equal sign or and sorry Data I is less than or equal to high Then we want to add the value of data. I to our turtle. So we'll say turtle plus equals data I We're going to go loop through the array check every single item and then at the very end we're going to return Turtle Alright, so let's go ahead and run these J unit tests here and we'll make sure that we've written everything correctly And yep, we've passed the J unit test. So that's great. So that method worked fine Now just as an important point Let's say instead of this method being called some in range. We have a different method called multiply in Multiply in range So now the task is asking us to find every item and data between low and high and multiply their values together So if we look down here in our Underneath our if statement, obviously we would need to change this to total Multiplied equals data I so multiply the total each time by the value in data But importantly if we initialize our total to zero then every time we Go ahead and run this line We're just going to be multiplying by zero and our total at the end will end up being zero no matter what So importantly if you're dealing with multiplication rather than addition You need to make sure that you initialize your total to one and now That would work if we go ahead and run that as multiply in range And so that's just an important thing to be aware of I'll go ahead and put that back Alright, so hopefully what you can notice here is that the general structure of an accumulation problem such as this one and Accounting problem is very very similar The main difference being is that with accounting problem You're adding one to a counter each time you find an item in your array that meets your criteria Whereas with an accumulation problem You're taking the total and you're some you're you're adding the value of The item in the array to it or you're multiplying the total by the value in the array so just to recap the general structure of a Accumulation problem is that you're given some criteria you create a variable total or some or Something along those lines and you initialize it to either zero for addition or one for multiplication We're going to use a loop to check each item in the array And then if the current item satisfies our criteria Then we're going to add that items of value to our total or multiply our total by that items value Once we've looked at every item in our array. We're going to return our total and that's it So thank you for watching this video. I hope you found it helpful and have a great day. Bye