 Hello, it's time to talk about chapter four, which is methods. Now, when we talked about variables, I made a very big deal about how variables in Java are not like variables in algebra. It turns out that methods in Java share a lot in common with functions in mathematics in algebra. So let's take a formula here. For example, this is if you have a distance D and G is the gravitational constant question is how long does it or it leaves me this. I'm not sure I know the name in physics, let's call it the the gravity gravity, but we'll just call gravity. This formula tells you how long it will take in seconds for an object to fall a given distance. Now, if we were to do this as a mathematical function in algebra, we would say something like this, where F of D becomes 2D. In fact, let's do this. I'll tell you what, let's put in the gravity for Earth here so that we don't have to worry about that variable G here for the moment, because it's 9.8 meters per second squared. Now, if we have this formula here, we can then use this formula we can use this function by filling in values for D. Let's go back here and change this again so it says 9.8. So we could say F of 30 and 30 would essentially take the place of D and we'd fill it into this formula here. The variable N is 47.2. And again, we're talking algebra here. We can call F of N and that would call F and whatever is in N would fill in D and then it would do this and that's the result we get. This is all algebra, and it's probably familiar to you, although when you first learned it, it certainly wasn't familiar. But now you, this is the sort of thing you just know without having to think about it. We're going to do this into Java. We now have to actually think about it because things are going to look a lot different, but it's the same idea. And in fact, I'm going to use this exact same formula here and the same name for the function or method. So here I have a class called falling object. And let's just set up the first part of it here. Enter distance fallen in meters. And we're going to have here double D becomes, let's call it distance, what the hell, input dot next double. Now what I'm going to do instead of putting the calculation right in here, I'm going to write a method to calculate it. I'll make it public and static. And again, just because I have to at the moment. Now, unlike main, which doesn't give us back any value, this function here, F of D. It gives us back some number that we can use later on. We have to tell Java that we're going to be returning a value. We're going to be returning a double. And let's call this function F from just for the for the moment. And what we're going to say, it's going to have a parameter called D. I believe that also called parameters in Algebra as well. They're also called formal parameters. Now again, because we are in Java, we have to specify what data type everything is. So D will be a double value. And then what we can say is, let's calculate a result, which is going to be the square root of 2 times D divided by 9.8. And then we're going to return that result to whoever gave it to us. So we got to do a little bit more verbiage in here. The fact that, okay, some of these, for example, I can see I have here. And we just know instinctively from having having cultivated our instincts, we know that F event will stand in for why. And the thing that we do when we put in into this formula, it gives us back a result which we can use later on. And we have to say this explicitly that we're going to return some value for the person who called us to use. Now I can say double time becomes F of distance. Notice by the way, this is okay. Distance is the argument to the method. And D is the parameter of the method. We can think of a parameter as a placeholder. It's a fill in the blank. We're going to fill in the blank with the actual value, which in this case is the argument. And then we can print that out. Time to fall is percent dot, I'll say three F seconds. And we want that to be a print F. Otherwise it will compile. I think I did this right. We'll see how many typing errors I have. Oh my goodness. Oh, that's because I put fall in object instead of falling object. And falling objects is public and I didn't have the right final name. That was just a mistype. Okay, fine. We'll call it falling object dot Java. Are you happy now? Yes, you are cool. And now we can execute this program. And so let's say I have some of the falls 100 meters. It'll take four and a half seconds to fall 50 meters. It'll take 3.194 seconds. Now ordinarily we do not like to. Okay, mathematicians absolutely love to call everything with single letters. In Java we like to have meaningful names. So let's call this time to fall. And this D we could also call it distance. This variable distance here has absolutely nothing to do with this one. This is an important concept, by the way, called a local variable. For example, this variable result belongs to this method. And nobody else can get to it. Let's prove that. And you will see. Oh, yes. Well, first of all, we got to rename this that matches. Okay, today. This is not my best evening ever. Yeah, let's see. Oh, my goodness. This is terrible. Okay. I think I'm a little bit tired tonight. Okay, what did I do wrong here? I changed this to distance, but I forgot to change this. So it couldn't find that symbol. Now it can find the symbol and now we finally have what I wanted in the first place. I can't look at result because result does not belong to main result belongs to time to fall. This is good because it means that I can name my variables anything I want to inside of a method. And it's an absolute guarantee that nobody else's names will conflict with mine. For example, if I wanted to do something silly like integer result becomes 47. That would now be perfectly okay. This result belongs to main. And I can say system dot print F useless result is when it looks for result, it will look for one that's in this same block of code. This variable result is local to main. This variable result is local to time to fall. I don't have to worry about well will there ever be a conflict the answers. No, there's no conflict whatsoever. And here. Okay, back to where we were here. Now again, there's no law that says this variable here that I use as my argument has to be the same as the parameter. In fact, we saw that earlier when I call this D, which is different from the word distance and the compiler liked me. And it ran just fine. The fact that these happen to have the same name is nice, but not a necessary thing to do. The connection is that the first number that you put here, the first community first value you put in the parentheses fills in the first parameter. If you have more than one argument, then the arguments fill in the parameters left to right one after the other. I suppose it's the time for me to write such a thing. So let's go here and let's do template drama. And let's call this we're going to ask for a unit price and quantity. And then calculate the total price in a method. Let's say with a seven and a half percent tax. I don't know why I like seven and a half it keeps popping up all the time. And we'll call this purchase. And so we'll save this as purchase dot Java. Excellent. I can say this is an out dot print. Enter unit price. And then we're going to have integer. No, it's very a double price becomes input dot next double. And then we're going to print enter quantity and int quantity becomes input dot next double. Next int Hello Earth Eisenberg. That definitely would not work well. And now what I'm going to say I'm going to call a method. I'm going to say double purchase price is going to be calculate price. And I'm going to give it the unit price and the quantity. Those will be my two arguments. Now it's time to write that method. This is going to give me back a number, whatever comes out of this method call is going to go into purchase price. Okay, so I'll say double calculate price. Well, if I say double, thank you. Calculate price. And here we're going to have a double unit price. And then we're going to have an integer quantity. These names don't match up, but that's no problem. Whatever's in the price variable goes into unit price, whatever's in the quantity variable goes into qty. And we'll have a final double tax rate is 0.075. The result of our result is going to be unit price times quantity times one plus the tax rate. And we have to return that result. And now we can print that out. In fact, let me do something here. Let's put a dollar sign there to make it clear that we're not we're not looking for euros. It's $10. And we enter five of them. It's $53 and 75 cents. Now you're probably saying, well, what's the big deal. I mean, this is only one line. Why are we going to all the trouble of making a whole method for it. And the answer is because eventually we're going to have methods that do a lot of calculation. For example, let's say I want to compare prices. So it's a unit price for first item. And here I'm going to copy and paste this code. And this is going to be for my second item. We'll call it price to and quantity to, and our purchase price to will be calculated price with price to and quantity to. And then I don't know how that how that new line got there but okay, you can compile that. You can run it. So if you have $10 and I buy five of them. And my second one costs $9 and 87 cents, but I buy six of them. That tells me what the difference in the way I can look at them and I can see if there's a difference in price, which one's the better deal. And you know what I'm going to do here. What's important here is that I'm calling calculate price and I'm giving it different arguments. I'm just filling the parameters. You can think of this as sort of like a contract. I have a contract that says if you give me a unit price and a quantity, I guarantee that I will return to you a result that happens to be the correct amount that you have to pay. To fulfill the contract when you call the method, you say, okay, I want you to calculate a price and to fulfill the contract. I'm going to give you a price. I'm going to give you a quantity and whatever comes back goes into purchase price to see how this works. So see where this step by step I'm going to go and put this in the Java visualizer. Now the Java visualizer is a little bit different than what we use with regular Java. We can't use the scanner class at all. We're going to use something called STD in which is provided with the visualizer and instead of saying next double we say read double and read int. So standard in is something provided by our visualizer and here inside of our standard inbox, we're going to put the input that we want, which is going to be seven items at $5 and eight items at $6 and 35 cents. And here I've just put print LN for the prompts so they'll appear on separate lines. Now what happens when I say visualize execution. So these are the changes that I have to make to get the visualizer to do what I want. And now when I go through this, let me make this a little bit larger here. You'll notice I have here I'm in Maine on line 11. And here's what's going to be coming out of standard input. I'm going to move forward to the next thing here. And now I'm going to read a double into price. When I do that price becomes five. And you'll notice that's taken out of standard input. Then I can enter the quantity. And then I'm going to read an integer from standard input and the quantity is now seven. Now what happens when I go to line 17 here. I'm going to put Maine on hold and I'm going to start to calculate price. Inside of calculate price. I'm going to have the unit price and quantity. These are copied in. I want to go back a step here. When I call this remember price goes into unit price and quantity gets copied into QTY. And that's exactly what happened. The unit price became a copy of price and quantity was copied into QTY. Okay, now we can do this. There's our tax rate. When we go to this step. There's our result which is 37.625 and we're going to return that result. And that's the return value. Then because we're finished with calculate price. We're going to return to where we left off line 17 in Maine. This says we're in line 17 on Maine, which is where we have to come back to. And purchase price becomes the value of our call. Then we print that out and print a new line. Now we want that unit price for the second item and we're going to read a double from our standard input. That means the 635 is going to go into price two. And the eight is going to go into quantity two. Now we have another call to the function. This time though price two and quantity two are the ones that are going to get copied into unit price and QTY. The six dollars and 35 cents gets copied there. The eight gets copied there. We reestablish our tax tax rate. We calculate the result. And then we return it. That's the return value. And then we pick up where we left off, which was line 28. And that becomes purchase price to, and then we print it out. And there is our program. So that's why I like this job of visualizer a lot because it can help you see step by step what's happening. And you know what I'm going to do here. Let's go. And let me put it into a notes file here for everybody to see it. Let's save that. So what I've done here is I've talked a little bit about what's in section 4.1. The Java visualizer helps us see what's going on with 4.2. I talked about parameters and arguments. And multiple parameters. And when we were using the visualizer, we were able to see the stack diagrams. That showed which variables were allocated in which methods. I talked briefly about the math dot square root method. I did not talk about composition. I did talk about return values. So what I'm going to do tomorrow, and I should probably write a note down for myself. So I'll remember when it is. I'm going to talk about something called a void method. Methods that don't return any value. And I guess I'll see. Excuse me. I guess I'll see you all tomorrow then.