 Hi, tonight I'm going to try and clear up a little bit of the mess that happened last night or at least go into more detail on what I was thinking. When I was creating the constructor for the blanket class, I needed to have a size and it can either be twin, full, queen or king. So here's what I needed to do. I say the constructor has to check that the argument is one of those values. And if it isn't one of those then use twin as the default. And this is the solution that I came up with, which involved an array list that was made out of an array and that uses the contains method and it's really a lot more complicated than it needs to be. So let's think about what we have to do and let's try a lot of different approaches and I may go and end up with this one just to show it to you at some point. None of these are going to compile properly but that's okay. I'm just going to number these so that we can actually let's do this. And by the way let's really quickly take a look at this one here. I've got a blanket test I do create two blankets one with full in all caps for the size and one extra large which is not a valid size. And so if it's not valid I'm going to call it twin and if I compile this and run it. It turns out it gets twin for the size so that's working the way I wanted to. But again the question is, is there an easier way to do it than doing all this business with the array list and whatnot. The simplest approach because there's only four values. First of all I can I'm always going to be converting it to lower case that's going to make my life a lot easier. So I could say if LC size dot equals full or LC size dot equals twin and by the way notice my line is getting a little bit long I don't know if you can see it but there's a vertical bar here that tells which column I'm in, and I don't want to go beyond that column. So that means I am going to have to go to the next line here. LC size dot equals queen or LC size dot equals of if it's any one of those then I use that size otherwise I say it's twin. And let's save this as blanket if test, and we'll use that new class that we've just created here. Let's compile this first of all to make sure that it works. And what have I done here. Oh yes, I have to call it blanket if there we go. Excellent. So here, compile this and run it. Well that's interesting. Okay, it's going to be an evening of misnaming everything here so somebody remind me to do this correctly. And again it works fine it does what it's supposed to do. So, when I have only four items. Let's just say it wouldn't kill me to do this. Okay, this reasonable to do the four direct comparisons. Now that's not going to work when I have something like this, if I wanted to say let's say what's the country of origin, and it's somewhere in the European Union, and I have to search through 28 of them. I definitely don't want to have to go with a 28 way. I want to go with Austria or equals Belgium or equals Bulgaria or equals Croatia that's just totally out of the question. Instead I want to create an array with all the values. So let's go here and let's save this under a new name and call it blanket sequential dot Java. What I'm now going to do is I'm going to do this I'm going to make a new method called. I guess it could be private valid. We're going to create. And this is going to be very convenient because I can then use that everywhere else. And the idea is given a size check to see if it's a valid blanket size. Otherwise return twin. So in here I'm going to have a string array called sizes which will be twin full queen and king. And then I'm going to say size becomes size dot to lower case. I'm changing it but that's okay because strings are immutable. So I'm giving it a new value and the original will remain unchanged. Now I can say a sequential search and we I think that this was somewhere in chapter 11 or 12 where we learned about the sequential search. Yeah, it's chapter 12. So we've already been through chapter 12. And let's write a quick sequential search here. We'll set index is zero. And as long as the index is less than sizes dot length, and it is not the case that sizes sub index is equal to the size we're looking for, then we'll move the index one further. And then if the index is equal to sizes dot length, that means that we didn't find it. We'll set result is twin. Otherwise result is going to be sizes sub index and we'll return that result. And that also means that I have to have a string for the result. Then I can say this dot size becomes valid size of the size you gave me. In fact, let's call this to valid size. So that will convert it to a valid size. And let's compile. This is called blanket sequential. So that means I have to change all the names here. Let's compile that. And it looks like I am missing a parenthesis here. Excellent. And then what we're going to do here is in our blanket if test, we will change the name to blanket sequential test. Then we're going to do a change of blanket if to blanket sequential and change all of those in the selection. Let's compile that and run it. And again, it does exactly what it's supposed to do. This to be quite honest is probably our best bet is to use an array and do a sequential search through it. Why, because there's only four items. Again, for the country search, if I wanted to find out if that was a valid one, I could do a sequential search, but again, just to figure this out since there's how many countries are there anyway. Looks like there's 27 here. And on the average, we'll have to search through 13 to be able to find something. If it's in the list, sometimes it might be at zero, sometimes it might be at the last one. There's 28 excuse me is 28 starting at zero. So on the average, we're going to have to do 14 steps through it. Now, another way that we can do this is with the binary search. Okay, and let me put this in the notes again. So the sequential search takes on the average. And over two comparisons for a list of length and the binary search takes at most log to the base two of N comparisons for a list of length and so for our 28 EU countries. That's five comparisons. And for our 28 EU countries. That's 14 comparisons. So when we get 28 items, that's definitely going to make a difference. Remember, though, that the array must be sorted into order for a binary search to work perfect to work properly. Now, when we have four items, again, the average number to search is two. And the bind logged to the base two of four is also two. So there's really not a big advantage in using the binary search. However, just for the sake of completeness, I'm going to put it in here. So let's do this and save this as a blanket binary dot Java. Now we have to get these into alphabetical order, which is going to be full King, Queen and twin. Now the question is, I do I want to write my own binary search and the answer is no, I really don't. It turns out that in the arrays. Remember Java dot you tilled out arrays. That happens to be a binary search already written for us. And so rather than me right at myself, I'm going to do it for the binary search. And you're going to have an array of objects strings or objects. And you're looking for an object that is the key. It's going to return the index or negative one if or a negative excuse me a negative number if not found. So instead of all of this, we're going to say in index becomes Java dot you till dot arrays. Again, if I could or I'm importing it. Okay, so I don't have to do that. Excellent. So I can say arrays dot binary search sizes, and I'm looking for the size. And then if the index is less than zero results is twin. Otherwise result is sizes. Subindex. I guess that code didn't have to change. And let's go here and let's do a change everything from sequential to binary. And let's save that as blanket binary test dot Java. And again it works fine. Just so that you can see what's really happening here by the way. Let's do a system dot print f index of the size is percent D. And we'll say what is the size you gave us. And the index value that came back as index of full was zero index of extra large was negative five, which means it was not in the array. And to be quite honest, if I wanted to do the country search, I think I would use a binary search for this. First of all, I could use the one that's built in so I wouldn't have to write it myself. Although again, if I did want to write it myself, you can go and look in the book and you can see how a binary search is written. We did this in an earlier mini lecture so I'm not going to go and reimplement it this time. So that would be my choice for 28 countries. And again, just make sure they're all sorted into order before you start. And now the third way, which was the original way that I came up with this is it turns out let's go back to my original blanket dot Java. And let's go to array lists. If you have an array list. And again we've talked about those before. There's a method called contains, and it returns true if the object is in the array list. False if it's not in the array list. And that was what I wanted to use I figured oh that's going to be the easiest way to do it. The problem is that can array. This is this contains method belongs to the array list class. It doesn't belong to the arrays class it doesn't belong to arrays in general. And so that means I have to convert my array of names into an array list. And that's what this whole business was here. In fact, I'm going to break this into several lines so we can see what's happening in stages here. So here's what this code was doing, reading from the inside out. And remember the way parentheses work for us. We're going to get an array with the values that we want twin full queen and king. Now, we have to convert them to something called a list. And that's a Java class that does all sorts of really magical and wonderful stuff with lists, hence the name. Once we have a list, we can then use that list. We can then convert it to the constructor, and that will create a new array list of strings. And again we called it final because once we set it up we're never going to change it. So I went through this whole big business just so that I could get an array list. I can then use the contains method and say, okay, is this size inside of the sizes array list. Essentially what I was doing is I was trying to be too clever. And I was saying, oh, a ray list has this contains method so let's use that and then I had to build this whole structure around it. And this code is here, because I needed to convert an array of strings to an array list of string. Also that I could use the contains method. There is a fellow. His name is Ryan Kernigan. I believe the other guys named PJ blogger. And they were writing a book on programming style. Let me pause this and see if I can find my copy of it. I can't find my copy of the book but luckily the thing I was looking for is summarized here in the Wikipedia article. They wrote a book called the elements of programming style. The first chapter says right clearly, don't be too clever. And unfortunately that was what I was trying to do I was trying to be too clever by using the array list. And the book again is called the elements of programming style. It's a great book if you can ever find a copy of it. Well worth reading. It's an old book but it's still really good. So there it is right clearly don't be too clever. And unfortunately that is exactly what I was trying to do here. Probably my best choice would have been to either use the binary search. Or, again, because there's only four numbers or four if you need me four sizes to write this sequential search. And that probably would have been a much better way to do it. So that is an explanation of what I was trying to do originally and a better way to do it. And if I had had a longer list like again the EU countries, then the binary search would have undoubtedly been my best choice. And that is an in depth view of cleaning up the mess that I created last last night. So that's tonight's mini lecture.