 Okay, let's pick up a little bit before I accidentally turned off the audio in the lecture this morning. And we're looking at this framework for doing timing tests so that we don't have to duplicate code every time we want to test something. We're going to need the number of trials that we do for the test and how many we keep. Remember the first few of them were doing that so that the Java virtual machine has time to settle down. By default we'll do 25 trials of the test and keep the last five. Or you can use a constructor that tells how many trials and the number you want to keep. Or you can use the getters and setters. So there's nothing particularly new there. Now let's do a little bit of what's called pseudocode, which is half English, half Java, that tells what we need to do when we're doing our tests. The first thing we need to do is set up any variables, files, whatever, before we do any of the trials of our test. Then we're going to have a for loop, i less than the number of trials. And for each of these trials what we're going to do is we're going to set up any variables, files, whatever. For this specific trial. And then finally we're going to do the test of the thing we want to time. Then we can tear down any of the variables for this specific trial. So for example if I had created a file I might want to delete it later on. Oh there's something else I've got to do here. This is just a big overview and I'll get into the specifics when I look at the code. And once the trials are done then we're going to tear down any variables that we built before the trials begin. So these are the generic kinds of things that we want to do in our test. Now let's see how those are implemented here. I have these methods set up is the stuff that we execute before any trials run. Before each test we'll do the pre-test method. The test itself is do test. After the test is done we do post test and we tear down after all the trials have finished. Now how do we run a test? Well first we want to keep the timings. So we make a double array that has as many as we want to keep and set the index to zero. We'll call the setup method. Here is our loop. We'll do the pre-test and then we're going to call the garbage collector that's system.gc. In case there are any objects that are lying around in memory the garbage collector can clean them up. There's no guarantee that it's going to do it right away but it's better than nothing. Then we have to figure out what the Java virtual machine thinks the time is. And finally we can do our test and then figure out how much time has elapsed. Is this one of the trials that we want to keep? If so we'll add the elapsed time to our results array and go on to the next index. And then anything that we need to clean up after the test is done we do. When this whole loop is finished we tear down which undoes everything that we did in setup if necessary. And then we can return that array of doubles which are the last N keep times for our test. We can use that array here in get average test time. We'll run the test which gives us our array and then we'll figure out the average and return that. So again this is a lot of stuff that you'd have to do anyway every time you do a timing test and now we've put it in a place that we can get to it easily. So let's look at the fourth anagram one that we did. That was on Monday I believe. And we wanted to find out if two strings are anagrams and the way we're doing this one is counting how many occurrences there are of each letter. And that's supposed to be a linear algorithm. So we're going to change this. Actually let's save this as. And unfortunately I had this earlier so I'm just going to rewrite it. It may not be the same as the one that got uploaded earlier but that's because I'm doing this on the fly and isn't improvisation exciting. And we're going to call this anagram testing. Now we want to update our comments here at the beginning. And so what we're going to do is we want to find out if this is linear. That is to say ON, capital O, thank you, algorithm. We'll test strings of length 2600, 5200, 7800, and 10400. Those are multiples of 26 and we'll build a string with copies of the alphabet. In order and mixed up. So that's the plan of how we're going to do this. And here is our anagram solution 4 which is the one that does all the work to determine whether something is an anagram or not. And we're going to use that later. Now what we need to do is we need to go and have a class called timed anagram and it's going to extend our timed test framework. Great. What do we need in here? Well the first thing we want to have is the number of copies which is the number of copies of the alphabet to string together. And we may as well make that private. It couldn't hurt. We'll need a private string S1 and a private string S2 and these are what we compare for anagramness. There probably isn't such a word but there ought to be. Now to save a little bit of typing I have the alphabet and the alphabet mixed up and I will put them in here and the fact that they aren't private or public doesn't really matter. But I'll just keep it as it is. Now we need a constructor. And so we are going to have public timed anagram and we're going to give it a number of copies of the alphabet to test. We'll do a super class constructor and in fact let's do this. We're going to do the super class constructor and give it 50 trials and keep the last 10. And then we will set this dot and copies becomes math dot absolute value of n copies. Not like anybody is going to give us a negative number but you never know. By the way, let me put the closing brace in here just so that for the, oh I already have the closing brace in there. Okay. Okay, what do we have to do? We need a setup. So we're going to override the setup method and that's going to be public void setup. And what we're going to do here is we're going to set S1 to the empty string and S2 to the empty string and then for I0 I less than this dot and copies this dot S1 plus and becomes alpha this dot S2 plus and becomes mixed. By the way, this plus equals, that's an abbreviation. Instead of having to say something unless I have an integer n, instead of having to say int n becomes n plus 5 I can say that as n plus equals 5. I can do that with all my arithmetic operators. If I have a variable x and I want to say x becomes x divided by 3.2 then I say x divided becomes 3.2. For simple variables it's normally not tremendously useful but if I have something on the left hand side of the assignment that's really complicated like array of i times 3 plus j times 2 minus i plus j and let's say I want to add 7 to that. That's a heck of a lot easier than having to repeat this whole thing on both the left and right hand side of the assignment operator. There's a quick side trip for that. Let's see, what else do I need to do for my setup? That's about it. There is no tear down necessary. When the program ends the strings will get deallocated by the Java virtual machine and everybody will be happy. There's nothing that I have to do specific to any test before or after so pre-test and post-test can very happily do nothing. What I definitely need to overwrite is the public void do test and what it's going to do is it's going to call this method here. Now that's a static method. To call a static method from another class you give the class name dot and then the name of it anagram solution 4 would be nice if I typed it correctly and we're going to give it this.s1 and this.s2. This returns a boolean although we don't use the result of the test but just for the heck of it I could say boolean is anagram becomes that. Let me compile that to make sure I haven't mistyped a whole bunch of stuff and surprise I have I forgot to say extends not extend there we go that's much better by the way I could do a system.out.print line of is anagram and then every time it does a test you would see whether that came out true or false so if you need to do some debugging that's the place to do it but for right now I'll comment that out. Okay now we get on to the main event we have public static boolean that hasn't changed that code still tests whether something is an anagram now what we have to do is we have to do something very different what we're now going to do is we're going to say okay how many copies of the alphabet are we going to use and that is going to be 100, 200, 300 and 400 so we're going to call this copy count is going to be 100, 200, 300 and 400 copies of the so let's put in 500 just for the heck of it and by the way so that's I've got to change my documentation and 13,000 and we'll put this on a separate line here there that cleans things up a bit now for int count in copy count and I'm using the enhanced for loop because it's a little easier to write what I'm going to do is I am going to create one of these guys a timed anagram and we'll call it a that's going to be a new timed anagram and it's going to be however many things we want and then I'll say my average is going to be time test dot get average time what was the name of it here get average test time and then we'll print out the results so length percent comma 6d takes percent dot 8f seconds and that's going to be our count times 26 and the length time is going to be average so notice that our testing itself is now very compact because all the work is being done by the framework okay let's compile this and let's run it and see what happens oh yeah I guess I have to be in this in my program that has the main method before I now you'll notice this first one takes a lot longer than the others which is sort of weird what we can do and I'm not sure why this happens again Java virtual machine is really sort of weird so what I'll do is I'll put another 100 this first one apparently seems to take longer so I'll do it twice and now here are our real data items that we want now let's go and see if that's linear or not let's go into LibreOffice calc open up a spreadsheet and so this is going to be our length and time and here we're going to call it I'm just going to divide by 100 here and we'll say 637 I'm going to multiply these so that we have reasonable looking numbers 52 is going to be okay we're going to have to put his leading zero in there otherwise it's definitely not going to work 0.1028 78 goes as 0.1234 104 the fact that I have I'm not using the extra zeroes and doing the multiplication the graph is still going to look the same just trust me on this that's interesting it went down for that and 13 is going to be 0.1773 I don't need to I'm not going to save it now let's go here and let's insert a chart and we're going to use an XY scatterplot with points and lines and there it is and unfortunately this last one here is sort of weird and then we got that it would be pretty linear if it weren't for that one let's just retry this and let's run it one more time that's not going to do it when you see something strange like that happen this time I've got some much better numbers because they seem to be in a rational sequence so let's try this one as 0.0866 0.0818 it went down again 0.0919 0.1616 and 0.1874 and I'm not sure why we're not getting a straight line here when I did this earlier in the morning I was getting something that was really nicely linear and all of a sudden this time it's not doing what I want that is not the main point of this it certainly isn't quadratic we can see that it's not quadratic so the point of this is teaching you how to use this framework let's go back up here I'm just curious to do this let's try 0.15 keep the last 20 I wonder if that will make a difference you might have to play with this to get the numbers now everything is going in the right direction so 0.069669 810 and I'm sorry there's somebody outside with a leaf blower so you're probably going to be hearing that in the background of this but there's not much I can do about that at this point it went down at the end of course 0.1305 but you can see that we are getting a linear effect here and the next thing I did earlier in the morning was I went over the assignment and the easiest way to do this is show what the result of it is so we want to verify that index of is linear order n and very similar to what I did with my counting earlier I'm now going to have different array list sizes of 100,000, 200, 300, 400, 500, and a million I'll set up an array list with those integers start a timer and generate 10,000 random integers in that range calling index of with that random integer as an argument and then figure out how long it takes to do that so I'll do that steps two through four 25 times averaging the last five timings and here are the results of running the program much like I did here now you could either write this yourself you could do all the timing on your own the way it is in the book let me bring up the book here for example here under array lists where I was trying to find out how long it took to remove an item so you can write the loop yourself or you can use the framework I don't care which way you do as long as you get it done and you can put in a graph if you'd like to and but it's important that you give me the results of your test and also upload your program because I'm going to be uploading I'm going to be running the program also to see what kind of results it gives and if it doesn't come out as a perfect straight line just like mine didn't today don't worry about it the purpose here is to get you to understand how to do a timing performance on some algorithm and we'll worry about getting everything clean and getting the Java virtual machine out of our way which I think is a little bit above my pay grade at the moment the other thing that I had here for today was this lab and this was calculating the variance okay, variance is a measure of how spread out numbers are so for example if I had here these numbers let's say 48, 50, 46, 48, 50, 52, 54 and these numbers 10, 30, 50, 70, 90 both of them have the exact same average so here is the averages are the same but these scores vary more widely they are more spread out and therefore the variance is larger and there are two ways to measure the variance to calculate it rather namely you can sum up the squares of each element minus the average and then divide that by n minus 1 or you can take n times the sum of all the squares minus the sum of all the elements squared that's not not the same thing and divide by n times n minus 1 and one of the things that I uploaded earlier is let's see here static variance calculation dot java and this has static methods to use the classical formula and the computational formula and here I'm creating an array of random double numbers in the range 0 to 100 let's compile that and run it and they both give the same result for the variance when you do this test it turns out you really don't need to put any numbers into your array you can fill it all with zeros because if you look at the code here there are no if statements the calculations that are done are exactly going to be the same no matter what the numbers are you're going to have the same number of calculations being done so the question we're going to ask is the classical formula linear or quadratic or what and is the computational formula linear or quadratic or what and the way we're going to do that is we're going to do timing tests with the time test framework or at least that's what I'm going to upload later on today now you may not want to go ahead and do the lab because it's a little bit more complicated than the assignment but if you want some extra practice after you get done with the assignment then be a good one to take a look at and that is the lecture that I did and am now redoing and I've gone through it a little bit faster but I think I've hit all the main points so again there's no class on Monday the 19th so I will see y'all on Wednesday the 21st when we will begin discussing Chapter 3 which are basic data structures and we'll start off with stacks which are incredibly useful and you find them all over the place in computer science