 So, okay, great in chapter one, we talked about Java. We got a review of that in chapter two. We just got this very big high level theoretical thing about how do we analyze algorithms. And since this is a course about data structures. Yay. Finally, we're going to get into data structures. And we're going to be talking about the linear ones in this chapter. Essentially the items are in some order, depending on how they're added or removed. And one of the more useful ones is the stack. And sometimes called a push down stack. And what are the two ends of it called, they usually they're called the bottom and top, or the base and the top. By the way, I'm probably going to be doing a lot of drawing on the board. So this is going to be really interesting for the recording to see if this all works out properly. The book says it's like a stack of books. Every time you get a new book you put it on top of the stack. And then when you get to take one off the top. Also, it's like cafeteria trays. You know, this is a character stage trays that are on spring. Does anybody ever seen those. Okay, so you push the cafeteria tray on and then you pop one off the top and the rest of them pop up for you. So if I wanted to get to the physics book in a stack I'd have to remove the Java and calculus book here in order to get to the physics book. So here's a stack that has a number of objects 114766 and 10. The bottom is 11 and the top of the stack has a 10. Now, this is where stacks get useful whenever you have something that you need to have data that you have to remember. And then restore in reverse order a stack is exactly the data structure that you want. We're always going to remove things from the top. So when I remove the top object the 10 will come off first, then the 66, then the 47 and that last will be the 11. So they'll come off in the reverse order that they were put on. Do you need me to draw a picture of this on the board. Stop sharing here. And let me. Okay, it's not very good. So I hope this is going to work. This one. So here's the bottom of the stack. And let's say I put 11 on there first. The next thing that comes on the stack is a 47. Then I put the 66 and then the 10 so those are my four entries. Take things off the stack. I always take the top entry all first. Remember 11 was the first thing that came in. So the 10 comes off first. And it goes away. Then the 66 comes off the stack. Then the 47. And the last item that goes out is the 11. So they're coming off in the reverse order that they were put in. The last thing that goes into the stack is the first thing that goes out of the stack. And sometimes I've seen it as. Which is the same thing. The first thing that goes into the stack is the last to go out. But usually we call it last in first out. So things get reversed. Well, in this, when you have this stack of integers that seems sort of useless. There are places where you a stack becomes very natural. One place where it would be, for example, is in this image editing program. That I have here. I can edit. And look at my undue history. So the first thing I did here was I flipped the image and I'm sorry, this stack is upside down. I loaded the base image. I resized it. I'm going to do the color levels and then flip the image. When I do the undo, I do them in the reverse order. So first undo the flip image. Then I will undo the color levels. And then I will undo the resize. And then I'm back to where I was. So whenever you have something that needs to be undone in the reverse order. In your word processor, when you're doing undues, they're in the reverse order that you did them in the original. And so the people who implement the word processor, the people who implemented this image editor. What they did was they keep all the items on a stack. And then they can pop things off the top of the stack. And it does them in the reverse order. So you can undo in the reverse order that you did things. And these really are useful. So what are the operations that we're going to have on a stack again this is an abstract data type. How this is going to be implemented we're going to actually implement one later on in the chapter. We're going to have to have a constructor that can create some new empty stack. We have a push operator. And what push does is it puts a new item on top of the stack pop removes the top item from the stack. It doesn't need any parameters and it returns the item and the stack gets modified. Now, sometimes you want to see what is on the top of the stack without removing it. And that's what peek does. It looks at it, but it does not take it off. Otherwise, every time I want to see what's on the top of the stack, I'd have to remove it. And if it wasn't what I wanted, I have to put it back again, which would be a waste of time. And that's why this operation is usually implemented. I'm going to be able to see if the stack is empty or not. That's really useful. And it's nice to know how many items there are on the stack. So for example, if I have stack is empty. There's the content. If I push for then push 27. If I do a peak, I'll get the 27 back, but it will not be removed. I can push 1066 and then get the size which is three is empty will be false. Push 47 11 and then pop. I get back the 47 11. If I pop again, I get back to 1066 because that's what's on top of the stack. And then when I'm done, I have only two items left in the stack. So everybody okay with that. So the question is how do we do this in Java. And the answer is because there is no built in stack data type. However, we have the array list. So what we're going to do is we're going to use behind the scenes. We're going to create an array list. That's going to hold the items in our stack. So now here comes our question. When I draw my stack, I'm going to implement it as an array list. And the big question is where should the bottom of the stack go and where should the top of the stack go. We could store it with the top of the stack. As the first item. Or we could also say, no, let's put this top of the stack as the last item. Which would mean that we would have, and it turns out that this is the way we really want to do it. The reason is because we're always going to be adding things at the top of the stack. And we're always going to be removing things from the top. Yes. Well, in an array list, adding things at the end. And removing from the end is order one. But if my top was at the beginning of the array list, removing and adding from the beginning of an array list is an order and operation because I have to move everything down or push everything over to the right. So because almost all of our operations are on the stack are going to be at the end of at the top of the stack. We want the top of the stack to be at the end of the array list. When we do our implementation. I hope I'm not standing right in front of this, but we'll, we'll find out on the recording. And rather than you having you read this, I put it here. So we're going to need an array list. And we're also going to have to bomb out our program. If there is a, if we have an empty stack, if we try to pop an empty stack, there's nothing to pop, right? So we're going to cause an exception called no such element exception. Now we haven't talked about exceptions yet. And that's going to be a little bit later on in the course. Just take my word for it. By doing something called throw. An exception that causes your program to crash. And it will crash with this message. So we're going to have an array list of items. And those, by the way, we're going to use a generics here. Why? Because I might have a stack of integers like I had there on the board, but I could have a stack of strings. I could have a stack of doubles. I could have a stack of some sort of object. If I had, for example, a stack of circles, like the circle object that we came up with a couple of weeks ago, I could have a stack of circle objects. That's why I want this to be generic. And I'm going to store them in an array list. And this is important to document where the top of the stack is. It's at the end of the array list. So to create a new empty stack. Create a new array list of that given type. If the array list is empty, that means the stack is empty. Push will add to the end of the stack. And pop. Now if the stack is empty. Then I'll say. And there's no such element. There's nothing to pop. Otherwise I'll remove. The last item in the array list and return that to you. Peak is the same logic. If you have an empty stack, I can't peek at it because there's nothing there. I could, I could, I guess I could return null, but. That's a design decision. My design decision was to be, to be consistent between peak and pop. And here I'm just getting the item at the end of the list. I'm not doing a remove. I'm not doing a get so that it doesn't get rid of it. And the number of items on the stack is the number of items that are in my array list. And then finally I want a two string method. Namely, if I have an empty stack. If I don't have an empty stack, excuse me. I'll take my array list converted to a string. And then return a string that shows where the bottom and top of the stack are. Otherwise, if it is empty, I'll return that. So there's not a lot to implementing a stack in Java. There is this. Well, there's nothing to it because stacks are pretty. Well, they're very basic, if I can I tell you. So let's look here at stack test dot Java. So I'm going to create a new stack. And is empty should return true. Then I'm going to push the words Java and keyboard notice this is a stack of string because it's a generic type. I can have a stack of anything I want. And then I'm going to look at what's on the top. Then I'll push computer. See the size and print out the current stack. Then I'll push program and the current stack. Look at the top, then I'll pop these two items. And then find out how much is left. So let's compile that. And run it. And there we have it. Is empty is true when I first do that. The top of stack will be keyboard. The size will be three. There's Java keyboard and computer. Then we can try pushed program. And then we pop program and computer. And there's only two things left Java and keyboard. Just for fun. Let's do this. Now we have an empty staff. And now let's try and pop one more time. And there's our exception. No such element stack is empty. That's what that throw did. And since I want you to have a working program, I'll comment this out. That's better. I'm going to skip this one, the basic balanced parentheses. Reason is because it turns out that it's a stack is overkill for the task. What they were going to do is you're going to say, okay, how can I make sure that the opening and closing parentheses match? It turns out you don't need a stack for that. All you have to do is count. Okay, so let's do something where we do have to remember things on a stack. And that's when we have balanced symbols. So for example, I have square brackets for array lists. Curly braces are code blocks and array initializer and parentheses are arithmetic expressions. And as long as each one of them. Maintains its own open and closed relationship. Everything's cool. So all of these are balanced. Every opening. Square bracket has a closing square bracket. Every opening marker has a closing one. And they're paired up. These are not balanced. So either I can have more of one than of the other. For example, I could do something like that. Let's go to liberation mono here. Or I have too many. Or I could have something like. That where I have too many opening and not enough closing. So that's another place that they could be unbalanced. And even if they're balanced, they have to balance correctly. Because that closing symbol here. Does not match this opening symbol there. I guess I'll put this in the notes here. I'll put this in the notes here. Too many closing. Any opening. And we have a mismatch. So what we're going to do is we're going to do as follows. Let me stop sharing for a second here. Both I have parentheses. Square brackets. Brace. Closing brace square brackets. Parenthesis. And then another set of parentheses. And I want to check to see if that's balanced. I'm going to have a stack every time I see an opening marker, I'm going to push it onto the stack. This is an opener. That's an opener. When I get a closer, I look at the top of the stack. If the top of the stack is a matching one, a matching opener, then I remove it. If it's a non matching, then I don't remove it. Then it's an error. Now I get this thing. It's a closer. I look at the top. It's secure. It matches. I have a closer look at the top of the stack. It matches. I remove it. And I have an opening parenthesis. And a closing parenthesis. Look at the top of the stack, remove it. And when I'm done, the stack had better be empty. Let me do it with one that doesn't balance properly. Let's do. But here I've pressed to do this. I put the opening brace. And then here's a closing brace. I've hit the end of the string, but there's still something on the stack, which means I've got a problem. I push this, I've got a closing symbol. It matches that. I remove it. I've got this. Oops, the stack is empty. That means I have too many of these. So those are the situations I have to be able to look at. And also open there. I'm going to push the opener. Here's a closer. Wrong type. It's an error. So everybody okay with the idea of how this works. And the stack makes it. Very straightforward to do this because it remembers everything I've done, no matter how deeply I have my parentheses or braces or square brackets nested. I always remember all of them. So when I get the closures, I can make sure they're in the right order. And the right type. There's the code for it. So I'm going to have a string that has all my opening symbols. And the corresponding closing symbols. And I made a routine to just check to check that an open symbol and a closing symbol matches. And it's going to return a true or false. So I'm going to return. Whether the index of the open symbol. Is the index of the closing symbol. Is the index of the closing symbol. And I'm going to go back to the logic behind this. Notice, by the way, I did not need to do this. It's just a programming style thing. I didn't have to say. If. Openers. Index of open symbol. They is. Closures index. Of close symbol. Return true. Else. Yeah. I mean, there's nothing wrong with the Java here. This is exactly the same as this. This is a conditional. It's already true or false. And that's what I'm returning a true or false. So I don't have to do. This comparison and then return true. The condition already is true. And I see code like this all the time. And I say, oh darn. I decided a little, let's. Let's cut out the middle man and do this directly. Now I'm going to have a stack of character again. It has to be capital C character because remember, your generics have to be objects. So I'm using the wrapper class and that's my symbol stack. And you're going to give me a string of symbols and I'm going to return you a true or false either balances or it doesn't. So I'm going to look at the current character. If it's an opening character. I'll push it onto the stack. If it's a closing symbol. Then if the stack is empty. I'll return false. That means I have too many closing and not enough openers. Correct. That was what I did on the board earlier. Otherwise, if the symbol stack is empty, then the symbol stack. At the top had better match the symbol that we have here. If they don't match. I'll return false. But no matter what happens, I'm popping that symbol. I could make it a little bit more clear here. Let's do this. Let's do. So I'll pop off whatever's on this. Top of the stack. And then the top symbol had better match this symbol that you gave me in terms of the opener, matching the closer. If that's not the case, I'll return false. If it's not an opener or a closing symbol, I'll skip over it. And then. I return whether the symbol stack is empty or not. If it's empty, then it must have balanced. If there's anything left on the stack, that means there was. An opener that didn't have a matching closer. And again, I'm return because this is already a Boolean. I don't need to do an if statement. Again, I could write if. Sim stack dot is empty. Return true. Else. Return false. But again, this is already a true or false. So there's no need to go through all this rigmarole when I can just say, okay, this is the value I want to return. And then we can check the balance of these three things here. Pilot. And we should expect true and two falses. And sure enough, that's what we get. Now what I could do, okay. And I'm not thrilled with this, but it might help you if you're going to go and look at this code yourself later on. So I have a closing symbol without an opening symbol. And here. I have. Excuse me. I've got a. And then here I'm going to unfortunately needed. An if statement, if not. Sim stack dot is empty. I'm going to do a print. I'm going to do that because I'm not returning a Boolean here. I'm actually doing something with it. So if it's not empty, then. Too many opening symbols. So here I have two of them that are mismatched. And let's put another one here. There should be too many openers. Yeah. This is by the way. Notice that I'm taking the finished product here. And I'm what I'm now doing is I'm restoring the kind of thing that I did while I was testing it. I had all these print statements in here. And then when they were done, I removed it when I was done with it. And I knew that it was working. Then I removed them all. Okay. But while you're writing it, you probably want to put in some of this information here. So for example, I'm going to say. I guess it could come in here. Checking balance. Or. Plus symbol string. And then here when I'm done with this. I'm missing a parenthesis here. So the compiler is doing some of the sort of sort of the same thing that I'm doing here. Okay. Okay. This one that I put in here for readability did not help my readability at all. So. So I, again, I don't expect people who are writing a program. Since I don't do it myself. I put in all of these. Lines here with the system out print line. And then I can see what the heck is going on at every stage of this. And sometimes I will also put in here. And that way, once I'm done, I can find all the debug statements by doing a search and replace really quickly. So this is just one of these techniques that. When you were doing your programming in computer science 75, did you often do this putting in a lot of print lines to see what was going on? Well, I recommend it. Okay. If you haven't done it before, you can try it on one or two year programs and you'll see, wow. Oh, so that's what it's actually doing. Okay. Converting decimal numbers to binary numbers. A similar thing here. I'm not going to use base two because base two is boring. Are you all familiar with number basis other than two, other than 10. How many people need to review of number basis other than 10. Okay. Real quick. Then I'll let me go and stop sharing here. Okay. When we have a number like. Three 57. This is the 10 to the zero column. 10 to the first and 10 to the second. Yeah, you're familiar for this. And when you were in grade school, of course, they call this the hundreds column, the tens column and the ones column. But since we're really sophisticated, we're going to use powers of 10. And that's going to be three times 10 to the second. Plus five times 10 to the first. Plus seven times 10. Okay. You're, you're familiar with this. Yes. No surprises here. Now, here's a question. What would happen if we lost our thumbs and we had only our eight fingers left. Now, instead of being able to count to 10 on our hands, we would have zero one, two, three, four, five, six, seven. And then that's eight possible numbers. Oh, we're out of fingers. So now we're going to have to go to one set of eight and no. One, one, one set of eight and one, one. Are you with me on that? Remember how we have had 10 to the zero and 10 to the first because there's 10 possible digits, zero through nine. In base eight. We have eight to the zero, which is the ones column. Eight to the first eight squared. And eight cubed. So when I have a number like three, seven in base eight, that's three times eight plus seven, which is 31 in base 10. Okay. This is, this is going to be geekiest jokes I know. So I got to tell it. Do you know why programmers can't tell the difference between Halloween and Christmas? Because oct three one is the same as octal, which is base eight three one is the same as decimal 25. I guess you had to be there. Okay. I told you it was a pretty easy joke. So now here's our question of what do we want to take something that is in base eight and convert it to base 10. So let's say I have in base eight. Oh, I have two, two, six, four area. Let me, let me check what I've got here. Yeah, we're taking it. I'm sorry, we're taking a decimal and we're converting it to base eight. So let's say something easier, like 25. If we take 25 mod eight. That's going to give us a one, correct. Now what we're going to do is let's do this with Oh, am I sharing my screen? Okay, good. I'm still sorry to make sure I was doing the right thing here. I'm going to do this because I'm not sure if it's going to show up properly. Okay. What I could do if I needed to split this up into the individual digits. I take mod 10, which gives me a seven. And then I divide by 10, which gives me this part. Then I take mod 10 of what's left, which gives me a five. And then divide by 10 again, take mod 10, divide by 10 and eventually get a zero. And there would be my three numbers, three, five and seven. I split them apart that way. So now let's take, let's say five. 57, 56 is not going to be much of it. What's a good number that I should choose. Let's take 47. And what I'm going to do is I'm going to do it mod eight. And that comes out to be a seven. Okay, because 47 divided by eight is five with a remainder of seven. Now I take 47 and divide it by eight, which gives me a five. Correct. Now I take five mod eight, which gives me. Five. And then I divide by eight and that gives me a zero, which means I've done it. So 47 in mod in decimal comes out to five seven in mod. In base. In base eight. But notice I did the seven first. Okay. The last digit came first. And the. This digit came last. So they're in the reverse order that I want them. Yes. And that's what I can use a stack for. I can take these digits, put them on a stack, and then reverse the stack and take them off in the order that I really want them. Like 357. I had seven, then the five, then the three. Pull them off in reverse order. And that gives me. Three comes first. Then five, then seven. And a stack is great for doing that because we have something that we need to remember things and then pull them off in the reverse order that they entered. And here this is. So we can now take a decimal number and give convert to any base we want from two to 16. And here are our digits. These are through nine. And because we're going to have 16 possible digits. Because we don't have 16 fingers. We need symbols for them. So we'll use ABCD ENF to stand for 10, 11, 12, 13, 14 and 15. How many of you have used base 16 before, by the way? Yeah. You'll find it. How many of you have done HTML before to write web pages? Have you ever done colors? Okay. And when you do things in HTML, you can set colors. Here. For example, number sign. F F 0080. That's in base 16. And every two digits tells you how much red, green and blue there are. So for red, I have 255. No green and eight zero is 128. So that tells me the proportion of red, green and blue. So if you've ever seen something like this, when you're looking at colors in a webpage, that's what they're doing. They're using base 16. They're using base 16 to tell you what the values are. Instead of having to use decimal. Meanwhile, back at the ranch, we're going to create a digit stack, which is a stack of integers. And then as long as our decimal number is greater than zero, we'll push the mod. And then divide. In fact, let me get put some debug output in here. And then as long as the digit stack is not empty, I'll get one digit at a time. And then I'll take my result string and add on whatever digit number it was by adding on that substring. So for example, if the digit was six, I'd go to zero one two, three, four, five, six. I'd take this substring here, and then I'll add that on to my string. So here I'm converting base 30 to base two, then 30 to base eight, which is three six. By the way, we do not ever read this as 36. You read it as the individual digits, three six. And when I'm converting it to six, 30 to base 16, I read it as one E. So even if I had a base 16 number that came out to, let's say three seven, I would not read it as 37. It's 37 to make sure that we don't understand that it's not a decimal number. And you'll notice here's my bottom of the stack is zero and then four ones. And I'm pulling them off in the reverse order. Here this I stack the six first, then the three. And I pull them off in reverse order. 14 and one, which comes out to one E. So stack makes this very convenient to convert numbers from decimal to some other base. Yeah, I may spill over a little bit into the lab time here. Sorry about that. My timing is not quite as good as I thought it was. Okay. So here when we have something like a plus B times C. Okay, we know that we don't do it from left to right order. We do the, because multiplication is more important than addition. The multiplication happens first. And now the question is, is there some way that we could represent an expression without needing to know the priority that would have it baked in so that everything would work in the correct way. So we wouldn't have to worry about these precedence level, which operators have more priority than the others. And one way that we can do it is. If I fully parenthesize everything. That shows me that the D in multiplication happens first because it's the innermost, then comes this addition. And then comes that addition. So now once everything is fully parenthesized, that takes care of the priority because the priority is shown by how deeply the parentheses are nested. Now, the problem then that we have is okay, we got a lot of these damn parentheses. And people don't like having too many parentheses. So what we could do is we could move the operator before the operands. So instead of a plus B, we would say, plus a B. Okay, or we could say a B plus and these look really weird as the book says. Okay. So let me write some terms here that we're going to. So in fix. We have operand operator and operand. Such as a three post fix is we have the operand, the operand and then the operator. We have three four plus. And then for prefix. We have the operator first. And then the two operands to be plus three four. And you say, well, why would everybody's familiar with this one because that's the one we've been used to turns out that if you do things in post fix. Then you can discard all this business with the parentheses and the priority, because the order in which things appear makes it all work out fine. And here's how you do what they say to do is when you have things and you take everything in parentheses, then move the operator out past the operands. And this plus works on a and this whole thing so the plus goes all the way out to there. Then you can get rid of the operators about the parentheses. In fact, let me stop sharing here for a second. Let's try this here. That means I'm going to have, I'm going to move this out to here, which gives me a plus BC times. Then the plus is going to move out to there. So we'll have a BC times plus. And that's our post fix way of doing it. So I'm going to have a B and C, I'm going to do the multiplication first, and then I'll do the addition. And in fact, this is the way we're going to evaluate it. So let's say I have 237 times plus. I have the two three and seven, the times will multiply these two together, which gives me 21. Then I take my plus from the two and 21. And that gives me 23. Now it turns out that this is what's called also reverse Polish notation, mostly because the guy who came up with prefix notation was a Polish guy named Jan Lukaszewicz. This is reverse Polish notation because the operand is coming last. And you say, well, who the heck is ever going to use it. The answer is HP calculators used to use reverse Polish notation. So when you did things on our HP calculator, you had to learn how to do how to turn your expressions inside out sort of speak to make it all work. And it worked really well, by the way. And here's a more complicated expression where they fully parenthesize it, and then move either the operator before the operands, or after the operands. Now the question is, what happens if we want to have a general in fix expression, like for example, I don't know, where it's not fully parenthesized. Or even worse, if I just say three plus seven times two. So the question is how do I convert that into post fix. I'm going to be using post fix because that's what the book is using most of the time. And here is the algorithm. I'm not going to derive a proof for believe me. I'm going to get each item, which is called a token. I mean I have an operator stack for keeping my operators and empty list for the output. So I'm going to scan the token list from left to right. If I have an operand I append it to the end of the output list. If it's a left parenthesis I'll push it on the stack. If it's a right parenthesis I'll pop everything until I get the left parenthesis. If it's an operator like an arithmetic operator I'll push it on the operand stack, but first I have to make sure that I take get rid of everything that is at a higher press or equal priority. So let's go and see if I can do this here. Let's move this over to here. So the tokens and operand all attended to my next token is a plus. So now I'm going to have a stack here. Now it has a plus on it. Okay, it's not a left parenthesis is not a right parenthesis. But it is an operation I'm pushing on the operator stack great. Now my next stack is an operand I push it on to my list of what's going to go into the output. Now I have a star. And I'm going to push it on the stack but first I got to check is star higher priority than. Well, since the plus is lower priority I'm not going to get rid of the plus I'm not going to do it yet. Instead I'm going to add that there. Now I have an operand. It goes in there. Now I have the slash. Okay, slash and star are equal priority. That means I have to get rid of the star and put it into my output. And now it's okay to push the slash. My last operand is a five. I'm done with my tokens. And now the question is, is there anything left in the stack. Yes, I have to do the division first, and then the plus I pop them in the reverse order. And when I want to evaluate that. I'm going to multiply the seven and the two first. Then I'm going to divide by five. Oops. Then I'm going to divide by five. And the last thing that's going to happen is adding the three which is exactly what I want to do. And where was it here. Convert to post fix there we go. So this is I rewrote the one in the book so that I could. And this is going to be using symbols instead of numbers. So if I say a plus beat. Let's say, in fact, let's do this a plus B times C. It comes out to A and B get added first and then I multiply by C. But if I say a plus B times C, then I do the multiplication first followed by the addition. Let's see if I do some of the division is the first thing that happens, followed by the subtraction, followed by this multiplication. And the addition happens last. Pretty nice isn't it. And again, the stack makes this all possible because it remembers the operation and pops them off in the reverse order. Once you take priority into account when you create your stack. Okay, great. Now the question is what would happen if I had numbers there. Like I had up there on the board, which I of course erased. If I had this post fix expression now how am I going to evaluate that is the question. So let's say I have a post fix expression. And I'll switch over to the video in a moment here at three seven two times five. Plus. Excellent. And the answer is a stack is going to come to our rescue again. Every time I get an operand I'm going to put it on a stack of operands. The three gets pushed, then the seven gets pushed, then the two gets pushed. Now I have a times that will multiply the first two objects on the stack I take the two and the seven multiply them, and I get a 14. Now I take the five. And then I do a division 14 divided by five since we're using integers is going to come out to two. So I pop the five in the 14, I get a two. Now I have a plus. And there's only two things left the two and the three. And I add them. And what's left on the stack is my result. So once things are in post fix I can use a stack really easily, because I don't have to worry about. Okay well as in addition more important than multiplication multiplication more important addition. That's all been taken care of by the order in which things occur. And since we're running short on time here. I'm going to upload this later. Okay. So here is the post fix evaluator. So if you give me, and by the way, what they were using they were using split with blank in the book they were getting the tokens so that in your tokens you'd have to put a blank before and after every single item. I've written it this way using something called tokenize so that it will put all the digits together and the operators. It will split a string into a array of tokens. So I could have numbers that are more than one digit. So you may want to look at this code. This code isn't is not the important part. I'm going to tell you that this is just to do something better than split. This is the part that's important. Okay. I have a post fix expression. I'm going to tokenize it. Whether I use split or some other weird method that I wrote up there that's that's important as I've got a bunch of tokens. And I'll print out what my tokens are just so I can see them. And then for each token, I'll get rid of any bleeding and trailing blanks and print it out. If it's the empty stir if it's not the empty string. Then I don't know why I put that in there that was weird. I'm going to check to see if it begins with a digit if it begins with a digit, it's a number. So that means I'm going to put it onto my operand stack. Because then I'm going to have an operand stack of integers. Otherwise, it must be an operator. So I'll take my first two operands and I'll pop them off. And then I'll do the math with whatever that operator was. And those two operands and whatever that came back to I'll push that on as the result. And when I'm done with all of that, I'll just get rid of tell you whatever's on top of the stack because that's our answer. And here's what do math does. You give it an operator and two operands. And depending on whether it's a multiplication division addition or subtraction it returns the right thing for you. So if I went to seven, 18 plus three, two plus divide, and that comes out to five when you're done. I should make this look a little bit nicer. Luckily, I can look at things without removing them. I'm glad that that operator is in there. The result's five. And that's the case because if I take seven and 18 and add them, that gives me 25. Then I take three and two and add them, which gives me a five. 25 divided by five is five. So I will let you look at that at your leisure. And what I'm going to do now is I'm going to talk about the assignment for staff. This is a little bit complicated. I did do a lab today. I put one together that uses the last two of these conversion to postfix and evaluating postfix. It's harder than the assignment. So let's just let's just look at the assignment then. I mean, if you want to do the lab, great. And if you want the solution to the lab, let me know and I will send it to you at some point. So you're going to have a test stack.java. It will have a method that will merge two stacks into a single stack. And we're going to presume that they're in increasing order from the top, from the bottom on up. And you'll give it two stacks and it'll return a new stack that has them merged together. And you'll also want one that reverses a stack. So given a stack of integers, it'll return the same stack but everything in the reverse order. And you'll want a make stack method, giving it an array of integers and it'll return a stack with the same objects. The first item will be at the bottom of the stack and the last item will be at the top. Then you're going to test to see that it works on two stacks of equal length with no duplicate values. Unequal length and no duplicate values. Unequal length of the duplicate values that are shared between the stacks. There's a seven in both of these. And one stack that has values and one empty stack. The important part is you can have stacks of unequal length and that's what makes this tricky. When you have stacks that aren't the same length. You can modify the input stacks, you can even destroy them if you want to on the merge and reverse. If you want to write us a doesn't destroy them, you can use the clone instance so use this version of stack dot Java. It has a special method called clone that will give you a copy of the stacks so you can be able to basically destroy the copy without hurting the original. Yeah, let me show you really quickly what this merge thing looks like. Let's put in two stacks here I'll make them the same. 10142648. And my other stack is going to be let's say 51237. So how would you do this by hand. Well, first thing you do is you look at the top of both stacks, which one's the winner 54 is the winner. So I'm going to put that in the new stack. Here's my new stack here, which has 54. Now I look at the top of the two stacks which one is the winner. Well he's whichever one I want. I'll use this one as the winner 48 goes in there. I'll pop it off. And I'm comparing the tops of the stacks right 48 and 26. Who's the winner 48 is bigger. Okay cool so that means 48 goes here. Compare the top of the stacks 37 wins 37 goes in there, compare 26 and 1226 is the winner 14 and 1214 is the winner. Now versus 1012 is the winner. 10 against 510 is the winner. Now I have one thing left in this staff. It goes in there. Now, when I merge them I still want the smallest number to be at the bottom correct. So reverse the stack. They'll take this stack and do a reverse on it which can be five 1012 1426 374848. And 54. And that's what I'm going to return from merge that you'll want to draw a diagram like this. You want to actually do if you don't want to if you don't have a whiteboard use index cards that have numbers written on them. You want to see a vertical stack of that don't don't stack them on top of each other but stack them up so you can play with it and see what happens. What happens if the, I run out. What happens for example if I have three things in one stack and none in the other, what do I do then. Well, when you're playing with it in terms of physical cards or doing it on a whiteboard, you just know what to do. And as you do what you know how to do you well you know I have to do x, y and z. Ask yourself, how did I know to do x, y and z, and how do I translate that into Java. So I strongly recommend that as my house tell people, my parents are always telling me, do I have to draw you a diagram answers yes yes you do have to draw me a diagram because when I see it and physically play with the elements of the diagram. I finally understand what's really going on. So I strongly recommend that you do some of these by hand. And as you do it, do the introspection. Wait a minute, how did I know that I should do this. And then write down how you knew and say okay now how do I say that in Java. And that might make your task a little bit easier to do this. Okay, so avoid I ran way over time here I'm very sorry about that. So let me stop the recording.