12:48:55 In this module Lu will be working on methods, and it's all about encapsulation of tasks for reusability and modularity. 12:49:05 So as we start this, you're gonna discover this is going to be one of the shortest videos that you watch. 12:49:13 And there's good reason. 12:49:18 You have been creating methods since week one. So this is a big deja vu review. 12:49:25 We will cover the definition, purpose and benefit the algorithm for creating and using methods and some miscellaneous important method concepts. 12:49:36 But let's start with that. Deja vu review. 12:49:40 So, looking at this slide, remember, we discussed early on in the semester that we'll we want to make a method call how we market is we use a process box with those 2 vertical bars on either side. 12:49:59 This notes that we're making a method call. Notice, this method is going to be called, get in, and it's going to return a value, probably a integer type. And it's going to be stored in a. 12:50:14 Okay. Now, as a result, what does get in? Do? Well, get it will get its own separate flow chart. 12:50:23 Do you see how we say? Start, get in. In this case we're gonna get something from the users. 12:50:28 If you notice, I added, create scanner there, then the next step is declare my storage space while I'm collecting an int. 12:50:37 So I'm gonna have an identifier of integer type. 12:50:41 Here it is. X. Then it is the 2 step prompt. 12:50:46 Forx Store Acts. Those are parallelograms because they are for input and output. 12:50:51 As a reminder, and then the next thing is, if you notice, get in in the main method over there on the left hand side has the letter a see, the assignment operator. 12:51:04 So it's bringing back something it's bringing back an integer. 12:51:07 So we're learning another word. Here, return. So return says this method will return, whatever the value is in acts. 12:51:16 And notice that that's a process. Boxes as well. 12:51:19 And then finally we stop. So every time we write a method now, it will have its own separate flow chart to solve the problem. 12:51:28 You'll use this technique in your super calculator. 12:51:32 Oh, that's a spoiler! That's where this will end today with the super calculator. 12:51:38 So as a review in week 2, remember in the design phase and in your project. 12:51:48 The second thing that you did was look at the big picture and try to break it down into smaller tasks. 12:51:54 Those smaller tasks called modules, are often written as methods and clouds, and so we are going to start writing them as methods. 12:52:03 Okay, also in week 2, we carefully went through the payroll program to know the parts of the method now granted. 12:52:14 We were looking only at the main method. But now we're going to write our own. 12:52:18 It will follow the same recipe, that same algorithm for creating a main method to write other methods. 12:52:28 So, as you recall when we look at that header right? 12:52:32 So we're looking at the header, and I apologize. 12:52:34 I put on the screen. What is a method reminding you? 12:52:37 We covered this in week. 2. It's just a group of statements referenceable by an identifier. 12:52:43 Now we always wrote the main method. Now we'll write methods with our own. 12:52:46 Identifiers, identifying the task that will be handled within that map. 12:52:51 Now, remember, there are different access specifiers for now we're still gonna work with public for our methods. 12:52:59 Okay and static we're still gonna keep working that as well so we don't have to create an object in order to use it. 12:53:08 Now the return type of void may change, because if our method, like the example I just showed you in the flow chart, get in. 12:53:17 The return type was not void. It was integer type. 12:53:20 Because it was going to bring an integer value back. Okay? 12:53:23 And instead of name it was called get int. Now you see that string in ours that's our parameter list. 12:53:31 That's where we will say what kind of data this method needs in order to perform its task. 12:53:37 The caller will send that data when it makes the method call. 12:53:44 Okay, so that's a little bit of what we've already seen. 12:53:49 And now we're just gonna use it in our own way. 12:53:53 So methods define their collection of statements that perform a specific task like Get in its job was only to get an integer from the keyboard and return it. 12:54:05 So the purpose is to encapsulate smaller tasks, used to solve a larger problem. 12:54:10 You'll be doing this in your project work as well. 12:54:13 Some of the things that you've done. You can now discover might be better handle in a method and making a call to that method. 12:54:21 And so the big benefit is code reusability. So we moduleize our code and we can reuse the methods that we need to throughout the process. 12:54:32 So what is the algorithm for Chr and using methods? 12:54:37 Here's the step by step process. First, we're gonna create that method outside of main. 12:54:44 So. In other words, after that closing currently brace to Maine. 12:54:49 But before the closing curly brace to the class, okay? 12:54:53 So after the curly brace of name, but within the class definition. 12:54:59 Now our method header is gonna include what method modifier. 12:55:04 So we got that public static. That's what we're gonna use for now. 12:55:08 Our return type. Now it could be void if the method doesn't bring any data back to the caller or any P. 12:55:15 Dt, what's pdt, primitive data type? 12:55:19 Or we can bring back a string object. We can also bring back a raise. 12:55:25 But I'll talk more about that when we get to a raise in our next topic a method identifier. 12:55:31 Now it's your job. You name it. You decide what the methods going to be called. 12:55:37 It is a good idea to pick method identifiers that describe what task, then, that will be doing. 12:55:45 And finally the parameter list. If your method needs some incoming data from the caller in order to perform its task, you'll create a parameter list. 12:55:56 Now, when you do this, this is basically variable declaration in that parameter list. 12:56:04 So when you're flowcharting now, you are actually going to create 2 storage boxes in your flowchart. 12:56:11 At this the first storage box will be for the parameter list. 12:56:14 In other words, the identifier and the data type. But it is actually gonna be initialized by the caller that sends that data into that parameter list. 12:56:27 Now the method itself might actually need more storage space. 12:56:32 So that will be the second storage box where you declare the variables like you've been doing in Maine, so you'll have 2 storage boxes, the first one being initialized by the caller, and the second one variables, that you're still going to need in order to complete 12:56:51 the process within the method. Okay? So remember that parameter list has a data type and identifier. 12:56:58 It is local to the method. In other words, it's scope is only while that method is running those data types and identifiers exist. 12:57:10 Once the method is complete and we've reached the end of it. 12:57:13 Those data types and identifiers will no longer be available. 12:57:19 Okay, and you may use multiple parameters. And so, in other words, you can have a list of data that you need for the process. 12:57:29 How do you separate each of the parameters using a comma? 12:57:34 And the values that are passed into the method are called arguments. 12:57:38 So. In other words, when you make the method, call and main the data that is being sent is called an argument, and it is passed into the parameter list. 12:57:51 Now the method. Body. Well, this is pretty much the easy part. 12:57:56 It's the code commands to solve the particular test that the body is going to do. 12:58:01 It's encapsulated within curly braces, and if the return type is not void, you're going to use that new keyword return to send the return type value back to the calling method. 12:58:16 Now remember that return type in the method header must match the return value that you are sending. 12:58:25 Okay. So now you've created the method that's spectacular, and you use the same procedure as the flow charting you did with the main method. 12:58:34 No, here's where it gets a little different in the second step you need to use or call that method. 12:58:41 Remember, it's outside of Main Main, is our driver program. 12:58:45 And that is basically what runs our process. So how do we get to use our call? 12:58:53 This method? Well, if it has returned type, we need to catch or assign that return value. 12:58:58 If the return type does not void any arguments we need to send. 12:59:04 If there's a parameter list we need to send. 12:59:07 Those arguments, via the parameters, and then we call the method when needed by its identifier, and put those arguments into the parentheses. 12:59:19 Method calls do end with a semicolon. So where are you going to see it? 12:59:27 Examples of this? Well, in 2 places. Yeah, I already let it out of the it lets you know that there is a super calculator you'll be building in this module. 12:59:38 It is called that on already runs. So before you modify it in any way, please compile and run it. 12:59:47 You'll see the methods in there that I have already written for you. I've given you a few. 12:59:51 There's others you'll have to finish, and then more that you'll need to write. 12:59:56 But you will see how those methods were used or called within the main method. 13:00:01 Also on that note. Nothing do not have to be called by the main method. 13:00:09 Methods can call one another so you can have method, one calling method, 2. 13:00:14 So know that any method can call another method. You will learn in programming to what's called recursive methods. 13:00:23 In other words, methods that actually call themselves. Now, this is a fairly complex idea. 13:00:29 So I'm not going to spend too much time on it, but I want you to know that that can happen. 13:00:35 You can have a method that actually calls itself within its method. 13:00:38 And that's called a recursive method. Okay? So a little bit more. 13:00:45 Some miscellaneous things that you need to know. 13:00:49 The main method is listed first in the file, because it's our driver program. 13:00:55 That main method is automatically called when the program starts, other methods must be called by main or another method. 13:01:03 In order to be executed. So, in other words, if you write a method, but no one ever calls on it, it will never run. 13:01:11 It will never be executed, because programming rule number one still applies the main method being our driver program takes control of the CPU and is being processed in a linear fashion. 13:01:24 That fetch, decode, execute of each of the commands. 13:01:29 Now, if one of those commands happens to be a method, call, then we jump to that method, and that method is now run in a linear process until we reach the end of that method at which time we return to the caller where we left off. 13:01:46 Now reminder variable, only exist within its scope. So, in other words, if a variable is declared within a particular method, it only exists within that method. 13:01:57 I believe I've given you an example in the method overloading where I use the same identifiers in a couple of the methods, realize each time that I use that ideaifier. 13:02:09 It is actually a separate instance of that variable that only exist within that method. 13:02:16 It is not necessarily good programming form to use the same identifier in different methods, because it can cause ambiguous code. 13:02:26 I did it on purpose to show you an example. That method, if it has a variable identifier, the same as another one, it doesn't matter. 13:02:38 It's getting separate storage space in main memory, and it only exists while there's method is running. 13:02:44 Finally, those primitive data type arguments. They are passed by value. 13:02:51 So what does that mean? It means a copy of the value in that variable argument is assigned to the parameter variable for use within the method. 13:03:02 So. In other words, when you pass an argument variable, any of the primitive data types. 13:03:08 That primitive data type in the calling method still remains the same. 13:03:14 Only a copy of its value is passed into the method. So if the method changes that value, it's only changed within the method, the original primitive data type is left onchanged. 13:03:28 Now I will be adding details to this, as we cover. 13:03:31 A raise, because arrays are handled differently. They are not pass by value. 13:03:38 They are passed by reference, but I will explain that when we get to array little little bit more, 3 things to remember the return type. 13:03:49 If it's void, there's no need to catch a return value. 13:03:53 Otherwise make sure that whatever the data type is that you have a an identifier of that data type and the assignment optator to catch that return value number 2. 13:04:07 Those are, arguments are passed by value that's the original data in the identifier is not modified. 13:04:15 Only a copy is sent and stored in the methods. 13:04:20 Local identifier. Our exceptions are, those are raise and strings, and I will talk more about them when we get to a raise. 13:04:27 Remember, strings were an object as well. And there's a mistake on this slide that should be an uppercase S, because strings are objects, and number 3. 13:04:37 A method can call another method. So they don't have to be called by name. 13:04:43 So in conclusion, your lab. Time is to build the perfect super calculator. 13:04:51 Now I've provided you some code that runs with method calls. 13:04:56 So you can observe how they work, compile, and run it. 13:04:59 Look at those examples of the return types of the parameter lists, and how the code interacts after you feel comfortable with what the code does, then it is your job to add features to the super calculator. 13:05:17 Think of things that you would be handy for you in your everyday life, that you would like a way to quickly calculate. 13:05:24 Build those features into your super calculator. You'll need to do at least 5 different menu options than what's been provided. 13:05:36 You may find that some of the methods that I've already written might be useful for you to call on for the methods that you write. 13:05:45 Your imagination is needed for the activity. Build something. You can imagine that you would like to use. 13:05:58 Now one final note. There is a text document below this video. 13:06:03 I'm method overloading the idea of method overloading is that we may have a method with the exact same identifier name. 13:06:14 So, how does the computer know which method to call? Well, it's based on the signature list. 13:06:20 So a method that is being overloded. Each method must have a different signature list. 13:06:28 You will see that in the example that I provided as a result, if there's a different signature list, you can also provide a different return type so you'll see that in my M. 13:06:41 One example of method. Overloading listed after this video, I hope this makes sense. 13:06:50 If you have any questions as always, please contact me via email, or stop by joining my office hours.