In this lecture, we'll solve a problem from our iteration work using methods. Our problem is we want to print out a box composed of asterisks using a user-specified width and height. The constraints on our problem are that both the width and height needs to be between three and 20 inclusive. And a new constraint for this lecture is we need to use methods as much as possible. Let's go refactor our nesting and boxes code to do that. We're starting with the code that we wrote for our nesting inboxes lecture but I added at the end the get valid input method that we wrote a couple of lectures ago. So we'll start by using that method to get a valid width and height. We'll start by replacing the get valid with code. So instead of all this code, we can just say int width=GetValidInput and our prompt will be right here and our error message is right here and we know our lower is three and our upper bound is 20. So we'll get rid of this declaration of our width up above and we can get rid of all this code. Let's go ahead and test this and make sure it works properly. And I'll try some invalid inputs and as you can see I got the error message incorrect. So let's just get out of here and by error messages of course here instead. So let's try that again. And this is one of the reasons we test as we change our code to make sure it continues to work properly. Now that we know this works. I'll say get valid width and height and we'll do the same thing for height, I'll get rid of this variable up here, I'll change this variable to height, I'll fix this to height. And I'll fix this as well. And boy, as I look at this, this error message should have been width not height. So it's important, especially as you're copying and pasting stuff to make sure that you're both copying the right stuff and when you change it after you've pasted it, you need to make sure you change it properly as well. So finally, width should work fine, I'm asking for width, width is in the our message and I'm putting it into with the variable and I'm asking for a height and giving the height error message and putting it in the height variable and I can now get rid of this code. So let's test this again and I'll make sure I'm getting the correct width error message. And the error checking is of course working properly and I'll ask for a three-by three-box. You'll notice that I didn't test again to make sure that height gave me the error message when I gave an invalid height. Once we know the method works as long as we're passing in the appropriate arguments, we don't have to keep testing the functionality of the method. And that's one of the benefits of using methods is once we've thoroughly tested those methods, we don't have to test them every time we call them, we just have to make sure that we're passing in the appropriate argument when we call the method. So this is great, we are reusing that get valid input method, we wrote a couple of lectures ago. Let's add another method that will do some of the stuff that we do a lot in this code. And if we look at this code, we can see that when we print the top row were repetitively printing an asterisk. And when we print the interior of the box where repetitively printing space and when we print the bottom row were repetitively printing an asterisk. So it seems like one of the ways that we could get a benefit from a method would be to write a method that prints a given character a certain number of times. So let's go ahead and do that. Down here after our get valid input method, I'll add another method. It has to be static because I'm calling it from the static main method, this won't return anything because it's just going to print a character. So I'll say PrintCharacter and we'll need two parameters. We need the character we need to print and we haven't talked in detail about the char data type which holds a single character. We'll get there just as well, get two strings very soon now. So this is just going to be a single character and I'll call it character and we need to provide how many times we want to print the character and I'll call this parameter numTimes. I'll add the body of the method and I'll add a documentation comment print, the given character numTimes times, the character to print. And how many times to print it. And we actually already have code up in the main method that does this so we can just grab this code and put it here and make two changes. First of all, we don't want to say i less than or equal to width we want to say i less than or equal to numTimes and we certainly don't want to print an asterisk,we want to print the character. Now for the For loop, if you've convinced yourself that you should always start i at zero, you can certainly write it this way instead, but you'll have to change this less than or equal to to a less than instead. And this will iterate the same exact number of times as this will. So that's personal preference on your part. Let's go back up to the main method and here to print the top row, let's just say print character and we know we're going to print an asterisk. And as we can see from the For loop, we already have we're going to print it width times. So let's get rid of this For loop and we'll test our code. I won't do anymore ever checking here for the user input. So let's go with three-by-five box and we can see that the top row printed properly, that's the change we made. Since we know this works fine, we'll take this code and we'll bring it down here to print the bottom row as well. And we'll test it. I went five by three instead of three-by-five that time. And you'll notice that I keep making a small change to my code and testing it and making a small change to my code and testing it. And that's a really good way to approach both re factoring, which is what we're doing here. We're changing the structure of the code to make it higher quality code but it's also a great idea in programming in general because it's probable not guaranteed. But probable that if you run your code after making a change and you have a bug and you didn't have a bug when you ran the code before you made the change, then it's likely not guaranteed but likely that the bug is in the code that you just change. Okay, we have another place here where we print a character repeatedly. It's in the interior of the box. So we'll call that method again, this time, we'll print a space and if I hide that box, you can see that we're actually printing this with minus two times. So I'll do that and I can get rid of this For loop. I'll test my code again. I'll do a five-by-three box again and everything is still working fine. Now, we could stop there but I want to write one more method. I want to write a method that prints the interior of the box and I'll explain why I want to do that after we've done it. So I'm going to come down here and we'll add one more method. Again, it's void, it doesn't return anything to the color of the method. I'll call this print box interior and to do that, I'm going to need two pieces of information. I'm going to need the width of the box and I'm going to need the height of the box. So I'll pass in width and I'll pass in height and I'll add my body and I'll add my documentation comment, print the interior of a box with the given width and height. So we have box width and we have box height and I already have the code that does that. It's right here, so I'll just grab that code and bring it down here. And because my parameters are called width and height and the code I just stole from the main method used with and height as well I can compile just fine. And I'm very excited I can go ahead and run my code and I say give me a five-by-three box and I only get the top and the bottom of the box. So just as a reminder, writing a method isn't sufficient, we actually need to call it to make the method run. So I'll say print box interior here and I'll pass in width and height. And I'll run it one more time with another five-by-three box and everything worked great. I told you I'd explain why I wanted to do it this way when I decided to write this last method. And the reason I wanted to do that is because now our main method is really a description of the high level processing that our whole program does. We get a valid width, we get a valid height, we print the top row of our box, we print the middle of our box and we print the bottom row of our box and that's nice. We can just look at this small main method and get an understanding of how the program works at a high level. And then we leave our individual methods to sort of do the heavy lifting of implementing the functionality of our program. To recap in this lecture, were factored are nesting in boxes, code from our iteration work to come up with a more robust solution that uses methods.