 Hi, today's mini lecture is going to be about the assignment for strings and loops and let me close this which I don't need. Let me erase the message window. And let's go here and look at part one. So in part one, we're going to be asking these to repeatedly enter a sentence until they press only enter. And we're going to tell how many vowels, constants, digits and other characters, namely anything that's not a letter or digit are in the sentence. So the question is, how would we do something like that? So let's do something that's a little bit similar. And what I'm going to do here actually I'm going to get rid of this trim here because that is not what I want at the moment. So given a string, I want to find out how many characters are right angle letters. And these are the letters that can be written in uppercase with only right angles. And those are letters E F E F H I L and T. And we've gotten the I put in this loop here already because we've done it several times and I didn't want to spend time re typing it. Let's just test that to make sure that that works. And so I print anything. Okay, cool. That's all working. And I'm going to put in a system to out dot print line. Readability about put. And those by the way these comments are telling me why I'm doing things not what I'm doing. I'm printing a blank line. Why am I doing it. I want the output to be more readable. These comments are what because when I'm describing the purpose of the program that tells us what the program does is not going to be a why or how. I don't know how to go through all the letters of a string one at a time. In fact, I would do this for the moment let's keep this in output here. And what we're going to do is we're going to have integer. And that's going to be let's call it and right angle. That's going to be the number of right angle characters. And it starts off at zero for every sentence. Then we're going to say for integer. Index is zero index less than line dot length index plus plus we're going to use a for loop to go through it one at a time. And then we're going to say our character. CH is going to become line dot char at index. And let's just see that this works properly by the way. Let's make sure that this part is working before I go any further. So I say, okay, cool. I'm happy with that. Now what we're going to do we're going to have to ask, okay, is the character one of these when we convert to uppercase. That means that the first thing we'd like to do is we're going to have to say, I don't know if I can do this with a character. This is going to be interesting. Let's see if this works. I may have to change things around a little bit, you know, when I want to try things out rather than do to my program, I think I'm going to use J shell. So if I say CH is lowercase a. All right. Can I say CH becomes character dot to uppercase of CH is there such a thing. If there is, okay, cool. There happens to be a two uppercase for characters. I am a happy person now. So I'm going to translate it to uppercase that way don't have to do testing for both upper and lower case. So I'm going to use the two uppercase method that's in the character class and that will translate to uppercase. Avoid having to deal with both upper and lower case. Again, that's why I am doing this. Now I could do this the hard way. I can say, okay, if the character is equal to E, or the character is equal to F, or the character is equal to H, or you can't. The character is equal to I, or the character is equal to L. Why this is a, I'm glad I had chosen a small set of subset of letters here. Then the number of right angle letters. Plus plus. Otherwise, if it's not one of those, I don't want to do anything at all. Now, once I get out of this loop, I can say system dot out dot print line. You had percent D. Right angle letters. And that's not print L and I'm sorry. This is a print F. That'll work much better. And let's say I also want to find out. Also. So user, how many. Characters were not. Right angle letters. Now, does that mean I have to do here something like otherwise. The jury is asked to disregard that I was going to do something really weird and it's not going to work. Okay. It says it's going to work but it's not what I want to do here. So the question is how many other letters are there. The easiest way to find out that out is to say the number of others, or let's call it and non right angle is going to be lined up length. Minus the number that had a right angle. The subtraction will give me the answer that I want. And then I can say percent D. Characters were not right angle. And that'll be in non right angle. There we go. So let's go and compile this. And let's try a word like the, I should have two letters that are upper that are right angle letters and one that isn't. Well, that certainly didn't work very well did it. Oh, that's right. T, H and E are all up. So let's try a different one. How about that? The T, H and the T are going to be right angle letters and A is not. There we go. Three right angles and one characters were not right angle letters. Perfect. Okay. And let's try something like helper for right angle letters, the H, E, L and E and the two that P and the R. And if I have something like ABC, D, E, F, G, H, I. So it's going to be counting these spaces. Now the question is, do I want to count only letters? If I want to count only letters, then I have to change my logic, but let's leave this for the way this is for the moment. Here's the problem. Well, no, no, let's put this in here. This is a good idea. Okay, now I'm getting where I want. This is perfect. So we're going to have to say the number of non right angle and we're going to count letters, not characters, but letters only. So that means if we get a comma or a space bar or a question mark, we don't count that because it's not a letter. Now, do I want to say, first of all, else if CH is equal to a or CH equal B, you can see that this is going to go on for a long time. We need a better way to do it. One way that we can do it, and this is sort of sneaky, if the character is greater than or equal to a and the care. No, I don't even need to let's let's let's do something better. Remember, I want to be able to do things with accent marks. So if character dot is a letter, then the number of non right angle plus plus, then I don't need to do this length minus business. Some of this, by the way, may be relevant to what you're doing when you're doing the assignment. Some of it might not be. I'm just exploring this particular program at this moment. Let's say it's a letter B is an E and F and H and I L or T no, but it is a letter. And therefore it's not a right angle letter. What if we have the letter F CH is equal to E no character equal F yes F is equal to F and that means this whole thing is true. Remember we can early exit. That means we'll do this will add one to the number of right angles, and we won't do the test. It's a letter and add it to non right angle, because the if came true, we ignore the else. This should do what we want. And I need to say which which which character is a letter there we go. So let's try that and for exclamation points. I'm going to go up with three right angle letters and one non right angle letter and all those exclamation points should just be ignored. And sure enough that's exactly what happened. So this comma that I should have 123456 letters that are right angle, and two of them that aren't the s and the a. Perfect. That's exactly what I wanted. Now, this is still bothering me, because if I had a whole bunch of things and specifically, I'm looking at you consonants, because there's a lot of consonants in the alphabet. If I did it with an if statement like this, this would turn out to be really, really ugly. Let's go to J shell and do a quick experiment here. Let's say I have here a string called right angles. And I give it E F H I L and T. And now let's have a character CH and have it be let's say a capital M. I can say right angles. Let's find the character inside of right angles. That is negative one that means that M is not one of the right angle letters. What if I say index of capital T that comes back as five right angles dot index of E comes back as zero and right angles dot index of let's say a would also come back as negative one. So that's how I can tell if this is one of my right angle letters, I can create this string and use index of instead of having to use this whole long compound condition. This whole long compound condition is not wrong. There's nothing wrong with it. It works exactly as advertised. I just want a better way to do it. Let's say this is right angle letters to and we'll call this system. And what we're now going to do here is we're going to have final string. Let's call it right angular is going to be E F H I L and T. And now I can say if right angular index of whatever character I'm looking for is greater than or equal to zero. That means it must have been inside this string. And that will give me my N right angle. Otherwise, check to see if it's a letter and if it's a letter, then it's a non right angle letter. So if we're going to do something like this comma that it works exactly the same or the as three right angle letters. So this is a semi largest hint on an approach that you might want to take when we are looking at the first part of the strings and loops assignment. Now let's talk about the second part. Something called the standard deviation and I just added this by the way today, what an explanation of a standard deviation is. We're going to repeatedly ask the user to enter a price of an item or negative one when they're finished. That's going to be our Sentinel value. And if you read the book, you know what Sentinel value is. It's a special value that we use to detect when the loop should end, as opposed to continuing. Then we're going to compute and display the total number of items, the average price price and the standard deviation of the prices. So what's the standard deviation business. So you may want to read this. I'm going to go over it fairly quickly here. So let's look at these first set of numbers 1020508090. One way to describe a set of numbers is by its average. So for example, if I have two sets of people, and one of them their average height is 120 centimeters, and the other one, the average height is 130 centimeters. I know that on the average, the second group probably has taller people than the first group. Here, though, I have two sets of numbers and they both have the exact same average. But if we look at them on the number line, they're distributed really differently. This first set you can see is spread out a whole bunch more than this second group. If these were test scores, let's say, I would say these people are a lot less. They have a lot more variety in their test scores than these people. These people are all pretty much right around the center, whereas these people are really spread out. So as an addition to the mean, the average, we would like to have some measure of spread outiness. Well, one way to figure out how spread out they are is to say, okay, let's say these are mile markers on a road. So we've got a mile marker at 1020508090. How far would I have to drive from 10 to 50 plus 20 to 50 plus 80 to 50 plus 90 to 50 in order to figure out how spread. The more I have to drive, the more spread out the numbers are. Here I don't have to drive as far from here to this average point is a lot less than from this one here to the average point. So by adding up the distances from my points to the average, that gives me some measure of how spread out they are. So for the first set, it's 40 plus 30 plus 20 plus 40 plus 30 plus 30 plus 40 and that adds up to 140. And for the second set, it only adds up to 52. So the larger this sum of the distances is the more spread out we are. And here's how we express it in mathematical terms. So this means some sum not so me. So this is the addition of the absolute values of each of the items in our X set of numbers minus the average of X says X bar is how we pronounce it. So in fact in math I would process some of the absolute value of X sub I minus X bar. That's how I'd say it. And this is a perfectly reasonable measure of how spread out the values are. There's only one problem and that's that we're using the absolute value function and mathematically that is not a nice function to have to deal with. If you've taken calculus you know that trying to differentiation with absolute value is problematic. So let's we need these distances all to be positive. If we use negative and positive it'll always add up to zero. And so that's totally useless to us. So how can we get all the distances the center be positive. Well, if we square them, they're guaranteed to be positive negative numbers squared turn out to be positive. So for that first set of numbers. It works out to 5000 and for the second set, it works out to 776. Again, the first set of numbers is more spread out than the second set. And mathematically that's the sum of X sub I minus X bar quantity squared. Okay, so far so good. Here's a little sort of problem though. Let's look at these three numbers. These are just as spread out as this second set here. You can see from 32 to 68 are the endpoints. And here they are also 32 and 68, but the sum of squares won't be as big because there aren't as many numbers. Because here I had five numbers. And I was adding up there's squared distances. Whereas here I'm only adding up three numbers squared distances. So I'll take that into account by dividing by the number of items minus one to make that and that's the only reason I'm divided by n minus ones because statistically, that makes things work out better. I'm not going to go into long detail explanation trust me on this one. And this by the way this formula is called the variance. Now the other problem is when we squared the numbers like 68 squared is pretty darn or 68 minus 50 that's 18 squared, which is a pretty big number. In fact, if you look at this sum of squares, we got these ginormous numbers 5,776 as opposed to 140 and 52. Well, one thing that we can do is we can use a square root of the variance and that'll cancel out the squaring sort of speaker. And this is the formula for the standard deviation. You take each item and subtract the average square that add them all up divide by n minus one and then take the square root of it. That's the classic standard deviation formula with one small problem and that's that I need to go through all the numbers twice. I need to go through all the numbers first time to get the average. And then I have to go through them all again to compute x, each individual x minus the average. So I have to go through all my numbers twice well if I had only five numbers no big deal. If I have 5000 numbers. That turns into a lot of work. So it turns out, there's some nice algebraic magic that you can do with this equation. And then it translates to this formula which is the one I want you to use it's the one in the assignment. And with this one I need to process each number only once. So let's look at this this says I want n times the sum of each item squared minus the sum of all of the items and square that. In fact, I should probably bring this up here. Let's see if I can find this. Oh no, it's here in my browser. Find the browser again. Excuse me let me move it. So this will be the sum of the squares and the sum of all of the items squared. Now this is all very abstract so why don't we do this calculation a calculation by hand to calculate the standard deviation. Let's have our numbers that we're going to be using. We're going to have 12, 7, 3 and 20. So my first item is 12. And then we're going to have a negative one which is going to end our loop. We're not going to count it in. So item is 12. Well first of all before we start by the way what's our sum of some zero what's our sum of squares, also zero. Let me do something here real quick. Let's try to find this here. So some of the squares zero and the number of items we have so far is zero. Okay, there now I'm not much happier. So my first item is 12. That means I add zero plus 12 which now changes that to 12. I take 12 squared which is 144 and zero plus 144 is now 144 and I now have one item. My next item is going to be a seven. Seven plus 12 gives me 19. Seven times seven is 49. I'm going to have to do this button on a piece of paper here 144 plus 49 is 493. And now I have two items. My next item is going to be a three. Three plus 19 is 22. 193 plus nine is 202 and is three. And my last item is going to be 20. 20 plus 22 is 42. 20 squared is 400 plus 202 is 602. And I now have four items I get the negative one and I'm done with my loop. Now I'm going to plug it into this formula here. I'm going to do this and I'm going to have, let me just write it down here. It's going to be four times the sum of the squares of the prices which is 602 minus 42 squared. And I'm going to divide that all by four times four minus one. So let's see what that comes out to four times 602 is 2408. And then let's put it, now let's, let's show, should I have, yeah, let's have Excel do the arithmetic or the spreadsheet do the arithmetic for me. Four times 602 minus 42 times 42. Divide it by four times four minus one. And now I've got to give and get enough parentheses here so that everything is in the right order. Now comes out to 53. And then we want to do the square root of whatever's in cell 813. So this should be our standard deviation should be 7.325. If I did my arithmetic, right. Let's try to check this. Let's put in those numbers again which were 12, 7, 3 and 20. And here let's have the spreadsheet asked for the standard deviation of a seven through D seven. Yep. And there it is. And here's what I did every time I got an item. I added it on to the sum. I squared the item and added on to the sum of squares. And I added one to end. You know what this is worth writing down in my notes. And of course I forgot to save a file for this as usual. Curse me. Yeah. Where are we here. So, every time I get a number from the user. I got to set things up. So I have to set my sum to zero. I have to set the sum of squares to zero. And then I have to set the number of items to zero. So every time I get a numbers. If it's not a negative one. Then I'm going to add one to the number of items. I'm going to add the item to the sum. And I'm going to add item times item. To the sum of squares. As long as I don't have a negative one. Now I have in the sum. And some of squares. And I've popped them into the formula and the assignment. And I'm going to add the sum of squares to the sum of squares. There's my standard deviation. Now there's one slight problem that we need to worry about. And that's what if N is equal to one. If N is equal to one, we can't get the standard deviation because we'd be dividing by zero. So we have to put some code in there to make sure that we don't print things out. When we don't want them. If the total number of items. We can't get the average average. So let's say. Something like no data and no results. Otherwise we have at least one item. If we have only one item. Then we can, well, we can always strike an average. And if there's zero items, we're out a lot. We can't get an average of no items, but we always can get the average. Now. We can't get a standard deviation. Because N minus one won't be zero. Otherwise. We have only one item. Which means we need to tell the user we can't. Get a standard deviation. What I've done here is I've written something called pseudo code. Um. Oh. This part here is going to have to be in the correct places in here. Okay. So I can't just. Remember after I get done with this loop, I can't just say, okay, now I'm going to calculate the standard deviation because I might not be able to. If they gave me zero items or one items, I can't do it. I'm going to make sure it's okay for me to do it. This way. And this is what is called a pseudo code. Half English. Half Java. And it's a good way to write a plan. It's a good way to write a plan. And it's a good way to write a plan is not engraved in stone. I said, oh, okay. Well, let's just, you know. Pop it into the formula and tralla. There we are. And then I said, oh, wait a minute. I might not always be able to do it. So I had to change my plan. I had to add to my plan. And I might have to move this to a different place in the plan. So your first plan is not engraved. In stone. But again, at least I'm going into this program with a plan rather than just say, okay, let me just write code and throw it against the wall and see what sticks. That's a really bad approach. And it really tends to waste a lot of your time. So that I think is what you will need to know. Or these are, these are some rather generous hints for the part one. Which will use a lot of the stuff that we did in the right place. And then we wrote down the letters. Not the exact same logic, but it should give you some idea on how to plan it out. And the second part. Where we did a calculation by hand, just to see that we understood the process. And that's usually a very important thing to do. And then we wrote down, okay, here's what we did. And we wrote in half English half Java. And then we got an idea of, okay, here's what we're going to have to do to make this happen. And that is today's mini lecture. See y'all next week. And what I'm going to do probably on Monday, I know there's a midterm coming up. I think it's coming up on October 4th. I'm not sure. Let me check this out real quick. Yes, midterm ones on October 4th. So it's a consists of a part where you're going to be doing questions and answers. And there's a second part of it. Also on the same day. Where you will be expected to. Write a program. And together, those add up to 75 points. So I'm going to talk about that on Monday, that that's going to be the topic for Monday's mini lecture. See you then and have a good weekend.