 Good afternoon. It's time to move on to chapter 10. And we're going to be talking about mutable objects and we're going to be talking about some of the objects that are provided in libraries by the Java system. And the first one we're going to talk about is point objects. We're going to have objects for representing points. Now, why do we want that to be an object? Let's say, and I'm going to have to open this up here. I'm going to call this template. We'll call this points without objects dot Java. So instead of using the one that's built into the point class. Let's say I wanted to represent two different points on the coordinate plane. And I just want integers for their coordinates. So I could have integer x one become let's say three integer y one becomes four. X two is seven and y two is let's say 12. Now, if I want to get the distance between those two, I could say, and I'm going to make this a double distance. I could say that's going to be the square root of x one minus x two. That's one minus x two plus y one minus y two times y one minus y two. And then I could say system dot print line of. Let's just see if I've got that typed properly. And we can run that. And that's what we'd come out with as our distance. And let's say I wanted to write a method where I wanted to find the midpoint between x one y one and x two y two. Well, and at this point I would have sort of a problem. The reason is because let's say I want public static. I'm going to put a question mark here and I want to find the midpoint. And I'd give it an x one y one. Why to actually why one x two and why two. And then I would say, since we're going to be rounding off these coordinates, I could say mid x is going to be x one plus x two divided by two. And the midpoint why is. Why one plus y two divided by two. And now what do I return. I'm stuck. The reason I'm stuck here is because I can only return one thing. Now I could get around it I could use something really really ugly. I can say okay let's return an array of integers. And then I could say integer result becomes new integer to and I can say results of zero is middle point x and result one is mid why. And then I could return result. So when I treat the x and y coordinates of a point as individual variables. I rendered the ugliness like having to return to values. I need to create an array to do it. So I'd have to do something like this integer mid becomes midpoint of x one y one x two y two. And then I would say, so this is what I'm doing here. I'm having actually trouble writing it because it looks just so ugly to me. I mean it works but he cats there must be a better way. The problem here is that the x and y coordinates really belong together as a unit. And that's what an object that's one of the things that an object lets us do it lets us take different pieces of data that are related to one another, and combine them into one nice little unit that we can work with together instead of separately. So when I import the point class from the abstract windowing toolkit, and that's a Java graphics package. And I'm not going to go into any detail on now. What I can now do is I say I want to build a new point object that contains both the three and the four package together and put that into a variable called first point. I can access the attributes I can say first point dot x and first point dot y. And then I can store those in separate variables, and I can print them out. And in fact, if I do that. Now that doesn't seem too exciting but we're going to get to the exciting point in a while here. In fact, listen, they called it blank here by the way in the book why the heck they called it blank that doesn't make any sense to me I think it's a meaningless variable name. And this is why I decided to use something slightly more meaningful, namely first point. Let's see what this looks like. What point is is an object now, or basically blank or what I not called first point refers to a point object that contains both an x attribute and a y attribute. The parts that the variables that make up the point object are called attributes or properties. So that's worth a note, certainly. And I've seen all three of these being used, and depending on what language you're using they will probably use a different term, but they all mean the same things. These are the objects data. So objects attributes are the things that an object has. And in fact, in our example, a point object has an x property. We'll call it attribute. And a y attribute. And now I can use those as a unit. Again, if I want to get there, they call them fields. Okay, there is attributes. So they're calling attributes in the book. Good. That's what I'm going to go with. This dot notation says or go to the object that blank refers to. And then get the value of the attribute x. You can also think of it, if you read from right to left as the x attribute belonging to the object named blank. And then I'm assigning that value to a local variable named x. In this case, there is absolutely no conflict between these. What was I here for example one, instead of calling them x coordinate, I could have called these x and y. And there would have been absolutely no conflict. This is the x that belongs to the first point object. And this is the y that belongs to the first point object. And this is a plain old variable called x and a plain old variable called y that has nothing to do with the object. So the fact that I'm using this dot notation, there's no ambiguity, there's no conflict between these. But I decided to use different names just to be contrary wise. And in fact, now we can pass objects as parameters in the usual way. Here we can now do our distance much more nicely here. What we can do is we can say public static double distance from point P one to point P two. And that's going to be math dot square root of P one dot x minus P two dot x plus point ones why coordinate minus point twos why coordinate squared, and we can return that result. So in fact, let's do this let's have our point second point be a new point seven comma 12. And then I can say, double distance between points is the distance between first point. And second point on the distances. Oh, dear me what did I do. I forgot a closing curly brace that's what I forgot. And by the way that's distances exactly the same as I had before that happens to be a distance method already, but I decided to write my own here just to show you that it could be done. Also, it turns out I did not need to have this print f to do this. Let me go into, hold on let me pause the recording for a second here to set something up. So if I import Java dot a w t dot point. I can say here point P one becomes a new point. Three comma four is this x and y coordinates point P two becomes new point. And then I can say P one dot distance to P two. And that will give me that and you'll notice that but to print it out here. When I printed out it automatically formats it very nicely for me. I guess I should do that in here. Let's do this and save this as point example to that Java. So instead of doing this business here. I'll say first point at. Let's do print Ellen. Plus first point. And then here my double distance between points is going to be first point dot distance to second point. That's another thing by the way that I can now do with objects so I've got the properties. And along with the attributes. An object contains methods. Methods are the things that an object does. And in our example. We can tell a point P one to give us its distance from some other point P two. So what I'm doing here I'm saying excuse me go to the object that first point refers to call its distance method and give it the second point to work with. And that will return the distance between the points. And if I compile that. And rename it properly. Notice here it prints out the. It's nicely formatted but at least it gives us the information about what's in there as opposed to a raise which certainly did not. Now you'll notice here that I have this distance. And that is a method belonging to the object. In the same way that when I had X and Y here. There was no conflict. There's no conflict between this distance method that belongs to first point. The distance method that belongs to my point example to program. But to be clear about what's going on I'm going to get rid of it. That will be absolutely unambiguous and guarantee since it's no longer there. I'm not going to be calling it. And the program will still work objects as return values. You know I'm going to go with this in a moment, but let's first go and get right that midpoint again. So here was my points without objects, and I had to do this really ugly crap to get my midpoint to work. But now I can do it much more needly with this. I'm going to say. Give me the midpoint, which is going to be another point. And I'm going to give it two points point P one. And point P two, which gives me P one dot X plus P two dot X divided by two. And point ones Y coordinate plus point twos Y coordinate divide that by two and that's my midpoint X and midpoint Y. And then I can return a new point. Whose X coordinate is mid X and whose Y coordinate is mid Y. And in fact now I can say double center is going to be midpoint of first point and second point. And now things look a lot better I don't have to fiddle around with arrays or do any weird stuff. Because the point contains both the X and the Y by using an object I can return both those values at once they both belong to the point object that I've created here. By the way I forgot to put the purpose of the program here is show how to access and print point objects show how to pass them to methods and return them from methods. I'm going to description on this one. No I did not. Shame on me have a bad person was here in point example one that represents a Cartesian point with X and Y attributes for the object. There we go that's much better. Here is how I pass objects to a method and how I can return an object from the method. And they did the object as a return value they did it with a rectangle where rectangles have an X Y width and height attribute. Okay let's do that example here it couldn't hurt. So let's open up our template file again. This time I'm going to say what my purpose is create a rectangle object and show its attributes and show that the object is mutable the attributes can be changed. I guess I'd better import that hadn't I. So I can say I want a new rectangle called box and it's going to be a new rectangle. And what do they do in the book there what they call 00 100 200. So it's at the origin. That's 100 units wide and 200 units tall. And when we print it. It gives us all the information we need to know what class it is and what the attributes are the X Y width and height. Now saying that's mutable remember that when we were back in with our strings strings were immutable. So I could not do strings stringing goals. However, with most objects they are mutable. So in this case for example I could say box dot X becomes box dot X plus five. And I could move the boxes Y coordinate by adding 10. And let's say I wanted to add with. I want to add 25 units to that. And then I print that. I can do this. The X and Y coordinates have changed and so has the width. I left the height unchanged. So not only can I use the dot notation to access one of the attributes. I can also use the dot notation to change one of the attributes to some new value. And in fact here what they did was they said let's give it a rectangle and return a point. And what they did there was finding the center of the rectangle and I'll let you look at the code there and figure out that it is indeed correct. And I've just shown you that rectangles are mutable. So if I wanted to be able to move a rectangle by a certain amount. I could say move it a DX for Delta X and Delta Y. Let's pop that in here. And then I could say let's move this rectangle again. And we're going to move the box. And we're going to move it let's say 20 by the in the X direction and 50 in the Y direction. Oh let's make it negative 50 in the Y direction. Sure why not. So this is the coordinate plane. We can have negative coordinates. Voila. So what happens is that what happens five plus 20 is 25 10 minus 50 is negative 40. Yes back to aliasing. When you sign an object of variable you are assigning a reference. So when I have a rectangle box one 00 100 200 and I say box to becomes box to one. I'm not copying the data. I have two references that refer to the same area of memory. And this is really important to understand just like when I had an array when I assigned one array to another didn't copy all the data. All I got was a copy of the reference. And because I'm doing this that also means that when I have something passed into a method when I change one, I change the other one. Okay, I believe grow is built into box. Let me just check something here. Okay, well, we'll find out in a moment. So let's go and look at this here. Yeah. So now if I say box to dot grow. Let's say how did they put it in here. And now I print out box one box one should also have changed. And if I were to look enough notice that the X coordinates have changed. And the width and height have changed from 100 to 200 and the height has changed from 200 to 300. Now, how do I know what grow is supposed to do the answer is, turns out it's built in. And if I were to look for Java dot a WT dot rectangle again this is a really great site to be able to look at it's called the Java API, the application interface. We can always look up any class like rectangle here. And we can see what all of its methods are. And if I say grow resizes the rectangle both horizontally and vertically let me make that a little bit larger she can read it. So it modifies that it's so it's eight units larger on the left and right and V units larger at both the top and the bottom. And we'll also change the upper left corner, and the within height even give a negative value for age and V the size of the rectangle decreases. And that's exactly what grow did here, I'm sorry, I'm, I'm, I'm. I'm I'm I'm I'm I've slipped a gear this afternoon because it looks as if I'm forgetting to write my plan first before I start writing. That's probably because I read the book and so okay there's my plan, but still that's not a good idea. This program show that when you assign an object to another object, you get a copy of the reference, not a copy of all the data. That means if you change the attributes for one reference, they get changed for the other reference. Because they're both referring to the exact same place in memory. Yes. This is sort of interesting. If you ever want to find out how these things are written, you can actually get a hold of the library source. You can see how Java dot awt dot rectangle was actually written, or how string and scanner work. Now they're going to be using a lot of constructs that you haven't learned yet. But it's sometimes sort of interesting to look at them. In fact. So we do that. Sure, why not let's open point dot job. If I can figure out where it is. See, I think I had a commuter science 75. Where did I put that. I know I unpacked it earlier here. JDK source. It's in Java dot base probably. Let's see here. Okay, I'm going to cheat. Excuse me. Excuse me while I cheat and figure out where it is easily. It's in Java desktop Java awt. Okay, thank you. So Java desktop Java awt. And here is my dot Java. All the documentation for it is there. And you'll notice it has an integer x and an integer y. And then here is how we build a point. And I'm not going to explain that because that's part of chapter 11, somewhere. But here, for example, is how do I move something to specific point, or how do I translate it with a delta x and delta y. And they do pretty much the same thing that we were doing. But this is the internal representation of a point. So if you ever wonder, gee, I wonder how Java implements some stuff, you can actually look it up. It's all there. It's open source. So how cool is that. Now, one thing that's incredibly important to know is, we can document things what the attributes are and what all the methods are in this big long business here. Let's go back to point. This is point point again. And here where we have the fields and what they are and the constructors and all this other. Resmet has turns out you can also draw diagrams that condense a lot of that information so you can see it at a glance. And this is something called unified modeling language UML. And so here are the class diagrams for the point and rectangle class the UML diagram always has the name of the class at the top. And then it has the attributes. So the attributes of a point are it's x and y now notice this is totally backwards from the way that we do things in Java. Remember in Java we have to do things like integer x and integer y. Whereas in UML, we say x is an integer and y is an integer. So in this case, the field name comes first and the data type comes afterwards. So something to be aware of. What does plus mean the plus means that is publicly available. That means that the word public has been defined when we define the attribute again in chapter 11 we're going to see this when we define our own objects. There's the rectangle which has the x y width and height attributes all of which are integers. Then we underneath the attributes we have the methods, we have a method for building a point. That's called a constructor and the two string method for rectangle we had to string we had to grow and translate, which means move the rectangle from one place to another. Again you'll notice that the variable name comes first, then comes the data type and the return type comes at the end, not knew the beginning. So in Java, we say void grow of int. Where they call those here. So I got here, whereas UML says grow takes DX which is an integer and DIY which is integer and returns void. So everything just goes in the opposite direction. And that's just the way it is. Plus is public minus identify identifies private and we'll discuss those in chapter 11. This is a really good place to stop right now. What I may do is come back in later today, or tomorrow morning and do the rest of chapter 10 so that tomorrow afternoon or tomorrow evening rather, I can pick up and start on chapter 11 I need to get a little bit ahead of the game as I was saying earlier this week. I'll give you something to think about for now, and I will see you at the next mini lecture whenever that happens to be.