 Hi, let's move on to chapter 11 where we're going to be designing our own classes and I'm going to follow the book pretty much here Where we're going to create objects that represent a time of day by its hour minute and second Now normally I would design my entire UML diagram before even going to do the coding, but because this is the first time we're doing it I'm going to do them in parallel with one another Let's start off and look at our diagram The name of the class comes up at the top and then we're going to need the names of our properties So let me copy and paste this line and move it down to about there. That'll give me some room And now I can go here And I'm going to put in my attributes These are going to be public Excuse me. There's I'm not going to be public. These are going to be private attributes and we're going to see why in a while And that means I put a minus sign Than the name the hour and it's data type again backwards from what java does We're going to have a private attribute called minute Which will be an integer And we'll have a private attribute called second which will be a double That way I can have two hours 14 minutes 37.9 seconds And let me move this over just a little bit That takes care of my attributes Uh, remember yesterday when we were doing points, we were doing the new Keyword and then the name of the class and then any arguments that told us how to set up the attributes We're going to have to write that stuff ourselves and that's called a constructor This is calling the constructor. So the people who did java dot awt point Had constructors that they designed Now we're going to have to design constructors In our uml diagram that goes in the Bottom section where we have methods constructors are methods. They just happen to be incredibly special methods Our constructors are going to be public If which is a plus sign in a uml diagram If your constructor isn't public Nobody else can get to it which would be a bad thing The name of the constructor must match the name of the class exactly upper and lower case. Remember java is case sensitive This one is going to have no Our parameters This is called a no argument or no parameter constructor and that will give us a default value for a time of day Notice there's no return type. We don't say void or anything like that constructors are special We're also going to have a second constructor where we will be able to specify Exactly which hour exactly which minute and exactly which second We want as the initial values For these attributes Now that we have this let's go and write that up We're going to first construct first Specify our attributes We have an integer hour An integer minute and a double second Now we build our constructors And by the way, I've got to keep my keep my word here. We wanted these to be private So these are no longer public like x and y were in our point class Now it's time for our first constructor public time and notice it does not have any return type And we're going to say This dot hour becomes zero this dot minute becomes zero and this dot second becomes zero Time to talk about the keyword this what does this signify? in a constructor The keyword this signifies The object we are currently building So i'm saying assign zero to the hour attribute belonging to this object i'm currently building Assign zero to the minute attribute of this thing i'm building And assign 0.0 to the second attribute belonging to this object i'm building Now our three argument constructor Which takes an hour a minute And a second we're going to say this dot Hour becomes hour This dot minute becomes minute and this dot second becomes second That is why the this keyword is important If I didn't use it and I said something like the following like what I have here That would say say take the hour parameter and put it back into the hour parameter Which would be sort of useless By using the this specifier i'm saying take the hour parameter And assign it to the hour attribute that belongs to this object By using the this keyword You avoid name conflicts and ambiguity The book in fact uses this all the way through and I'm going to do so as well To make sure very explicitly when i'm talking about our parameter And when i'm talking about The attribute that belongs to some object Okay, lovely Now let's test this out What we're going to do is we're going to have A time object and we'll call it default time and that's going to be a new time Um something I want to do here. Let's compile this to make sure I don't have any errors in there cool And let's build a new time called c time And that's going to be a new time. That's 10 o'clock in the morning Two minutes and four seconds And let's print those out and we'll compile those And let's run it And oh goodness. Well, that's certainly not fun We have the name of the class and the reference to where it is in memory That's not terribly helpful Aha, you might say wait a minute. I've got a great idea Why don't we do this? Why don't we subprint line default time dot hour? plus colon plus default time dot minute Plus default time dot second There that'll fix it and the answer is Uh, no, it won't because we declared them private and nobody else has access to it Well, that puts us sort of a wrench in our plans, doesn't it? Now one thing we could do is make everything public But I don't want to do that and there's a reason I don't and I'll talk about that a little bit later So what I'm going to need to do instead is I'm going to have to have some public methods here that will allow me to access and change The values of the attributes and those are called getters and setters The getter methods are public And they provide access to the private attributes The setter methods are also public And they provide a way for people to change the values of attributes by the way Getters setters you will also see the terms Accessors and mutators Let's put these into our uml diagram Again, they're going to be public. We want some way to get the hour And that is going to return an integer to us We want to be able to set the hour to some Into our that's an integer And that's going to be a void method. It doesn't return anything Similarly, we're going to need a get minute That gives us back whatever is inside of This variable this attribute And we'd like to be able to set the minute to some new value And we're going to need a getter and setter for the second by convention Accessors always have the word get and then the name of the field Mutators have set And the name of the field This by the way is the most mechanical and most boring part of having to develop a class is writing the getters and setters It turns out that some integrated development environments like visual studio code And I think IntelliJ might do it and maybe Eclipse and NetBeans They have an option where you specify your um attributes And then you do some menu item and it generates all the getters and setters for you so that you don't have to but in this case We have to so Here comes the fun part We're going to have a public Integer method called get hour And it's going to return this dot hour It's okay for me to be able to do this even though this is private private means It only is accessible within this class Okay, no problem get hour is inside this class and that means it can look at this private attribute Test time objects is not inside the time class. It uses it, but it's not inside it And that's why it can't do anything with this Okay, and then we're going to have a public void set hour To some new hour and that means we're going to set this dot hour becomes hour We're going to have public Int get minute Which returns the minute of this object And by the way that means I've got to add something to the notes in other methods the keyword this signifies The object we are currently working with Okay, meanwhile back at El Rancho We have our public void set minute By the way, do you notice that I'm putting a blank line between all of my methods here instead of just smooching them all together Just makes it more readable and we're going to have to say this dot minute becomes minute And finally we have public void public double excuse me get second Okay, boy, that was fun And let's compile that to make sure that I've typed everything correctly. Lovely And now what I can do is I can say get hour Plus default time dot get minute And in fact, this is too long of a line by the way Now there's no problem with accessing private variables I still can't access the actual attribute, but I can certainly call get hour because I said it's public Get minute is public get second is public and they will take care of handing me back The information that I want by returning those attributes Now let's come back here compile this Hooray no compile errors and when I run it Well, I get zero zero zero point zero, which doesn't help much. Okay, and I guess I'm going to have to do the same thing for t time You know by the way Hold on a second here. Just hold on to hold on a cotton picking minute here. Let's run that again Notice I got zero zero And I really want two digits for the hour two digits for the minute and then the seconds So let's use print f instead What's our format going to look like? It's going to be percent sign zero two d. This is something that I might not have talked about before I say I want two places Let me put a new line in here so that doesn't get confused I want an integer That takes up two digits and I want it padded with zeros if it's less than two digits Then I'm going to have another point zero two d and then percent sign Zero I don't I'm not sure how this is going to work That's a point two f there we go and then we'll do our get hour Get minute and get second. Let's compile that Am I going to run it? There that looks a lot better Now we can do the same thing here for our t time And there we get 10 o two and four seconds I want to just try something real quick here. I'm not sure if this is going to work But let's say I say I want five Padded with two to the right of the decimal. Let's see if that does what I want Yes, this is much better Now here's the problem that I come up with gee. Well, yeah, this is a bit ugly. Don't you think? um Now because this is now the correct formatting now, I've got to go back and fix this one to make it work Yuck Gee, wouldn't it be nice if I could say System dot out dot print line of default time And have it do all this work for me instead of me having to do it every single time I wanted to print it out so rather than getting the reference I'd really rather Let the time class do all the work We can do that What we're going to do is we are going to add one more method here And it's going to be called public To string and it's going to give us back a string object When we put in this method here Then anytime that we need to Anytime java needs to present the value as a string it will call that method instead of the default method Which gave us the reference which wasn't very useful So here is our public string to string Now we need to print to put it back give back a string and we don't want to print And use print f so how are we going to do that and the answer is there's a very nice Method called string dot format it works exactly the same as system dot out dot print f But instead of printing to the screen It gives you back a string With the same thing that would have been printed So let me grab this one here Which is exactly what I wanted And I'm going to put this here and I'm going to say String result becomes string dot format And that has to be a capital s by the way Here's my format string And now because I'm inside of my class. I don't need to use the getters and setters. I can say our minute Second and then I can return that result I want to keep this here by the way. Let me save this as a copy So this is what I had to do before I had to string So that's why I needed to write this Get our Get minute Etc I could have made it into a method by the way So I didn't have to duplicate the code, but this is going to be better Now that I have my two string method here That gives me back a string that's nicely formatted. I don't have to do all of this stuff here I can say system dot print line of default time and t time and this will explicitly call the two string method and voila There I have it. Um, oh since I'm returning a string by the way I guess I really don't need the percent sign n anymore. Do I? In fact, whenever you have a two string method, you should probably never return a new line at the end of it. You'd have just a plain old string And if some of the user wants a new line, that's on them to provide it. Don't you provide it for them? Let's put that in the notes New line of the end That's for the user to decide whether they want a new line or not And let them take care of it. Okay Let's um Recompile this and now I should be able to rerun this And lovely that works like a chain Okay, I said earlier that I made them private for a reason Now I'm going to tell you what that reason is What would happen if somebody said time weird time is a new time of negative 500 for the hour 3422 for the second a minute rather and 256.445 seconds How about putting a space there and it dutifully goes and gives you this totally weird and totally wrong output Well, that's our classic garbage in garbage out Gee, wouldn't it be nice if we could have some control over it? That's why we made them private because that gives us more control what I can do here is When you are going when I'm going to get the hour I'll take the absolute value of the hour So I don't care if you give me a negative number And I'll take it mod 24 That ensures the hour is in the range zero to 23 Similarly here for the minute You give me that and I'll take it mod 60 And so that ensures the range zero to 59 The second also Mod 60 and now let's compile that Notice by the way for the person who is using our code. They don't have to change anything But behind the scenes I'm making sure that if they give us bad data I'm cleaning it up in is somewhat reasonable manner And now my weird time is translated into something that's at least useful If they weren't private Then somebody could create a time. In fact, they could have said for example t time dot hour Oh, hmm, wait a minute Well is 500. Okay But again, and they can't do it because it's private Ah, but you're saying well, what happens if I say t time dot set hour to 500 Now am I not in the same position? And in fact, it's not out and the answer is yeah, I'm still in a bad place but again Because the only way they can set the hour is by calling our method I now can reassert control and say, oh no, I'm not going to let you get away with that buster I'm going to say When you give me an hour, I'm going to do math dot absolute value of the hour mod 24 There that'll fix your wagon And if you want to set the minute, I'm going to make sure that you I'm working with a positive number in the correct range And when you want to set a second, I want it to be in the correct range Also mod 60. Now when I run this hour again is in a reasonable range Oh, let's set this to something like of 127. So I don't get somebody who looks the same Oh, yeah, and this is what is called data hiding this private Since the only access Is mediated by the getters and setters I can enforce Limitations on the values of my attributes Thank you Do I want to talk about this? Yes My nose is that's here. This is this dot minute and this dot second I had to Duplicate the code that's here It turns out that you can call getters and setters from within A constructor I'm going to keep this here because I want you to see what the old way is Not the old way. This is one way to do it. There's nothing wrong with it. It works exactly as advertised because constructors Can call other methods in the class? We can use them to set the attributes Without having to duplicate code So I can say set the hour to whatever hour you gave me Set the minute to whatever minute you gave me and set the second And then those will take care of the absolute value and the mod to make sure everything is in range Which way should you do it? I don't really care if you want to save yourself a little bit of trouble especially Okay, if this set hour if it was not a one line thing Let's say where there was some immensely complicated formula that we had to do to make sure that the hour was in range In that case, it would probably be better to do it this way. So I wouldn't have to duplicate the code because the amount of manipulation that we have to do is a one liner Duplicating the code here is not a big deal back here and run it And again, everything works exactly the same as it did before Okay, let's go back to the Book here and see what they're doing Okay. Oh, I forgot to do the equals method Okay, you know what? I think this is enough for one mini lecture. So I'm going to continue with the equals method And adding times That will be the subject of our next mini lecture