 Let's pick up where we last left off. When we left off, we had implemented the time class, which has these attributes, hour, minute, and second. And they're private, which means the only way we can get to them is through the getters and setters. That is the only way we can get to them from outside this class. And we have our constructors. This is the no argument constructor. And the constructor that needs three arguments. And we've noted that the constructors can call other methods, which allows us to avoid duplicating code. And here was our UML diagram. And the last thing we did was we implemented the two string method, which is very nice because what that allows us to do is to be able to say print LN, and it'll implicitly call two string. And by the way, if you want to, there's no harm in explicitly calling it either. The next thing we'd like to find out is if there are two different times, are they equal to one another? What we can't do is we cannot use the equal equal to compare times. Instead, we're going to have to write an equals method to do the comparison. And here let me show you an example. I have a time called time one, which I create as 930 in the morning. And then I assign time one to time two. Then I create another brand new time object that also says 930. What that's going to look like when we look at the memory diagrams, it's going to look like this. Time one and time two both refer to the first object I created. Time three refers to a completely different object. When I print them out, they'll all look exactly the same. The equal equal operator compares references. When I compare time one and time two, I'm going to get true because they both refer to the same area of memory. When I try and compare time one with time three, I'm going to get false because time one and time three are not the same reference. And in fact, let's compile this and run it. So all of them look the same, but the equal equal doesn't do what we want. This is exactly what we had going on with strings, by the way. And that's why we needed to use the dot equals method in order to compare two strings for equality and do it the right way. We don't care about the references. We care about the data that they refer to. That means here in our time dot Java file, we are going to have to create a public method that returns a Boolean. And it's going to be called equals. We already have access to one object, this object. We're also going to have to compare this object to some other time object. And in fact, in the book, they give the code for this. They call it this and that. And we want to make sure that the hours are the same. The minutes are the same. And because we're using floating point. We want to make sure that the difference between the seconds is less than some small number, which is going to be one 1000th. So let's copy that. And let's paste it on in here. And we're going to return the expression this hour is equal to the other hour. And this minute is equal to the other minute. And that's a legit, a legit comparison. Because remember, hour and minute are integers and I can use equal equal on integers. And finally, the difference between the seconds between this and the other has to be less than point 001. So I'll take care of your weirdnesses with the way floating point works. Let's compile that. Now I can come back here into my time equality. And I can say system dot out dot print line of time one dot equals time to is. And now I'm going to compare the contents of time one and time three, and that should also come out to be true. Oh, well that's exciting. I forgot my closing. Her emphasis. That would help. And now I can run it. And the equals method returns true, which is exactly what I expected it to do. This tells us by the way, okay, let's let's put this into our UML diagram. So equals of time other. Normally, when I write a new class, I always start off with the attributes. Once I have the attributes, I immediately move to the constructors and implement those. And then what I like to do is I like to implement to string next. The reason I do that afterwards before I do my getters and setters is so that I can see if I am constructing my objects properly or not. Then I'll usually do my getters and setters. And probably the last thing I'll implement is equals. But wait, there is more there's one more thing that the book says and it would be what if I wanted to add two times. Instead of time of day let's pretend that they are durations so something is two hours 15 minutes. And I want to add some as four hours and 17 minutes. Or if something starts at two 15 and it lasts for four hours and 17 minutes, what's the ending time going to be. Now there's two ways to do this. One is the way that we normally think of it. Well, let's just write a function called add, and it'll be time t1 time t2. Oops, I'm sorry. Hi, I'm in UML. It's going to be time t1, which is a time and t2, which is a time and it'll give us back a new time. The other way we can do it is by using this pattern that we saw with equals we can say, hey, we've already got an instance of a time. Let's have an add method that takes this time and adds it to some other time. It turns us a time. Now, I've got to get into something very, very, well, it's not very ugly, but it's semi ugly. It's time to discuss it now. What do I mean when I say an instance method. When I say an instance method, for example, if I say get our, I can't say something like for example, um, well, get which hour, the hour for time one time to time three some other time. Whenever I use this method get our, I have to give some object that to get the hour out of, and that's called the instance. That's why it's called an instance method. And this goes into our notes I think would be very nice here. I guess I'll add it on to what I had from the previous mini lecture. So method is one that needs an object. To work on. In our time class. I can't say, instead, I need to provide an object. An instance, and this is called instance of the time class to work with where time one is declared as a new time object. Sometimes though, I don't need to have an instance to do things. Here's an example we've seen this before. When I say for example, integer. Positive is equal becomes math dot absolute of the absolute method belongs to the math class. Absolute value has all the information it needs. I don't ever have to do something like this. I don't have to say math. My math object becomes a new math. And then I can say, all right now, positive becomes my math object dot absolute value of number. In this case, I don't need an instance of the math class. The math class owns the absolute value method. Or an attribute, by the way, belongs to the class as a whole. Rather than any specific instance. It's called a static method. And that's what that keyword static means. It means this method. I don't have to build a new instance of the class in order to use it. It's just there because the whole class shares it. So in this case, this is an instance method. This is absolutely 100% an instance method because I've only got one time here. On this version of ad, it has all the information it needs. It doesn't need to have an instance of a time object. It has both the time objects that it wants to work with. That means that this one's going to be static. And in a UML diagram when something is static, it's underlined. Let's do this one first, the non static version. I don't know which one the book does first. I'll follow the book on this. Follow their lead. Oh, they start with the static one. Okay. Fine. They're going to work on the static one. I'll go with the static one. And for me, so we're going to have a public static method called ad, and it's going to take time t1 and time t2. And then we're going to say we're going to create. Oh, we need to know what it returns returns a new time. Hello. So we're now going to say, well, let's look at their code here. Oh, I like their code. And for me, I like that that will do. I mean, create a new time that says to plain old default. I'll add the hour from this one and the hour from the second one and return it there. Okay. Hmm. I guess I need to make right a file to test this, don't I. So let's make a template here. And let's save this as time at test. Add times to see that we can use the static version and the instance version of add to add to time objects, giving us a new time object. We're going to have time t1. Oh, it's called time one is going to be a new time and let's make it 205 and 13 seconds. And then time t2 time to he's going to be a new time. And we're going to make it let's say seven hours, 45 minutes and 26.5 seconds. And now we want to say is time, time three becomes now what do I put here before the ad I need to put something here. And it turns out when you have a static method, you could either give an instance or the name of the class and by convention when we're calling static methods, we use the name of the class. We're calling the ad method that belongs to the time class, not to any particular instance. And then I'm going to say print one of some of the times is plus time three. Let's compile that and run it. And some is nine hours 50 minutes and 39.5 seconds. Well, that's all very good except we're going to have a slight problem here. Let's call this time, big time, and it's going to be a new time that's going to be 14 hours. 59 minutes and 50.5 seconds. And let's say time, big sum becomes time dot ad of, let's say, time to, and the big time. Hey, today is missing space bar day. And let's run this. And holy crap. Okay, we got 21 hours, 104 minutes and 77 seconds. Well, that certainly isn't very nice is it. Well, I guess that means we have to go and improve our code here, and they have a way of doing it here in the book. And what they do is if the seconds is greater than 60, they subtract 60 and add one to the minute. If the minutes is greater than or equal to 60, they subtract 60 from the minutes and add one hour. And if it's greater than 24 hours, they do a sum dot hour minus and becomes 24. Do I want to keep this the way it is? Sure, why not? Let's copy this. I could do it by the way with mod. Okay, but for right now, I'll keep it the way the book has it. I prefer to do it with mod, but that's just me. By the way, let me just do this one by hand to see what this is going to come out to. What was my add times that I'm having here I have 74526.95 to 1459 and 50.5. So that comes out to 01677 which should be 17 seconds. Okay, a minute here that's 45 plus 60 which is going to be 45 should be 224517. I think I'm doing this really fast and halfway in my head so. Oh, yeah, I guess it would help if I recompiled my time dot Java wouldn't it. Now let's rerun this. Well that was exciting. Came out to zero. Holy crap, what did I do wrong there? Hmm. Wow, this is a little me reread. Let me recompile it just to make sure that I'm. Ooh, dang, we know he's something went really bad here. Oh, hi. When I pasted this stuff in I got rid of the original code. Okay, let's try some dot hour becomes some dot hour. T1 dot hour plus T2 dot hour. Great. Well, that's certainly what was exciting wasn't it. And some dot second becomes T1 dots second. It's going to be one of those days I can tell already. And sure enough, excellent. Now the times add up properly. Okay, cool. We now have our static one. And what is to have this one that's the instance method. And what we're going to do here is public add to time other. It's going to be the same logic. Copy this in here. This time it's going to be T1. I'm going to replace that with this. T2 is going to be other. So I'm going to take this hour and add the other hour this minute at the other minute this second and add the other second. And then all the rest of the logic remains the same. So now let's do a system dot op dot print line using the instance method. And in fact, let's go here and put here. What I'm going to say is time three becomes time one dot add time to this time, the one I'm currently working with gets added to that one. Big sum becomes time to add to big time. And we're going to hear say that the sum of the times is plus time three. And they should come out to be exactly the same. That's exciting. Let's go back to here. And let's compile this again and because I see we have an error here. Oh, because I never said what the return type was. And that's why I couldn't find it. There we go. Now when I come back over to my test add times and compile it. It will compile and now when I run it. So I now have two different ways, but they both come out to the same result. So the question, should you write instance methods? Or should you write static methods? And I'm going to say it depends on what the users want and what users are going to be doing most frequently. And sometimes you want to implement both the way we've done here. So I'm just going to say, it's complicated. I'm just going to say that. So I'm going to put a couple more notes in here. Diagram or line static methods to indicate. So again, calling an instance method. You must have an object. Like a static method. You use the class name. Okay, a couple more things I want to say about this class and then also I want to talk one, say one more thing about static. And notice there's a lot of duplicated code here. I have time dot add other and essentially it's exactly the same logic for both of them. Well, it turns out, I don't have to duplicate the code. This method has an instance, T one, it has another instance, T two. I could just as well say return T one dot add T two. So my static method, because I've given it two instances. I can use the instances and call the instance method and not have to duplicate any of that code. And in fact, if I compile this. And run it. It still works like a champ. Another thing I want to say about static is you can have static methods. And you can also have static properties. These are the instance attributes. And every instance can have different values. Now what if I wanted to do some fancy schmancy stuff with AM and PM and noon at midnight. So I could have something for example, a public string array called suffixes. And it would say midnight AM noon and PM. And I could use those when I'm printing things out. Now, we have all agreed that this instance attribute every time object that I make is going to need its own hour. They can't all share the same hour. So every instance of a time every time object I create is going to have its own copy of minute. It's not shared among everybody. Does every time I create need to have a sense own copy of this array. No. Because it doesn't I'm going to say instead of this hour, minute and second belonging to each instance. I'm going to make this a static array and that means it will belong to the class. So this is a class wide attribute that all the instances share and can access. Again, I have static attributes, which means they're shared by all members of the class static methods are methods where I don't need an instance in order to call them. Is that worth writing down in the notes. Yeah, you better believe it is a static attributes are shared by all instances that methods don't need an instance in order for you to call them because they belong to the class. Okay, that pretty much wraps up what I want to say about chapter 11. Now, I looked at the assignment and the assignment for chapter 11 isn't due until a fair amount of times from now. And that's a good thing because I will go over some of this stuff again next week, I will not be available on Monday and Tuesday. So I'm not going to be doing any question and answer sessions then, but when I pick up on Wednesday, I will go over this object stuff again by doing one of the examples in the exercise book. And the one that I want to do is this first one defining a class named lens to represent a camera lens. If you want to work on ahead, my suggestion is if you do this, the first thing you want to do is you want to draw a UML diagram that summarizes all the attributes and all the methods that you're going to need. And then you're going to write a program that tests the lens object. And I've written the start of that here by the way. So it'll be in the sample files. Here's lens and I've copied in all of the documentation there. And I you get, oh, we don't need scanner by the way, because it's not going to read anything. And then we have lens test where I've written the first part of it where you're going to enter the focal length for the first lens and its aperture and the second lenses focal length and aperture, and then the remaining code goes here. And it's going to be calling methods that you have created here inside of lens dot Java. In order for everything to work out right you'll have to have both of these files in the same directory or same folder. In this particular instance, time dot Java time equality test time objects, all of those are all in the same folder. And that way these three programs all have access to that class. This gives you some time to give it some thought and to work on that if you would like and if you want to work on it on your own and send it to me as I have time if time permits I will look at it. And again, if you are working on it and you have questions, write down specific questions and then I will be able to address them when I do more of the mini lectures. I probably will not be doing a mini lecture tomorrow, by the way, because I've, I've gotten far enough ahead into chapter 11 that I don't want to. I don't want to get out over my skis, and that should do this mini lecture. See y'all whenever.