 Hi, time for the mini lecture tonight and this time we're going to have something of a change of pace Instead of talking about how to do particular things in Java, which is what we've been doing up till now I want to talk about problem-solving because that's what programming is really all about And here's the program that we're going to work on So if I'm given the number and I've changed this earlier So let me change this to hours and minutes if I'm given the number of hours and minutes for two tasks I have to find the total time it will take me to do both of them So if the user tells me that the first task takes three hours and 15 minutes and the second one takes two hours and 34 minutes Then the total time will be five hours and 49 minutes Rather than just sit down at the keyboard and start hammering away at it Let's ask ourselves. How would we solve this problem by hand? Once we know that then we can help then we can explain to the computer how to solve the problem So let's take an example if the first task takes two hours and 17 minutes and the second one takes four hours and 31 Two plus four is going to be six hours total 17 plus 31 is going to be 48. It'll take us six hours and 48 minutes No problem, but we know from our experience In the real world that we could have another set of tasks Let's say the first one takes three hours and 47 minutes and the second one takes two hours and 35 minutes now we're going to have five hours and This is going to add up to 82 minutes Now I'd like you to pause the recording and say ask yourself. Well, how would I solve that? How would I get the right answer? Because I now have 82 minutes instead of something that's less than 60, which is a big hint So pause the playback and then start it when you think you have the answer Yeah, here's one way that you could approach the problem. You can say to yourself. Well now I know that 82 is over my limit of 60 So what I'm going to do is I'm going to subtract those 60 minutes and that will give me 22 minutes But those 60 minutes have to go someplace and that's 60 minutes is one hour and that means I'll add it onto my hours and that will give me the answer of Six hours and 22 minutes, which is it turns out the correct answer Now let's see if we can encapsulate all of this into some explanation that we can write in words So our first step is going to be add the first number of hours To the second number of hours and then add the first number of minutes To the second number of minutes Then we're done Except When the total for minutes Is greater than 60 Then we have to subtract 60 from the total minutes And add one To the total hours And we have to make a decision When we come up with a total number of minutes that's less than 60 were cool But if we go over 60 or over to 60 or more In fashion greater than or equal to 60 Because we can't have something that lasts five hours and 60 minutes. That's six hours Then And there's a problem. We don't know how to do this yet In java, we don't know how to do decision making and that's not going to be for a few chapters So do we just quit and go home? No, what we do is let's think is there's another way to get this 82 minutes Into one hour and 22 minutes that will also work when we have something that's 48 minutes And we can say well 48 minutes is really the same as zero hours And 48 minutes and then if we add them We'll get six hours and 48 minutes and we'll get the same answer. So there's our goal Our goal is to have something that we can do with 82 minutes that will split it into an hour and 22 But we'll take 48 And we'll split it into zero and 48 and then we can do the addition in both cases and it'll still work out great Now you may not have that spring to mind of how are we going to do this? So let me ask you another question if I have something that takes 500 minutes How many hours and minutes is that? Let's see if you can solve that problem And again pause the video Give it a shot and see what you can come up with Okay, let's take a look at what happens here Many of you might have thought well, okay I'm gonna can't subtract 60 because there's way more than 60 minutes here But what I could do is since I know there's 60 minutes per hour I could say 500 Divided by 60 that gives me eight hours With a remainder of 20 Or it's 500 divided by 60 like we did in grade school is eight remainder 20 And that's exactly what I want if I take my number of minutes and divide by 60 That the quotient gives me the number of hours And the remainder gives me the number of minutes left over Let's check to see that that works on 82 if I take 82 divided by 60 Our quotient is one And our remainder is 22 that's exactly what I want Now comes the big test What about 48 minutes If I take 48 divided by 60 that doesn't even go into 60 once that means my quotient is zero And I'll still have all my 48 minutes left over Aha, there's my answer If I divide by 60 That'll give me the number of hours And whatever's left over it will give me my number of minutes And remember I can take the remainder In java With the modulo operator That gives me my 20 and because integers divided by integers are integers That's what I need. That's the key to solving this problem So let me rewrite this I'm going to add the first number of hours the second number of hours and the first number of minutes the second number of minutes That's a given always have to do that Now Take the total minutes And divide by 60 Add that result to total hours. In fact, why don't we do this and make this more explicit? comma Giving total hours And that's giving us total minutes again Then take the total minutes mod 60 That result becomes the new total minutes There's 82 This one is 82 divided by 60 And the 22 is 82 mod 60 We don't subtract it. We just replace it Okay, now we have our algorithm now we have our method for solving it The important part of what we did is first we tried to solve the problem by hand Once we solved the problem by hand, we stopped every once in a while and say wait a minute. How did I know this? How did I know that 82 seconds is The same as one hour and 20 minutes excuse me is the same as one hour and 22 minutes. How did I know that? Again, we can do this so automatically that we never think about how we do it But when we're explaining it to a computer we have to go through that and say How did I know that? What was I really doing in my head when I converted 82 minutes to one hour and 22 minutes? Not I've done all this work. You know what I'm going to copy this down I'm going to put that here in my comments at the beginning of the program And let me just make this a little bit nicer looking So everything lines up pretty Ah, perfect Now that I have my plan I can start writing my program Well, there's something missing from the plan I have to ask the user for the first task number of hours and minutes And then I have to ask the user for the second task Number of hours and minutes I can't forget that when I write down my steps for the computer to solve the problem, but I was my my My goal earlier was to figure out how do I do this calculation in the first place? That was the important part Not that this isn't important, but I can think about that later. That's a separate problem to solve Okay, now I got my plan so Um system dot print Enter a number of hours for first task And let's call it hours one And that's going to be input dot next int We're going to use integers all the way through here. There's going to be no decimal points because we don't want, you know 37.5 hours. That's just going to screw us up completely And then enter a number of minutes The first task So minutes one is going to be input dot next int I'm going to save myself a little bit of time by copying and pasting this second task Second task and we'll call this hours two and minutes two You know what? Let's just test this to make sure that this works out for all right Let's compile this And run it So we have four hours and 17 minutes and then three hours and 22 minutes. Okay So at least it's reading things and it's not crashing A little bit ugly. I'd like a blank line in between the first task and second task. There we go Notice I put the blank line here in the code to make it easier to see where the code is grouped together Okay, we have our numbers hurrah So now what we need to do is we need to add the first number of hours to the second number of hours giving total hours cool So that means our total hours is going to be hours one Plus hours two And our total minutes is going to be Minutes one plus minutes two Now what we need to do is we take the total minutes and divide by 60 and that result gets added to the total hours So the total hours becomes whatever its old value was Plus the total minutes divided by 60 And by the way, if I wanted to avoid magic numbers like 60 I could have also said Final integer Minutes per hour is 60 And then that divide that by minutes per hour And that might make things a little bit more readable or more understandable Then my total minutes is going to become The total minutes Mod minutes per hour And now I can print that out Total time is percent d hours percent d minutes period and new line Fill in the first blank with my total hours And total minutes for my second one Now if somebody gives us a weird numbers like you know negative five hours and 3000 minutes We're going to get crazy answers, but there's an old saying in the computer world, which is Geigo Which stands for garbage in garbage out So if somebody gives us weird numbers that are totally meaningless, it's on them Let's compile this and let's run it So let's say we have three hours and 20 minutes And our second task is four hours and 45 minutes This one we can do in our head and that should be seven hours and Oh, wait, let's make it 15 minutes. Let's not go over 60 minutes first first thing out So we should have seven hours and 35 minutes And the total time is seven hours 35 minutes cool I want another readability blank line in there Right again. So again three hours and 20 minutes plus four hours and 15 minutes gives us seven hours 35 minutes. Perfect Now let's run it again where we have two hours and 47 minutes And three hours and 35 minutes Remember that's our 82. So we should end up with six hours and 22 minutes And yay, we got six hours and 22 minutes Now what happens if somebody says, okay, I have um five hours And one minute and two hours and zero minutes That's going to say total time is seven hours one minutes. So our grammar is incorrect Um, ask me if I care Answers. No, I don't care. So we're just going to leave that the way it is We'll worry about fixing that when we can do decision making to figure out whether we have a one or not I I don't even want to think about that right now Because that's not the important part of this the important part of this is what we have done Let me save this file by the way When we did our planning We figured out how to do it for the Case where we don't go over 60 minutes Then we figured out what happens when we go over 60 minutes And then we did the subtract 60, but that gave us a decision to make and we couldn't do that So we had to find another way to do it and we did it by solving a different but similar problem For 500 minutes and that leads us to the division and remainder and that works everywhere So there's our program Now there's one thing that I want to tell you about java Now that we have our program working And one thing that's slightly bugging me here Is when I run this I have four prompts. So it's four hours three minutes and seven hours and 59 minutes Gee wouldn't it be nice if I could get away with having less than four prompts? It turns out we can do this in java Let's save this as total time two dot java And that means this has to be called total time two And I'm going to write this in a comment here so that you will be able to refer to it later When you use the next Int next double, etc It uses what's called white space namely blanks tabs and new lines to determine Where the end of your input is That means you can ask someone to enter multiple values separated by spaces And next int Will be perfectly happy with it Let me show you what I mean Well, we're going to say number of hours And minutes for first task separated by spaces Then we'll do a next int And the next int We'll have the second task minutes for second task separated by spaces And then we'll do two next ints in a row and let's run it Well, that was exciting. Oh, yeah, I've got a compiler before I run it that would help Earth Eisenberg So now I have four hours and 17 minutes And three hours and 22 minutes Which gives me Seven hours and 39 minutes When things are related to one another It's probably good to ask for multiple values separated by spaces When things aren't related you probably want a different Prompt for each one. So for example If I were to have something where my prompts would be If I wanted to enter a unit price and a quantity And then figure out the total price Okay, I would probably do something like this system the out dot print Enter unit price And then double price becomes input dot next double And then I'd say system the out dot print enter quantity And I'd have an integer quantity which would be input dot next int These are not really closely related They're clearly different values So I want two different prompts I wouldn't say enter the unit price and quantity separated by spaces that just wouldn't it just it would feel wrong Whereas in the current program When I ask, um, what was it called here? Enter numbers hours and minutes That's okay to ask for both items Because we're both numbers Because they belong together They're very closely related. Well, let me put this in the notes So question is When do you ask for multiple? items on an input line And when do you use separate prompts So for something like this Dent this just so that it looks a little better there. It's easier to read And this is a guideline. This is not a hard and fast rule Um, if you can sometimes I can make a good argument for asking for multiple items Sometimes I can make another an equally good argument for individual prompts You'll develop a sense of this the more you program Is sometimes the assignment will tell you That it is better to ask for the things all on one line and then you should do it Otherwise use your best judgment That's not entirely related to what I was talking about here. Again, here's the important part of tonight's mini lecture is I write down what my task is in my comments. I do this before I start programming not after the at the very end I want to focus on the problem. I want to see okay. What is the problem? I'm actually trying to solve I want to Put that in enough detail that I can focus my mind on it Then the next thing I say is okay now How would I do this by hand because if I can't figure out how to do it by hand? There's no way I'm going to be able to tell a computer how to do it That's where I start doing these examples I do one example where everything works out great I do another example where things don't work out quite so great and I have to figure a way around it And maybe my first approach might not be one that use that I can do because I don't know enough java yet So I have to go for another approach which was This one here with the 500 minutes And that gave me the key that I need to unlock it And this is why planning is really really important. It will save you a lot of time later on if I had just started writing Yes, I could have planned this out all out in my head because I've been programming long enough But if you haven't been programming for a long time or if you have a particularly difficult problem You really want to sit down preferably away from the computer by the way and figure out. Okay. How am I solving this problem by hand? If you spend maybe a couple of minutes of planning It can save you hours and hours of programming where you're trying things and throwing stuff at the wall to see what sticks That's not a good way of doing your programs So and that's my sermon. I mean my mini lecture for tonight. See y'all whenever