 Hi guys, so in this video we're going to be taking a look at counting problems with arrays. So the basic structure of a counting problem is you're given some criteria. You want to count the number of items in the array that meet said criteria. And then finally at the end you want to return the number of items you counted that met the criteria. So for the first example we have a really simple problem. We have this method count positive. It takes one parameter and integer array data. And we want to return the number of items in data that are positive. So a good way to approach this problem is to start by writing out some pseudocode just to get our heads around how we're going to structure our method. So okay so if we're going to be counting every positive item in the array obviously we're going to need some variable to hold our current count. So we can have a variable called counter and at the beginning we're going to set it to zero. Now if we're going to be needing to check each item in the array then we're going to need to loop through the array starting from index zero and examine each item in data of the array. Now we're inside the loop and we're going to need to check to see if the current item is positive. And then if the current item is positive, increase counter by one. Eventually we're going to reach the end condition for our loop. Our loop will terminate once we've reached the end of our array. And at the end we're going to return... Alright so that's a structure for how our method is going to look. So now we can go ahead and try writing that in actual Java. So okay we know that we need to start with our variable counter. That's going to be of type int. And then the easiest way to loop through an array is with the for loop. So we're going to do that now. Now I am going to introduce a deliberate error here so don't panic. But how are we going to check to see if our current item is positive? Well we can say if data i maybe is greater than or equal to zero then what are we going to do? We need to increase counter by one. Now we're going to keep looping through our array. Eventually we'll get to the end and then we're going to return counter. So now that that's all done you can see down here I've written some junit tests to test this method. So let's go ahead and run this now and we'll see if my method passes all of the tests. Okay so right away you can see that we have at least one failure here. If we go down here you can see it was an assertion error and if we just double click here we can see. Alright so I did pass the first test but then on the second test here where we had as a parameter an array with two items negative one and zero. We expected zero to be returned so we expected no positive items to be counted but when we ran the test with my method we got one. So why might this be happening? Alright well let's have a look here. Is negative one positive? Definitely not. Is zero positive? Well maybe that is not necessarily intuitively clear to you when you begin figuring out the problem. So if we go up here you can see in my if statement here I've written if data i is greater than or equal to zero then increase the counter by one. So let's say I've gone ahead and I've written this method and I've kind of assumed anything that's not a negative number is positive. So therefore I've written if the current item is greater than or equal to zero increase the counter by one. Well if I look down here at the test I can see oh okay when I have an array with two items a negative item and a zero I should be returning zero and I can deduce from that that zero should not be counted as a positive number. So it's always very useful basically the moral of this is it's always very useful to take a look down at the tests if there's anything to you and maybe there isn't but if there's anything to you that is unclear when you look at the method that you're meant to be writing when you look at the description it's always useful to take a look down in the test and it could give you a better idea of the results that you're expecting and maybe of the assumptions that you should have when you write your method. Okay so we can fix this really easily just by getting rid of this equals sign and now if we go ahead and run this everything passes. Alright so now let's take a look at our second example. So I'll just go ahead and uncomment this. Alright so here we have a method count odd and not negative where we want to return the number of items in data that are odd and non-negative. Now straight away you can probably see that the way we're going to structure this method is going to be very very similar to how we structured the method above. So let's just quickly write some pseudocode again to demonstrate that. So once again we're obviously going to need a counter that will begin at zero. Once again we're going to need to examine each item in data. This time we're going to be asking ourselves we'll just say if current item is odd and non-negative. Increase counter by one. We'll reach the end of our loop. Terminate the loop and return counter. Condition. Alright so looking at this you can probably see what we're going to be writing in here is going to be honestly almost identical to what we wrote above. So let's go ahead and do it a little more quickly this time. So we'll say encounter equals zero. We'll say yep equals zero we're just going to loop through the array again. And then this time we're going to say if data i. So we're asking if it's odd how do we figure out if an item is odd. If data i modular two does not equal zero then we know that it's odd. And we need to know that the item is also not negative. So if data i is greater than or equal to zero. Now in this case we're asking if the item by the way is non-negative not if it's positive. So in this case we are going to put data i is greater than or equal to zero and that's not incorrect. So if the item is odd and non-negative then we're going to increase counter by one. And then finally at the very end we're going to return counter. Alright so let's just go ahead and run this test to make sure we didn't make any mistakes here. Beautiful. So as I'm sure you can see here these counting problems all follow a very similar structure. We start with a counter that we set to zero. We loop through each item in the array. If the current item meets the given criteria then we increase the counter by one. And then we do that for each item and eventually we return counter. So for this last example here we can work through this one really quickly. I don't think we'll need to write up suitor code for this one given that we've already done it for the first two. So we have here a method count even or negative where we want to return the number of items in data that are even or negative. Now to demonstrate how similar a lot of these counting problems are going to be. So what we're going to do is just go up here and copy the body of this method. Pop it in here. Alright now for this method count even or negative what do we need to change here in order to get this new method to work. Well the only thing we're going to need to change is what's inside this if statement because this contains this criteria that we're testing for. So we'll get rid of that. And then alright we're testing to see if the current item is even so data i modular 2 equals 0. Or if data i is negative so if data i is less than 0 then we increase the counter and then at the end we return the counter. So let's go ahead and run this with the test for this method and make sure everything passes. And yes so this method worked. So the big takeaway from looking at these three problems is that once you can learn to recognize a counting problem when you see one. So when you can see oh okay this is asking me to count the number of something in an array. Then generally speaking the way you actually go about structuring your method is going to be really similar for each problem. The main thing that's going to differ is just the body of your if statement here that you're going to tailor to match whatever criteria you're given for that particular problem. So yeah I hope you found this video helpful. Have a great day and thank you for watching. Bye.