 Hi guys, so in this video, we're going to be taking a look at violation problems Now there tends to be some confusion between violation versus validation problems So if you haven't yet checked out our video on validation problems, I'll put a link to that in the description below So that you can check that out later if you choose to So the general principle of violation problems is that we're given some criteria We're going to search through each item in the array and if any of the items fails to meet the given criteria Then we're going to immediately return false. If we get to the very end of the array and we have never returned false Only then at the very end can we return true So for this example, we're going to be working with the method is ascending it takes one Parameter data, which is an integer array and we're going to return true if the array is sorted in ascending order and false otherwise So a great way to examine a problem like this If maybe you're not sure where to begin with writing the code right away is to go ahead and write some pseudocode And this will just help you I think to work out your ideas your thought process and to kind of get a handle of the Problem, so let's go ahead and do that So, okay the first thing obviously we're going to need to be Searching through each item in the array consecutively starting at index zero So we'll say examine each item in array Now what does it mean for an array to be? Sorted in ascending order. Well, if we just think of it from the perspective of each individual value If let's say the item at index zero is greater than the item at index one Then we know that the array is not sorted in ascending order So for each item in the array, we're going to need to check to make sure that The value of the item is not greater than the value of the next item in the array so what we can say is If current item is greater than Next item Then we know already that the array is not sorted in ascending order We don't need to keep searching through the array. We can just return false If the array is not great if the current item is not greater than the next item We're just going to keep searching through the array Keep looking for a violation Eventually, we're going to Reach our end condition for our loop Whoops and condition The loop will terminate And then at the very end if we have never returned false then we know that we have not found any violations of our Array being in ascending order Our array is in ascending order and we can return True So how is this going to look in actual Java? Let's go ahead and try writing this method so, okay We're going to want to look through each item in the array Generally the best way to do that is with the for loop. So let's go ahead and write that Now I am going to write this with a couple of deliberate Errors, so if you do spot an error right now, don't worry. I am aware We're searching through each item in the array now if The current item is Greater than the Next Item then we're going to return false. Whoops return Typing's hard We're going to keep looping through the array eventually. We're going to reach the end if we Get out of this for loop and we've never returned false we can go ahead and return True Alright, so now that I have written my method you can see under here I've written some J unit test to go ahead and test the is ascending method So let's go ahead and run this now and see how my method does at passing the test All right, so right away when I run it, you can see we have an error If I look down here, you can see we have a null pointer exception and if we look at This second one right here double-click on that we can see The error has occurred at our very first test A cert true is ascending null So in this case this error has occurred because I have forgotten when I've written my method to account for a Null value being passed as a parameter Into our method and this will very often happen where you'll write a test You'll write a method you'll test it and then it will fail on some Test because you failed to account for say a null value or an empty array. And so let's go ahead and fix that now So I can go ahead and fix that just by saying if data equals null then and Often it won't be intuitive just by Looking at the method whether you're meant to return for example, whether you're meant to return true or false if the Array is null or if the array is empty But a great way to check is to just go down to the test and that will generally tell you what you're meant to do So in this case we can see that we have a cert true is ascending null So we know that we should be in this case returning true if our The parameter passed to is ascending is null We can also see that we have an empty array and we have a cert true is ascending for an empty array So we know we should also be returning true if our array is empty So let's go ahead and deal with this null array So we can say I Return true if the array is null Now let's go ahead and run this again and see if we have any other issues without test All right, so it looks like we're still experiencing an error So let's take a look down here and see what's going on. So you can see here We have a just double-click I'll click on that so we can see an array index out of bounds exception Index one out of bounds for length one Now if we just double-click down here, we can see so our tests are now passing for the first two tests But we're failing here at a cert true is ascending new integer array one All right, so maybe in this case It's not as clear what we've done wrong with our method as it was with the null exception So in this case, it might be useful for us to go ahead and debug our method So that we can see exactly where the test is failing because that will give us a much better idea of how to fix our method Now importantly when you're debugging a JUnit test, you want to make sure that you move the first Failure case To the top of your testing method Otherwise when we debug it's going to go ahead and test these two methods first And we don't need to do that since we already know that they are passing All right, so now let's go ahead and hit debug and yes All right, so let's go ahead. So right now we just have Our array data and it is of length one. It just contains value one at index zero so We're going to go ahead and Start moving through our loop We'll go ahead and move through and immediately it's failed. So we know now that the method is failing at this if statement right here So let's have a think about why that might be happening So in this case, we have an array of length one It just contains a single value and we're saying if data i So if one is greater than data i plus one But there is no data i plus one So that's why we're experiencing the index out of bounds exception. So what can we do to fix this? Well, when we're going through our for loop at the moment We're looping through all the way to The very last item in our array But since we're comparing each item to the next item We actually need to be loop looping through only two The second to last item in our array, which is then going to get compared to the last item So we can fix this just by changing rather than Looping through until i Equals data dot length. We want to loop through until i equals data dot length minus one Now if we go ahead and run this Everything will pass So that was just a quick look at how to deal with violation problems in java Um, they can come in many different forms. Um, this is just one example of a problem But generally speaking, um, no matter what the problem the general format of the violation problem method is going to look more or less the same And the main thing that you're going to be changing is just going to be Whatever is inside this if statement here For example, let's say rather than the method is ascending. We had the method um All positive So in this case, we are going to Loop through the array if we find any items in the array that are not positive We're going to want to return false. Otherwise if every item in the array is positive we can return true um, so in this case We're just going to need to look down here and For each item rather than this comparison here, we're going to be asking um, if data I Is less than or equal to zero then it's not positive so we can return false Now in this case here, um, we're also going to need to change Uh, just this little part right here because we are going to need to check every single item in the array So sometimes you are going to need to change more than just what's inside the if statement, but as a general General guideline, this is the main part that you're going to be changing when dealing with various different Violation problems with arrays in java Um, I'll just go ahead and put that back. So that's just a quick overview of um, how to deal with violation problems I hope you found this video helpful and definitely check out the video on uh validation problems The link will be below in the description. Uh, if you haven't already Have a great day. Bye