 Hi, time for today's mini lecture. Today, let's go back to that program that we did a few days ago, where I have the number of hours and minutes for two tasks, and I need to find the total time. And here was the code that I wrote. I had you enter the hours and minutes for task number one and for task number two. And then I figured out the total hours, total minutes. And did a whole bunch of arithmetic to make it work, and then printed that out. OK, that's fine. Let's call this total time two, I guess. I'm going to modify this. Now, what would happen if I had given the number of hours and minutes for two sets of two tasks, find the total task and find the total time, excuse me, for each set. Excuse me a moment here. Sorry, I had a brief interruption there. And then we do the same for a second set of two tasks. OK, well, one way we can certainly do this is to grab all of this stuff here, copy it, and paste it here, and now what we're going to have to do is we're going to have for first task in set two and second task in set two. And then we're going to, we want to have these all as different variables. We could reuse the variables, actually. Shall we reuse the variables? No, we don't want to reuse the variables because we never know when we might need them again. And this will be the first task in set one and second task in set one. So this is going to have to be hours three and minutes three and hours four and minutes four. So now we're going to have hours three plus hours four, minutes three plus minutes for the way this is. You can tell how exciting this is. And we're going to have to have this as total hours set two and total minutes set two. And finally, total hours set two, total minutes set two. He casts this. This is more work than I thought it was going to be. But this is good, actually. This is total hours for set one, total minutes for set one. Daring me. You know what I'm going to use? I'm going to use copy and paste so they don't make myself. Notice that, by the way, when I'm doing all this copy and pasting, I'm just sort of blindly hoping that I get everything right. Because if I screw up one of my copies and pastes, everything is going to fall down, go boom. OK, let's compile this and see how many errors I have. I know errors. That's quite a surprise. OK, let's run the program. So first set in task one is let's say four hours and two minutes and three hours and four minutes. That's seven hours and six minutes. And then the next one is going to be let's say five hours and 15 minutes and three hours and 49 minutes, which should be nine hours and some odd minutes. OK, cool. So that's working. Oh my goodness, what a lot of work that was. That's just horrible. Gee, wouldn't it be nice if there were an easier way to do this? And the answer is why? Yes, we can use functions, methods, excuse me, to do it. Sorry, I was programming in a different language earlier today and I was talking about functions. They call them functions, we call them methods. OK, we can use methods to solve this problem in an easier way. Let's go and save this as total time with methods dot Java. What I'd like to do is I'd like to get my hours and minutes here and then the second hours and minutes and then all of this stuff here, where I'm going to print all this stuff out. I'm going to cut that out and I'm going to put it here in a method. It's going to be public static. I'm going to fill this in a little bit later. And let's call this calculate total time. And it's going to have an integer hours one, minutes one. Now we're getting a line that's a little bit long. So I'm going to go to the next line, int hours two, int minutes two. Now by the way, I don't need this set one business anymore. And in fact, OK, may as well show you something that's going to be useful for you. Genie is really nice about some of this stuff. I can select all of these lines here. And then I can go to the Edit menu and I can say, I'm sorry, the Search menu and say Replace. I'm going to replace set one with nothing at all. Let me turn this up here. And then I can say, by the way, when you first open it, you might see the Replace all here. You'll click the little arrow. And then you can say, I want all of set one in this selection to be replaced with nothing. There, that saved me a lot of typing. So learn to love replace. Now the question is, what is this return? And the answer is, this returns nothing at all. All that we're doing here is we're printing stuff out on the screen. Let's look at one of our programs from yesterday here. Let's go to purchase.java. Remember, calculate price, return some result that we could use later on. And it was a double. The problem is here, look what we're doing. We're printing total hours and total minutes. And that's two numbers. And you can't return two numbers. But we can print two numbers here. And we're returning nothing at all. When you don't return anything at all, that's called a void method. So now I'm going to read hours one and minutes one, hours two and minutes two. And now I'm going to call my method, calculate total time. And I'm going to give it hours one, hours two, hours, or minutes one, excuse me. Better give them in the right order, hours two and minutes two. Now, let me print an extra blank line. And now I'm going to have the first task in set two. And there's going to be hours three, minutes three, hours four, minutes four. Instead of having to repeat all of this code, I can now call calculate total time. And this time I'm going to give it hours three, minutes three, and hours four and minutes four. That way, hours three, minutes three, hours four, and minutes four will fill in these four parameters here. And it'll do the calculations with the numbers that I want. And oh, OK, minutes per hour, unfortunately, belongs in this method. Let me undo this and show you what happened. It said it got to line 33 and said, excuse me, I can't find the symbol minutes per hour. And that's because minutes per hour is main, but calculate total time needs it. In fact, that's where it belongs, because main doesn't care how many minutes there are per hour. Only calculate total time cares how many minutes there are per hour. And I also have to change the name of this to total time with methods. And now let's compile it. OK, that's much better. And now when I execute it, I have three hours and 14 minutes plus two hours and 29 minutes. It's five hours and 43 minutes. And then for the first set, task and set two, let's say it's five hours and two minutes and 12 hours and 18 minutes. And there's my total time. So I've done two things here. The first thing I've done is I've discussed what a void method is. It's one that doesn't return any value that I can use later on. And the second thing I've done is I have made a method and I'm calling it twice with different arguments in order to avoid a duplicating code. Now, the next thing I want to talk about is I've told you I've written a cable. Actually, let's write some philosophies. So here's the question I want to ask. Which is better, value returning methods or void methods? And the answer is it depends on what you're trying to do. In general, I prefer to write value returning methods. And by that, I mean methods with a return statement in them. So the question is, well, since I can only return one value, would there be any way to make this program work where I could use a value returning function? And pardon me again for using the word function. When I say function, translate in your head to method if I don't do it myself. And by the way, if we want to get rid of duplicated code, gosh, take a look at this. This enter number of hours and minutes. Gee, wouldn't it be nice if we could have a method that would do that and return two numbers? But again, we can't return two numbers. But we can pull a really sneaky trick. Let's first of all save this under a new name, time with methods 2. And this is going to have to be called time with methods 2. All of this is going to remain exactly the same as it is. We're still going to do all the same thing here, except here's what we're going to do. We're going to ask the user for the number of hours and minutes and the second number of hours and minutes in a method. This method can only return one value, but we can be sneaky. We can multiply the hours times 60 and add the minutes to get the total minutes for this task. And that's just one number. Then when we ask the user for a second task number of hours and minutes, we can again do that in a method. Now we're going to have a total number of minutes. So this time we're not going to be able to do hours to second number of hours, because we're just getting one number back. Boy, I need to explain this further, OK? Here's what's going to happen. We're going to say enter number of hours and minutes for task 1. And let's say they say three hours and 20 minutes. What we're going to do is we're going to return 3 times 60 plus 20, which is 200. So that's going to be our time for task 1. Now we're going to say enter number of hours and minutes for task 2. And that's going to be, let's say, four hours and 49 minutes. And that means we're going to return 4 times 60 plus 49. And that is going to give us 240 plus 49 is 209 minutes. And we'll put that in time for task 2. Now we can find the total easily. The total for both times, for both tasks, is 200 plus 289, which is 489 minutes. Now we have to convert that to hours and minutes for printing. And what we're going to do is we're going to take, well, 489 divided by 60, that's eight hours. And 489 mod 60 gives us nine minutes. So that's the plan. We're going to do everything with total number of minutes so that we're always using only single numbers. And we can return the single numbers and everybody's going to be happy. This is why I had to write this down. And again, I'm trying to convince you of this. And I'm also trying to convince myself that this is going to be a good plan. So what we're now going to do is we're going to say, public static int get task times. We're going to give it a scanner to use. And we're going to give it a prompt to put on the screen. We're going to print out the prompt. And then what we're going to say is we're going to say int the number of hours is going to be input dot next int. And minutes is going to be input dot next int. Essentially, we are doing this code here. But instead of returning both of them, which we can't do, we're going to return hours times 60 plus minutes. And in fact, let's do this here. Let's say here integer task one time is going to be the result of calling get task times. And we're going to give it our scanner, which is called input. And the prompt is going to be the thing we want to print out, which is this. And let's just print this out to show what we're going to get out of that. That's going to be system dot out dot print. So the total minutes for task one is task one time. We don't have this anymore. Let me get rid of all this stuff for task three and task four here for the moment, OK? So here we have total minutes for task one is task one time. And let's compile that. And let's run it. And so if I have four hours and 17 minutes, that's 257 minutes. So now we're going to do another int task two time. And we're going to get the task times. We're going to have input and the prompt, which is the second task. Now, in fact, what I can do here is I can say int first set total is going to be task one time plus task two time. And then I can say system dot out dot print f total time for task one for tasks. Task one and two is hours percent d minutes. And I'll fill in the blank with the first set total divided by 60 and the first set total mod 60. We have three hours and 12 minutes and four hours and 50 minutes. That's going to be eight hours and two minutes. Now, it looks like I'm back where I started again because I need my second set. And I'm going to have to repeat all of this code again. Well, I'm going to have to now make another method that will take care of all this stuff here that I have highlighted. So we'll call this public static void because it's going to just print a result. It's not going to return anything that we're going to use. And we're going to say, let's process a task set. And we're going to give it an integer that has the task set number. Let's grab all this here, cut it from there, and paste it in here because we're going to call this one twice. Now, the problem is this is going to have to say set one and set two. That's why we have task set number. What we're going to do is we're going to create a string that has the number inside of it. Let's actually make this task set number. So here's our prompt. It'll say enter number of hours and minutes for first task in set, whatever number it is that we're using, separated by spaces. And here's our task two time. We're going to do the same sort of thing. Instead of set one, we're going to put in whatever our task set number is, and then follow that with the words separated by spaces. Then let's call it task set total and the total time for tasks. Now, instead of one and two, this is going to be interesting. Tell you what, for set n, we'll change our output a little bit. So when task set number is one, we're going to get the task times for that task, for the first one and the second task. We're going to add them together, and then we're going to say, for set whatever number you gave us, and then we'll print out the total hours and minutes. I'm not sure which version I'm on here, so I'm going to save this under and I'm going to say keep saving it with the visits. Total time with methods two, that's correct. And I can get rid of this comment now, because I think we're all aware of how scanner works at this point. Now what I'm going to do is I'm going to say process task set and process task set two. Follow that. And OK, since I don't have firsts. OK, we've got a whole bunch of areas. Let's figure out what's going on here. Cannot find. Oh, yes, I have to pass my scanner onto this one also. It looks like the problem is it doesn't know what input is. OK, not a problem. We're going to just say process task set. We'll give it the input and one input into there we go. That will take care of some of our problems. And then this is going to be the task set total div 60 and the task set total mod 60. And let's run it. So there's our task set one, which is fine. So we're going to say four hours and 24 minutes and let's say three hours and 48 minutes. And so for set one, the total time is eight hours, 12 minutes. For our first task and set two, we're going to say let's say five hours and one minute and one hour and 49 minutes. And there's our program. And I want to put in one more readability blank line in there. You may want to look at this video a couple of times to see what was going on here. I did a lot of stuff and I did it really fast. But the idea was in general, I figured out what things I could do that are repeated and I put them into methods like the Calculate Total Time, for example, here. Oh, I don't need Calculate Total Time anymore. It's going to go away. Wow, and now my code is much shorter. So now I'm processing a set of tasks. And inside of that, I have to get the task times. So here's one method that returns a value, which gives me the combined hours and minutes as one number. And here's a void method that doesn't return anything. We only love it because it prints things out on the screen. And again, I'm going to upload all of this so that you can see it and play with it on your own. We compile it to make sure that works. Let's write it one more time, two hours and 25 minutes and three hours and 48 minutes. And then the second set of tasks is, I'll say, one hour and 17 minutes and two hours and 46 minutes. And that's one reason that you really... Well, it was exciting. My phone just hit the floor. I hope it's in good shape. And that's one reason that you want to learn about methods because it lets you take a task which would be repetitive, that you'd be doing a ton of copying and pasting and it lets you avoid that duplication. Also, we've now split this program into smaller parts. One part to get the task times and another one to process a set of tasks. Use methods, avoid repetitive code with minor changes. And by minor change, I mean the only thing that is different between task set one and task set two was the task set number. I made it a parameter and solved the problem that way. And the second lets you break it down a problem into smaller parts where each method solves a part of the program, a part of the problem. One thing I would like you to do, remember we have the extra exercises book. The extra exercises book not only has extra exercises in it, it also has some extra material. So what I'm going to do tomorrow, I'm going to finish off chapter four by discussing call by value. And I might also talk a little bit more about the math methods. This concept of call by value, concept of call by value is ultra important to understand. And so please, please, please read through this. And by the way, if you have any questions about it, like, well, what do you mean by this? Or can you explain it further if you have some specific questions, send them to me in Canvas and I will address them in the next mini lecture. So that's it for today. See you online.