 Okay, first let me apologize to all my Python peeps. This is a Java lesson, something I'm putting together for my students, especially I had a couple of missed class. So problem solving and iteration. So when we see the word iteration, we know we're gonna be using a loop because we're going through a sequence. In this case, it's gonna be a string. One of the things I always tell my students is that don't start coding the solution until you actually really understand the problem. It really helps to write these things out, helps to really think through in a step-by-step fashion. Once you do that, the coding's easy. But if you're, especially a beginner, it takes a lot of mental energy to do them both at the same time and there's a lot of mistakes. But there are some common patterns you do see in code. So for example, if you're looking for the sum of some set of numbers, you know that you have to create a variable called sum and that's gonna be outside your loop and when you start it, the sum's gonna be set to zero. So here's just a couple little exercises I wanna work on and this will kinda help build up some of the skills that we need. First one's pretty straightforward. For example, how many letters are in the string called str, okay? So I'm gonna go over to, I'm gonna be using j-grasp and I'm gonna go ahead and I'm gonna make a string and I'm gonna say string str and I'm gonna say equals a, a, b, b, c, c, z, d, d, d, e. Okay, just for example and if I wanna know how many letters are in that, I'm gonna say system.out.println and I'm just gonna say print str.length and don't forget the parentheses because I often do and I'm just gonna run that and hopefully I will see 13. So there should be one, two, three, four, five, six, seven, eight, 10, 11, 12, 13, 14, 15. Did I count that wrong? One, two, three, four, five, six, seven, eight, nine, 10, one, two, three, wow, I cannot count but yes, that is the answer to that one. Let's scoot back here. It says, assume you have three integers represented by the variables a, b and c. Using three if statements, how would you find the greatest number? And do not use else, if or else. Now I know we can write this more efficiently using else, if or else but for this particular activity we're trying to learn the concept of using an external variable to do this. So you'll see what I mean in a second here. So what I'm gonna do is I'm gonna go ahead and get rid of this. I'm gonna keep string there because I'm gonna need that in a minute anyway. So I'm gonna say int a equals 10, int b equals 20 and int c equals let's say five. So looking at this problem, ask yourself what do you want to know? And this is the real key to this question. What do I wanna know? I wanna know the greatest number. So what we came up in class was int max int equals zero. Now we're gonna just assume that it's a positive integer from zero to whatever. Just to simplify the code a little bit. So we're starting out at zero. This is the lowest possible number that we can have because we wanna find the largest. So what I'm gonna do, and this is what a lot of students do this. They say, if a is greater than b, et cetera, et cetera, et cetera, which will work? It's not a bad idea, but if we extended this a, b, c, d, e, f, g, h, i, j, k, we wanna have something that's gonna work no matter how many variables we end up using. So if a is greater than max int, because we're just basically comparing it to the maximum, then max int equals a. And then this pattern will follow on from there. So I can do b, b, in this case, c, and c. Yes, there's some other things we can simplify a little bit, but let's just keep it like this for now, because this makes a little more sense to what we're gonna do later. So I'm gonna run that, oops, I gotta print that out. So system.out.println, max int. Okay, so we should get 20 because b is the largest. Okay, and we do get 20. Now if we wanted to properly test this, we should test it with all possible options where a is larger just to make sure it works. Now we're still getting the right answer, which is nice, and then I can make this 20, make this five, but I think I'm pretty confident this is going to work. It's fairly simple, straightforward code. So that is how we do this type of pattern. If we're looking for the largest or we're trying to keep track of something, we're trying to find something, we would use some type of variable to hold that information. And then we compare our possible choices, in this case a, b, and c, against that, and if a meets that criteria, then we set it to a. And if b meets the criteria, we set it to b. So just remember this pattern because you're gonna see it time and time again. Okay, so I'm gonna go ahead and actually get rid of that. Let's roll up a little bit here. So keep in mind I still have that string there because I'm gonna be using that in a minute here. So let's take a look at number three. How would you find how many uppercase vowels are in a string? So this is where it really comes in handy trying to write out the problem. Okay, so we gotta find out how many uppercase vowels there are in a string. So I've got a string here. So what I'm gonna do is I'm gonna say, okay, create a, you know, let's say, I'm gonna call a list but it's not accurate. Create a, well, decide what vowels we have, decide the vowels, okay? I'm gonna say I'm gonna iterate through the vowels. Then I need to iterate through the string, okay? And then I need to compare each vowel to each string. So each, sorry, character in the string. That makes sense, okay? If the vowel is equal to the string, to the character, sorry, then increment the number of vowels, okay? And then when I'm done, I'm just gonna print the output. So this is basically how we do it. We need to decide the vowels. We also need to know the maximum number of vowels. And we're gonna start at zero. We also need to keep track of the current number of vowels. I'm also gonna set to zero. Notice, just make your code a little readable. I've used num of vowels and max num of vowels. So I know these two are somehow related. So what we do here is I'm gonna make a little string and I'm gonna call this vowels, which kind of makes sense. And in this case, we're just using A-E-I-O-U. We'll ignore the sometimes Y for now. So now I'm just basically writing the code. So iterate through the vowels, okay? So for int I equals zero, I is less than vowels dot length and I plus plus. This will let me iterate through every single vowel. So start with A, then I, E-I-O-N-U. So now what I might do, depending on how you wanna do it, I wanna indent here, because now we're inside of a for loop. Now it says iterate through the string. So I'm gonna say for int, I'll use j in this case, and j is less than string dot length and j plus plus. Okay, so I got that and I forgot my closing braces here. Yeah, let's see here. Sorry, I hate to lose the track of the cursor there. So again, I might want to indent that, okay? So now I'm just following what I wrote out, okay? Compare each vowel to the character in the string. Okay, so this, what I'm gonna do here is I'm gonna go ahead and make a little temp variable that I can use. So string vowel and string variable, sorry, character, character. And I can just, it makes it easier in your head, we can set those equal to empty string rather than null. So what I'm gonna do is outside the loop, I need to know what the current vowel is. So vowel equals vowels dot substring i comma i plus one. Memorize that pattern because you're gonna see that quite a lot. And then here, I'm gonna get the character equals str dot, what is it, substring and j in this case, j plus one. Okay, so now basically I can just write it out practically in English. So if a vowel, let's say if character, that says vowels is vowel, vowel dot equals character. If they're equal, we just increment the number of vowels. Pretty straightforward, put that back. And so num of vowels, plus, plus, and I miss anything. Oh, actually, I didn't, is this question the maximum number of vowels? No, sorry, that's for part number four, I apologize. So we don't need the max number of vowels yet, but we will in the next part. So then I just print the output. So system dot out dot print l n, num of vowels, plus num of vowels, okay. Now just real quick notice, this is outside of this outer loop here. Okay, so we got our outer loop, we've got our inner loop. Let me put a little space there. Okay, let me separate that a little bit. And so you can see kind of how this works. Let's run it and see if I got it right. Sting, okay, it's sting, great musician, but not a Java, not a Java variable type. Okay, number of vowels, five. So we got two A's and three E's. So I think this is working. Now we could test it further. We could put an O and a U and see if it goes up to seven. And you should actually test it, but basically this is a pretty simple program. So we can leave it as is. So basically that's kind of the pattern what we're doing is we're iterating through this loop of vowels and we're checking each letter against each letter here. So we have an outer loop. And then again, I really strongly recommend doing this, saying vowel equals something and then character equals. Because down here it makes your code a lot easier to read. It's a lot better than vowels, that substring, blah, blah, you know, if you can keep it track, find your head, that's great, but you might want to do that. Also using clear variable names makes your code a lot easier to read, makes it a lot easier to debug. Now save that and go back to part four, which is a bit more complicated. Building on number three, how would you find which vowel is the most common? So hopefully you can see that that question is related to this one. Okay, so we're gonna be using roughly the same method to keep track of which vowel is most common. So I need to know two things. I know how many times each vowel appears. I need to know which vowel is the most common. So I'm gonna go back to my code. Now I've already created max number of vowels and I'm gonna make a string, maxVowel. I'm just gonna set that equal to an empty. So now I don't need to know the number of vowels. I'm just gonna go ahead and get rid of that or I'll comment it out, just leave it there, because it simplifies the code if you do that. So basically, think about this. I'm iterating through the vowels. I'm starting with A, I'm going to E, I'm going to I, O, and U. So through each inner loop, I'm finding the number of vowels for that particular loop. Okay? So when I start in here, number of vowels equals zero. So when I start with A, I have zero As and then I check, okay? Then I come around, I check E, I have zero Es. So what I need to do is after this loop, so once I've checked every letter, see if the num is greater than the max. So if num of vowels is greater than max, what did I put? Max num of vowels, as I mentioned previously, max num of vowels. So if I've got more vowels for this particular vowel and I say max num of vowels equals num of vowels, exactly what we did in part one. And I also have to keep track of the current vowel. So I say max vowel equals vowel. So max vowel equals vowel. And then down here, print the output, same thing, wait till the end. System, yeah. System.out.println. Max vowel, I could have called it most common vowels, probably should have been a little better. Max vowel and number, let's see, max num of, okay, so let's run that. And hopefully we won't get any, oops, that's a compile. It's good, it compiled. So this is max vowel E number three. Let's go up here and okay, we got three Es, two As. Let's add a couple more As here at the end and see if that works. So that'll give us five As and three Es. So you gotta kinda do some testing on this in different test cases. So A number five works. So you can see that A works even though they're separated. And that's three more Es. Let's see if that works. Okay, so we got six Es. Okay, so I'm pretty satisfied that this is going to work properly. So let's go ahead and just take a look at that real quick. We've got our string. We need to keep track of the maximum number of vowels. We need to keep track of which vowel is the most common. That's what we're trying to find. These two pieces of information. And then we need to know the number of vowels in the current loop. Okay, so we decided the vowels are A, E, I, O, and U. And then we're using these kinda temporary strings to kinda keep track of vowel and character. Again, you didn't have to do that, but again, it makes your code a little bit easier to read. So we iterate through the vowels. Say the current vowel is vowels.substring, blah, blah, blah. And then each time through, as I said earlier, we're starting at zero. So we have zero As, then we count the As. We have zero Es, we count the Es, and so forth. Iterate through the second string, and then we get the character. So the current character of that string. And then we can just compare it. If vowel equals character, we increment the number of vowels. And then we have to check and see if the number of vowels is greater than the maximum number of vowels. If it is, we set that to the new maximum, and we say the max vowel is the current vowel. And then once all the loops are done, we print our output. Et voila.