11:38:23 Welcome to our module objects and classes. In this lesson we are going to look at a different methodology for coding up until this point. 11:38:39 What we were writing were procedural programs. We learned many things along the way. 11:38:46 The long list is listed in the week below. All the topics that were covered in this course. 11:38:55 Well, this is new. Topic is a way of coding. 11:39:00 So what you're looking at is a calculator, typical calculator found on your computer. 11:39:06 And so we're gonna use this model model to learn object-oriented programming. 11:39:13 And so our basic calculator. Let's say I'm going to do some math here, and we'll do something pretty simple, so we can see that it works. 11:39:21 And so something like 2 plus 3. But I just want to keep on adding. 11:39:27 So I have 2 plus 3 plus 4 minus 5, and then I'm done. 11:39:35 Then I hit equal. So 2 plus 3 was 5, but then that 5 was reused when I added, plus 4, okay. So we had memory. 11:39:46 There. So 5 plus 4, because 9. And then we reuse that 9 to do our last part, minus 5, ending up with 4 right? 11:39:54 And so this is how basic calculator works. Now you'll remember in an earlier module, when we were learning methods, you built a super calculator. 11:40:05 And so you added features to your procedural program that from the menu you could choose different options for types of calculation, and you are only limited by your imagination. 11:40:17 Well, we're going to build more of a standard calculator. 11:40:20 But using this little methodology called object oriented programming. 11:40:25 So let's take a look at the code. The code is also available for you in this week's module for you to run and add other features. 11:40:35 So here we go. So you're looking at our public class calculator, driver 23. 11:40:44 Here's our main method, and if you notice I have something called calculator type. 11:40:51 Remember we have. We always had a data type, an identifier and here I'm using that new. 11:40:58 And so I'm creating an object called Calculator. Okay? 11:41:01 So you can see I have 2 calculators here. Calculator one in Calculator 2. 11:41:07 Now I don't play much with Calculator 2. And the reason for that I'm just demonstrating in this program the fact that you can have multiple constructors and so that's called method overloading or constructor overloading where you can see the first calculator add no arguments. 11:41:26 Okay, so no. Parameter list. That's our default. 11:41:29 Constructor, and then I had a second constructor constructor which took an argument specifically. 11:41:34 It took an operator. So with in other words, you calculator 2 is going to work as an adding machine, and so that's just to demonstrate 2 different constructors which I'll show you in a moment. 11:41:47 But then, you know, the next line of code just says one. 11:41:51 So that's my identifier from my calculator. 11:41:54 One, and it calls a method called calculus. Pretty interesting. Huh? 11:41:59 Not a lot of code there. Yet the miracle happens that this works as a calculator, just like when you drive a car, you don't necessarily need to know all the inner workings under the hood to make the car run you need to turn the key. 11:42:16 On, put it dry. Start steering, and use the gas and the brake, but other than that, it works well. 11:42:23 This is the idea of object oriented programming. So, in other words, when you make a call to calculate the calculator just starts working. Now, how does it work? 11:42:32 Well, that's behind the scenes, and I will show you that in a moment. 11:42:36 But for now let's make sure my code compiles. 11:42:40 Oh, please do that would have been so embarrassing! 11:42:43 And now let's see how it actually would run. Okay. 11:42:46 So we have a calculator. One I've constructed it. 11:42:49 Those are my constructors up there and then I tell one to calculate so what I'm going to do is I'm going to move this up a little, since we're seeing the code. 11:42:57 That's a driver. And now I'm gonna run it. And I'm gonna show you what happens. 11:43:03 Okay, so let's hit run. And you see, we have a blinking cursor here, why, it's waiting for some input, so let's say, I put in 3 point. 11:43:10 Oh, so that's my first operand, right? And I connect that with some sort of operator. 11:43:17 So let's say, I'm going to add right and then I'm gonna add 2.5 to it. 11:43:21 Okay. But I'm not done. I wanna do some more math. 11:43:25 Well, let's make this interesting. Let's subtract. 11:43:28 Okay. So I'll subtract point 4. Okay? 11:43:31 So at this point, if you're doing the math in your head, you've got 3 point o plus 2.5. 11:43:39 Well, that's 5.5 minus a point 4. So we should be at 5 1. 11:43:44 Okay. Well, my next operator is equals, and so this will then do the math for me and give me my calculation. 11:43:51 Well, sure enough, it's 5.1. Now notice the blinking cursor. 11:43:55 It's ready for another problem. So let's say, I just do 4 minus 7, just for whoops. 11:44:03 Can't put 2 on one line. It's just we have to enter an operand, an operator, and let's just let's do minus 2. 11:44:11 And then when we hit the equal sign, we should end up with 2. Okay? 11:44:16 So? How is the heck? Do we ever shut this calculator off? 11:44:20 Well, just like your calculator has an off button. How about if we hit lowercase? 11:44:24 Oh! So when he hit lowercase. Oh, the process terminates so until I hit a lowercase. 11:44:31 Oh, I can keep adding and subtracting, and my problems can be as long as I need. 11:44:37 As soon as I hit the equal I will finally get that results. 11:44:41 Of the entire problem. Now, this calculator that I've built only adds and subtracts, and I purposely did that, so that I would leave room for you to try to add multiplication and division to it. 11:44:54 Nice common features, you could add other features as well. The idea is, this is code for you to play with. 11:45:02 Now, how did this happen it wasn't a miracle. 11:45:07 It is object oriented programming. So let's do the big Reveal. 11:45:12 Let's take a look at the code. Okay? So hopefully, you've printed this code because it's also available. 11:45:20 And you're walking through it and putting some notes. Now I put a lot of comments in, but you probably want to add more yourself. 11:45:28 So you see, our main method was nice and clean. It was very short. 11:45:30 We created an object, and then we called calculate, which ran our object. 11:45:34 Now notice our opening, curly brace here. It doesn't close. 11:45:39 Why, at the very end it'll close, because within this public class I have created a an object. Okay? 11:45:50 So I have my public state class calculator you'll spend more time with private private and protector as you go into programming, too. 11:45:59 So I don't want to be a complete spoiler on that but I've created a public object called Calculator. 11:46:05 Now notice. Remember going back. Hmm! To one of the early modules when I introduced you to the idea of object oriented programming objects have attributes that describe the object. 11:46:20 So think what adjectives describe nouns right? So think adjectives, and they have behaviors, action, words, right, and so calculate is definitely an action word, isn't it? 11:46:32 And so that's gonna be one of our behaviors. 11:46:35 So one of the first things that we are actually looking at. 11:46:38 Let's see if I can zoom this a little bit. 11:46:41 There we go in our calculator. The very first thing that I'm doing is, I'm declaring some attributes. 11:46:50 Okay, they're basically data types that are going to store information. 11:46:52 And they're all private. So, in other words, Maine could not access these only within the calculator itself can these be accessed, and so I'm gonna store 2 operands, A and B, okay, so there's my double a and double B so those are the 2. 11:47:07 Values, because the operators I'm working with are binary operators, addition and subtraction need 2 operands in order to work and so those are my operands, A and B, and then obviously, I'll be calculating an answer. 11:47:21 That's the result of the calculation. And between those 2 operands I need some sort of operator. 11:47:27 And this case I'm only working with addition and subtraction. 11:47:31 Now I've also made some storage space for memory, because, remember, in the example we saw with the calculator, if I continue to have options, I had to use the preview calculations answer. 11:47:43 So memory will store the previous calculations. Answer, and then, now I have another variable called new data, because I'm going to use this if it's not one plus 2 equals. 11:47:58 If I don't hit an equal sign right away. I don't need a new operand. 11:48:04 A why opera and A is going to be using that memory from the previous operation. 11:48:10 So I have this Boolean flag that I would set to falls if I didn't hit an equal sign. 11:48:20 Yet I've got another value in another operator that'll signal to me that I' to use memory instead of a actually, what I'll do is I'll set a to be that memory value from the previous operator operation. 11:48:35 So let's take a look. So the first thing that we do when when we build an object is we are going to need constructors. 11:48:43 And so you see, constructors don't have a return type, but they're main purpose is to set all of those attributes to some sort of initial value, or in the case of the calculator to, I actually set the operator. 11:48:57 So let's look. So here's our basic default constructor and all. 11:49:01 I'm going to say is that the operator. So let's look so here's our basic default constructor. And all I'm going to say is that this whichever calculator is calling it in the case of mine with calculator, one it means one's value for A B answer OP memory and new data, are set 11:49:15 to these initial values. Okay, so that's the constructor that is called when I created the object. 11:49:21 Now, if you remember calculator 2 actually sent a piece of data. 11:49:26 It sent a character piece of data to set the operator, and you can see. 11:49:30 So I have character. X is going to be assigned to the operator, otherwise I initialize everything to 0. 11:49:37 So you can have multiple constraints for an object, and you can have a different signature. Parameter lists. 11:49:45 Now, if you notice though there is no return type for these, and they share the same name as the object itself. 11:49:52 Okay. So we got past the constructors. There. 11:49:57 It is void. Calculate, remember, calculate, doesn't bring anything back either. 11:50:01 It just turns the calculator on, and off it goes. So this is where we need to spend our time. 11:50:06 How in the world did that miracle happen? Well, let's take a look. 11:50:10 Well, in this case we know our calculator is going to get information from the keyboard. 11:50:15 So in this case, here is where I declare my scanner. 11:50:19 So, if you notice I did import that statement at the beginning of the main class, and so now I'm gonna need the keyboard here. 11:50:31 So where I need it is where I declare it. Okay. 11:50:34 I am going to be using a string array, and so to access each of the individual strings. 11:50:42 I have an indie as an index. Here is my boolean that turns the calculator on. 11:50:48 And so when I started, I set that bully onto troops. 11:50:51 Yes, I want to run the calculator. We'll see how later how that Boolean on will be set to false to shut the calculator off. 11:50:58 We know it's going to use that lowercase. 11:50:59 Oh, for that to happen now, my string array input is of size. 4. 11:51:06 Why, I get a piece of data, an operand. I get a piece of data. 11:51:12 And operator. I get a piece of data, my second operand, and then I may get another operator, or I may get the equal sign to say the problem is over. 11:51:25 So I need 4 strings to get started. Okay, now, I said, input 0 and input of 2 to X. 11:51:36 Now, the reason for this is because I am looking for that. 11:51:42 O in those 2 positions to turn the calculator off so, in order to get the loop to run the first time I needed some sort of initial value to just pass over that it's not off already. 11:51:54 Okay, so those were just to make sure that my loops were gonna stay on. 11:52:00 Now, as you know, with a good program games that you play once you've launched them, you at least do them once. 11:52:09 So I'm using a do while loop to run my calculator. 11:52:12 Okay. And so let's real. Look very closely here. 11:52:16 So here's our due, and we're going to do some calculating. 11:52:20 I'm going to go back in slow motion and show you all that. 11:52:22 But let's go to the end to the while. How do we get out of this how do we shut the calculator off right? 11:52:27 Okay, so we do and let's go all the way down to see the while, and then we'll come back and look at all the rest of this code. 11:52:35 While on. Remember, that was my bullion, and initially I set it to True. 11:52:42 So, while true, this loop runs now that means somewhere in the body. 11:52:48 Remember my Mo. I am going to have to turn that calculator off, and we'll see that in a moment. 11:52:54 Okay, so let's go back to the top at the due. 11:52:57 Yeah, boy, there's a lot of code in here, isn't there? 11:52:59 Okay. And the next thing is, if new data equals true. Okay? 11:53:07 And if you notice in my constructors, I do set new data to true. 11:53:11 So the first time around new data will be true. 11:53:15 I need to get those 4 pieces of data. 11:53:18 So here I have a for loop that runs to what less than length, so its length is 4 runs to 3. 11:53:26 So 0 1, 2, 3, my indices. It will get those pieces of data. 11:53:30 Now, if you recall when I was entering it, I entered a piece of data, then hit. 11:53:34 Enter on the keyboard, and then the next piece of data. 11:53:37 So you see, each piece of those data is going into my string array step by step at 0 is my first operand, A at one is my operator. 11:53:48 It's either plus or minus at 2. 11:53:51 It's my second operand, or B, and at 3 it's the equal sign. 11:53:58 If I'm lucky if it's not the equal sign, I'm continuing to do a problem, it's another operator. 11:54:03 So you can see that is what's being placed into this input array. 11:54:08 Okay. Now, I still can't work with the data, because this this is the calculator that does math. 11:54:14 And my data is coming in as a string. So we're gonna see how I handle that. 11:54:18 In a moment, but notice, it's loop that if input at 0 equals oh, stop the loop! 11:54:27 So, if literally, that will cause this to terminate. 11:54:31 Now then, it doesn't have it to cause it to terminate. 11:54:35 Now, but in a second iteration, or pass, it will allow this loop to be executed. 11:54:41 Okay. But as you can see in in the first pass, I haven't set to act so this isn't true. 11:54:47 So we're more concerned in the second pass through not to take in date. 11:54:53 If the user said, Stop, okay. So that's the loop to get a fresh problem started. 11:54:59 But new data is not equal to. True, there's a matching else, and, in other words, this means that we've already had a problem at this point. 11:55:08 So we only need to fill 2 pieces of data right? And so that means in that last position at input 3 from the previous 4 loop, Dan had an operator in it. 11:55:23 Instead of equals, it had a plus sign. So I'm going to set the operator to be whatever that last operator was, and then I will still need to do what get another piece of data, and then maybe hopefully, the equal sign. Or again, it might be. 11:55:42 Another operator. So if you notice this, for loop only goes. 11:55:45 I equals 2. So in other words, I'm starting at Operand B, and then I'm getting either an equal sign or another operator. 11:55:53 Again. I have that protection to make sure that the user didn't want to stop that's what that old there is of the character at. 11:56:01 And in this case, because the loop starts it to, I can't say character at 0. I have to take character. 11:56:06 At 2. Okay, so great. Now, we have in this one, we possibly have another operand, and another operator, or we have the equal sign. 11:56:17 So this is how we were getting the data right now, after we get the data here's where we check. 11:56:25 If at 0 or at 2, it's at the lowercase. 11:56:30 Always a user wants to shut the computer off, get set to false. 11:56:35 And so now you can see we fall out of the do while, and the calculator stops calculating. 11:56:44 Okay. 11:56:47 Let's go back here. So here was our if else right. 11:56:50 So, if calculator was not turned off, that means we do have to do some calculating. 11:56:57 So what happens in this else? Well, the first thing is, we take that first string at 0, and as long as the value is in the range of what the character 0 to 9. 11:57:10 And this is when you look at your Asci table, so it has to be in the range of 0 through 9 or it has to be a dot. 11:57:16 Right? Why, we're using the double data type. And so we could start with a fractional portion number like point 2. 11:57:23 So we have to look for that point as well. So if we find that, then we're going to set our a operand to that first string, so we're we're gonna say, double parse double whatever that string is. 11:57:40 Turn it into a double for me, and store it in a okay? 11:57:44 Now, if new data is true, what's in that next string at input, one we're gonna set that together. 11:57:55 Now remember, if this is not our first time through the loop, so it's not new data. 11:58:01 We don't want to set our operator to that. 11:58:03 We want to use the previous operator. Now, input. 2, either way is going to be our. 11:58:12 What are, the operate? So it follows the same path as that input 0 for our operand day. 11:58:19 We store that into? B, okay, now, if input, 3 is in equal to equal, we're lucky. 11:58:25 So somebody just wanted to do 2 plus 3 equal. If that is true, then if this OP. 11:58:32 Is equal to plus. What are we gonna do? We're gonna call add. 11:58:36 And we're also gonna see the last operator, meaning meaning the one at 3, which is an equal sign. 11:58:45 Because if it's an equal sign we want the answer printed out, and I'll show you how that works. 11:58:48 In a minute, and then I'm gonna reset new data to true, because if I reach an equal sign, then that means I'm starting all over again. 11:58:56 Okay. Now, if the operator was attract I'm gonna call a method called subtract again with A and B, and again with their last operator, which in this case would be equal sign. 11:59:08 So prints the results. And again, I sent new data to True. 11:59:11 Now that's if the equal sign is found. But what if they did it? 11:59:17 What if they said one plus 2 plus 4? Right? So what happens there? 11:59:22 Well at input, 3, I'm not finding an equal sign. 11:59:25 And so I'm feeling it, finding another oper. So if I find that operator, so if the initial operator which was set at that first position at input one, remember between 0 and 2, that was our original operator isn't original operators, a plus sign well, then i'm going to add and now look what 11:59:49 I do. I set a so. In other words, this means I did not find an equal sign, and I need to save to that memory. 11:59:59 I need to save the result of add. So let's just sneak down for a second at set a and take a look at it. 12:00:09 Okay. So set a, all it does is what's in memory is now put into that phone operand. 12:00:17 A, okay. While we're down here, I'll just show you what set operating does. 12:00:23 It takes whatever value is sent, and it makes it. 12:00:26 This app operator. Okay, so let's go back up. 12:00:32 See where we left off, so you can see all I did was take the memory location and store it there. 12:00:37 Well, in order to understand that you probably need to look at, add. 12:00:41 So I went a little fast. Let's go back and let's look at what add does. 12:00:46 Okay. So you can see the whole process. So add takes those values of A and B, and what does it do? 12:00:53 And it also takes that what the last operator is now, if it's equal sign, that's good news, we'll print out an answer. 12:01:00 But it's not the equal sign. We'll avoid the output statement. 12:01:03 So you can see this answer is equal to a plus B, and if and in other words, that character that we're sending is equal to the equal sign, well, then, we wanted to display the answer otherwise we're continuing to calculate so we don't want to display and now, the last thing. 12:01:20 Add, does is set to memory. Whatever that answer was. So, as you can see, memory is now set to that answer, and so when when we saw a set, a, it took what was in memory and assigned it to the new a because we skip that a if if it's not a new, calculation if it's 12:01:42 not new data. Okay? So that follows for if the operator was plus, we have to handle the situation of the operator was also plus, or if that last operator was plus, we still have to look at the previous operator, because it might be subtract, so add and subtract work in the same way, but what 12:02:04 if that operator wasn't plus well, like, I said, we have a limited list here. 12:02:07 We have equals plus and minus. So I'm handling the minus now. 12:02:11 I wrote this as an if technically, I could just say else, because that's the only other operator I have. 12:02:17 But what if someone entered a an extraneous character? 12:02:22 I could put out an invalid right for data validation. 12:02:25 I try to keep this a little more simple as the example, and I wrote it as an Ls. 12:02:30 If, because you might want to add feature other than plus and minus. 12:02:35 And so this is already set up for you as an if, because then you can add, else if and add your features. 12:02:41 So, if that third value is not the equals, and it's not the plus, but it is the minus. 12:02:47 There's subtract again. I have to go back to the previous operator, and if that one was applause, well, then, I need to add, Okay, and notice here, this is where I'm setting new data to false. 12:02:59 And I might have skipped that over here. But I'm setting new data to false, because I'm gonna be using that memory location. 12:03:06 Remember I'm setting a as a memory location. And so again, if it's either plus or minus, I handle that and run it. 12:03:14 Okay? And while the calculator is on, and you saw how ad work, you'll see that track is basically the exact same thing, except instead of adding I subtract right here. 12:03:26 But the rest of the process is identical. We already looked at, set a, and we looked at Set. 12:03:31 So let's run this one more time. Okay, and I'll run it. 12:03:39 And so I turned my calculator on. But what if I don't want to actually run it? 12:03:43 I wanna turn it off. What happens? 12:03:46 This is why it shuts off, because at that position 0, I set it to off so originally it was set to X, and I set it off right away and off it shuts off. 12:03:57 Okay, so that means I have to run it again to do another problem. 12:04:01 What if I have something as simple as 1.3 minus point 2? 12:04:10 Well, that's our simplest base case. That's all new data. 12:04:15 And it blew up. 12:05:22 That's ever so embarrassing when that happens, and it's live and recorded. 12:05:27 It was my fault if you look. I had hit the shift button, and so I didn't enter the minus sign. 12:05:34 I entered the underscore, so if you can see, I've run it with 1.3 minus point 2, and sure enough, it works. Okay. 12:05:42 So that's our simplest case. And you can see the program is still running right. 12:05:46 So this is like starting all over again with new data. So let's do another problem. 12:05:52 Let's do 2 mine point oops minus point 7. 12:06:02 Plus. Let's do something simple we can follow for so what are we at? 12:06:08 We are 2 minus point 7. That should give us 1.3 plus 4, that you give us 5.3. 12:06:15 So let's hit the equals and see if we get it. 12:06:17 Yes, it works, I hope you enjoyed this tutorial. 12:06:22 The code is there for you to play with, and I would suggest at least making a multiplier and divide.