 Hi everyone. In this video, we are going to create a project to explain how to compare two objects and name this compare to project. For the purpose of this example, we are going to create a very simple class, which is our favorite rectangle class. And all that it contains are instance variables width and height. We create one constructor and here width becomes math dot max 0 comma w. So we're going to perform some validation just to make sure that width and height are not assigned invalid values. And we will also implement our two string function so that when the object is displayed, this function is called automatically, this method is called automatically. And this returns is whatever the width is by whatever the height is. Let's add one more method, which is the diagonal of this rectangle on which this method is called. So we don't have to pass the object as a parameter. We're going to call this method on an object. And what we return is map dot square root square of width plus square of height. That's the Pythagoras theorem talking. We're going to create a client rectangle. So we can call it rectangle client containing public static void main. And we're going to create two objects here rectangle. R equals new rectangle. Let's say two by three. And another rectangle s, which is four by one. Let's quickly change the definition of the two string and say area equals the area. Oh, area function. Okay, so we have to add another method area, which returns width times. All right, go back to rectangle client and we create these rectangles and we display objects r and s. Now this program you can see in the console that we get two by three, area six, four by one, areas four. So what we're going to do is we won't compare which one is bigger, whether it's r or it's s. It's for this purpose that we create the method compared to inside a function. So compared to is nearly a special method. Obviously we're going to call compared to on a rectangle object and we call that object the calling object. So here what we already know is compared to will be called on a rectangle object. We call this the calling object and it's referred to by a very special keyword this inside the method. In short, we say that this is a reference copy of the calling object. So if I call the method on object r, r is my calling object and this will refer to the same instance that r refers to. If I call it an s, this will refer to the same instance that s refers to. But we are also going to pass another rectangle object. So whichever object we pass as parameter to compare to will be reference copied into other, which means other is a reference copy of the actual parameter. Alright, so that's all you need to know in terms of compared to and what is compared to do. Let's say return. What do we return? We return one if calling object is more than parameter object. We return minus one if calling object is less than parameter object and we return zero if calling object is equal to parameter object. But how do we define more than less than equal to so we have to specify the comparison criterion or comparison criteria. In this case, I'm going to use area. So the area of calling object which is copied into this is more than the area of other object. Then we return one. So what I'm going to do is because the areas are also integers. I'm going to call them as a one. You don't have to do that. You can compare directly this dot area with other dot area, but I'll make intermediate copies and keep them in a one and a two. And after that, it's pretty smooth sailing. Once we know what's the calling object and parameter object. Once we copy the area, all we have to do is compare the areas if even is more than a two, it means calling object has a bigger area return one. It's guaranteed if you reach line number 47 that even is either less than or equal to a two. So we check is even less than a two and return minus one if that's the case. If we reach line number 51, it's guaranteed that even must be equal to a two otherwise we wouldn't be here and we can return zero unconditionally. Please note that if you put if you wrap return zero around a one equals a two, which we know for a fact is true, your method will complain Java will complain that the method must return a result of type int. That's because all three written statements are conditional. And that's why since it's guaranteed that even is equal to a two at line number 52, you can return zero directly. We'll go back to the rectangle client and we'll say int result equals r dot compare to s. Here are is calling object and s is parameter object. So you can as I said earlier, the calling object is referenced copied into this. The parameter object s is referenced copied into other. It doesn't necessarily mean that we have to name it other you can name it whatever you want. Let's open our lecture notes and take a look at conditions functions inside objects. Please do take a look at this example that I've provided here. Here I have R1 compared to R2. And therefore, in the scope of R1 dot compare to R2, this is a reference copy of R1, because the calling object is R1. And other is a reference copy of R2 because the parameter object is R2. And that's your method definition. Very quickly, we're going to run this. And what we're expecting is that if the area of the calling object, which is R, and its area six is more than a two, which is four, it will return one. So when I run this program and display result, I'm expecting one. Oops, not one directly result. Okay, let's run this. And you can see that the outcome is one. On the other hand, if I call the method on object s and par pass R as the parameter, now s has become the calling object and R is the parameter. And now we're expecting the parameter to be the outcome to be minus one. If I pass both s as calling object and parameter, basically this and other refer to the same instance, and our answer would be zero. Okay, let's revert back to R.complete to s, place breakpoints in our function that is expected. And I can guarantee that if you stay with me for the entire duration of debugging, then things will become much, much easier for you to understand. So here I'm going to say this is the result and return result. All right. Let's put breakpoints in the area. I'm going to put breakpoints in area a little bit later. I'm going to debug our program. And you can see in the variables that I have two objects of type rectangle R and s, R has an ID of 21, s has an ID of 22. When I call compare to, I'm calling it on object R. And the parameter is s. In my rectangle class, as I said, the calling object is copied into this and the parameter object is copied into others. What I'm expecting is this will have an ID of 21 and other will have an ID of 22 if what I say is right. So if I resume my execution from line number 11 in rectangle client, I will jump into the compare to method, but I didn't put these debug points. But that's fine. You can see this has an ID of 21, other has an ID of 22. Let's stop the debugging and run it one more time. So debug. This time, R has an ID of 22, s has an ID of 24. If I resume, you can see this has an ID of 22 and other has an ID of 24. So this is a reference copy of R, other is a reference copy of s. A1 is this dot area. Now this is three by two. So we are expecting A1 to be six. And A2 is other dot areas. You're calling the method area on object other, which is one by four. So the answer should be four, which is correct. And the checks is six more than four. Yes, it is. And we enter the conditional block and it returns one, which is what we described. On the other hand, if I swap these around and change the calling object to s and debug my program once again, s has an ID of 23 and R has an ID of 21. So when I go into the area method, I'm expecting this to have an ID of 23, other to have an ID of 21. So that's exactly what's going to happen. This has 23, other has 21. Then we are calling the method area on object this, which is one by four. So it's area should be four, A1 will be four, and A2 will be other dot area. So other is three by two, its area would be six, and A2 is six. This time, it will skip the first conditional block. But it does go to the second conditional block and it returns negative one. And result becomes negative one. And that's our answer. As I said earlier, I didn't put breakpoints in the area method. Let's put breakpoints in the area method as well. And debug our program once again. Let's stop the debugging and remove or terminate it, debug the program once again. I think I made some mistake. Let's see. Let's go here. It's because of this. Because when I displayed R and S, two string was called and two string was calling area. That's why I'm going there. So let's get rid of that. Debug our program. Result is S dot compare to R. Note S has an ID of 23. R has an ID of 21. So in the compare to method, because S is the calling object, this has an ID of 23. And other has an ID of 21. And now this, what I'm going to say is the key. Next, what we're doing is we're calling the method area on object, this, which has an ID of 23. So when I jump into the area method, again, in the scope of area, this reference is created now. That this reference is different to this reference from compare to anyways. So here, this is 23. So when I call this dot area, it jumps into area. And the this reference in area will also have ID 23. So when I resume, you can see we are inside area and this has an ID of 23. Okay, result is for and it returns it to compare to then we call other dot area. Other has an ID of 21. And it's the calling object for area. And because it's the calling object for area, inside the method area, this will be a reference copy of other. So when I resume this, inside area, there'll be a this reference, but it's ID will be 21. And there you go. This has an ID of 21. And it's area six return. And everything else is the same. So that's an A to Z explanation of the compare to method. Well, almost instead of comparison criteria criterion, it can be comparison criteria. So area and diagonal. So basically, if areas are clearly different written one or minus one, but if they're the same, then check the diagonal. So here, we start checking the diagonal. So we say double D one equals this dot diagonal, double D two is other dot diagonal, and the same thing again. By the way, there are easier way to do this, but obviously in Compt 1010, we expect students to do everything from scratch for better or worse, return minus one one and zero unconditionally. We're going to change our rectangle client a little bit. I'm going to name have this as three by four, and five by one. So here, clearly, our has a bigger area. So it will return one. Let's stop debugging and run it in Java perspective outcome is one that's good. If I call it on S, by passing our S clearly has a smaller area. So it will turn minus one. But if S has the same area is going to go and check the diagonal. So if I say R dot compare to S, it will jump into R dot compare to this is a reference copy of our other is a reference copy of S. It's not even is not more than a two even is not less than a two. Yes, it is equal to a two. So they'll check the diagonals. The diagonal three by four and the diagonal of six by two, it will compare. And those won't be equal and return one or minus one. Now, which one has a bigger diagonal? Let's see three by four. So three square plus four squared is nine plus 1625 while six square plus two squared is 36 plus 440. So S has a bigger diagonal. So S is bigger. So R dot compare to S returns minus one. Let's run this and we get minus one. If we compare S compared to R, we'll get plus one. And if S represents a rectangle of four by three, then the diagonals are also of the same length. In this case, R compared to S or S compared to R will return zero because the diagonals areas are the same and diagonals are the same point being that compared to can have any number of levels of comparison criteria. So we call here areas the primary comparison criteria diagonal is the secondary comparison criteria. So there you go. That is compared to if you follow the instructions, if you follow the specifications, if you understand reference copies clearly, basically, if you're diligent, it's not a problem at all. Thank you very much and hope you have a good day. Take care.