 Hi. Back again and we're going to do one of the exercises in the book. And I've copied and pasted this stuff from the book into the source code and we formatted a little bit so it's a bit easier to read. Hey, we're going to have a class named lens, which represents camera lenses and they have attributes of focal length. The distance from the lens of the center sensor. And the aperture, which is the diameter of a lens opening and these are both usually measured in millimeters. And we're going to put them into private double instance variables. Now I'm not going to start coding right now. What I'm going to start doing is I'm going to start making my UML diagram using this as my guide. And I have my empty template for UML diagram sitting here. So let's call this lens. And we may as well make it monospaced font, not that it matters. So this always goes there. Now let us see again what are those attributes there called focal length and aperture. Okay, well that's our next job is to go here. And again, these are going to be private. So that means we went to minus sign. Focal length is going to be a double and aperture is a private double. Okay, well that takes care of our attributes. And by the way, I can also use this as a guide while I'm writing the code. I can print it out and have it right next to me. Okay, we're going to have a default constructor that sets both attributes to 1.0. This is where our methods go. And so we are going to go here, and we're going to have a public constructor. The name of the constructor matches the name of the class upper and lower case identical. And it has no parameters. And there's no return type from a constructor. But two argument constructors that sets the attributes as specified by the caller. And it'll set the value to 1.0 if the argument is less than zero. So if you give me a negative number, I'll just say, I'm going to use one instead. And again, this is going to be public and it's going to take a focal length, which is a double and aperture, which is a double. Okay, it looks like I need to do something here. Let me move this over a little bit. It's still going to be a little bit too big here. So tell you what. Let's move our font size down to about 14 point. It's going to be a little bit more difficult to read, but at least it'll all fit. Getters and setters for both attributes. Again, the mutator. And now the question is a mutator, a getter or a setter? I'll give you a couple of seconds to think about it. This next word sort of gives away the show because if it sets a value, guess what? A mutator is another word for a setter. Okay, lovely. So that means we're going to have, and I usually put a blank line there just to give myself a little bit of space. You know what? Let's see if I can move this up to 16 point. Okay, there. Good compromise. So we're going to have a public get focal length, which takes no parameters but returns a double. We'll then have set focal length, which will take a new focal length, which is a double. And it is void. Then we're going to need to get aperture, which returns a double and set aperture, which takes one. Which is a double. And it's a void method. Write an instance method named calc f stop that returns the lenses f stop by dividing the focal length by the aperture. Okay, that means we're going to have to have a public method called calc f stop. It doesn't take any parameters. Why not because it already has the focal length and the aperture available to it as the attributes in the object. So the object has attributes and methods all packaged together. And it's going to return a double provide a two string method to display the focal length and aperture properly labeled with one digit to the right of the decimal point. That means we're going to have a two string method, which returns a string. And again, we don't have to provide any parameters here because we have access to them here. We have access to the values we want. And finally, we want an equals instance method that takes another lens object as its parameter and returns true if they're the same focal length and aperture false otherwise. Okay. And that's going to be equals. And it's going to take a lens called the other lens. And it is going to return a Boolean. And now it turns out when we write this remember that we have to make sure that because there's round off error and all that kind of stuff. We'd like to make sure that things are the doubles are close enough to be considered equal. So we're going to need to do it for the focal length and we're also going to need it for the aperture. So let's write a method and this one can be private by the way, called. Considered equal. And we'll give it the first number, which is a double. And the second, let's call it a and B. And it'll return a Boolean to us. Now, this is not going to be an instance method. We need an instance method for the getters and the setters. Why because every lens has a different focal length and aperture potentially. So we have to stop, we have to work with the objects attributes. The instance that we have available to us, same with two string and equals we're comparing our current object to the other object. But when we want to see if two doubles are considered equal, we don't even need an instance of a lens to be able to do that. And in this case, we're going to make that a static method. So here we say something is static is we underline it. So that tells us going to be a private static method. Why private, because nobody else is going to have to call this method. People who are using our lenses are going to have to do all of this, these other things. But they never have to worry about how I'm considering doubles being equal or not. Give me a break. Very good. That and hold on to me to check something real quick. Perfect. Everyone make sure I was saving it under the right file name. And there's our UML diagram. And yeah, let's do this. What the heck. There we go. Save that. And let me export that as a PDF so that you'll have it. When I upload the example files. Okay, now I could print this out and work with it. Next to me. Since I have the description here already. Oh, here comes the fun part. The message window. And let's see tools here split window top and bottom. There we go. Excellent. So I'm going to go up here and in this one here and public class lens. All right. Now I'm going to do things in the exact same order that I had them before. I'm going to have a private double focal length. I'm going to have a private double aperture. Then I'm going to have my default constructor that sets them both to 1.0. I'll have a public constructor call lens. That sets the focal length to 1.0. And the aperture to 1.0. Then I'm going to have my two argument constructors. Public lens. And I'm going to give it a focal length. And I'm going to give it an aperture. I'm going to spell aperture correctly too. And I'm going to say the focal length that belongs to this object that I'm currently constructing. Takes on the value that I passed in as the parameter. And this objects aperture. Becomes the aperture you gave me as a parameter. Let's compile this just to see that I haven't made any. Gigantic mistakes here. And says compilation finished successfully. So I'm a happy boy. And now here I have a lens test program. I'm going to ask the user for two sets of focal length and aperture and create corresponding lens objects. There should be an S here. It'll then display the first lenses attributes and f stop. The second lens is lens is not equal to the first one. It'll also display the second one's attributes and f stop. Otherwise it'll output a message. The two lenses are the same. Okay, cool. And here I have where you're entering the lens lens focal length in millimeters and the aperture for the first. And for the second one. So let's do this. Let's have a lens. Lens one. Becomes new lens. And we're going to give it focal one and aperture one. And lens lens two will become a new lens. With focal two and aperture two. Then what we're going to do is we're going to system the print line. First lens. Plus lens one. And. Second lens. Have a compile this. And let's run it. So if my first one is let's say five and seven. And the next one is three point two and four point six. And the output is less than wonderful because remember, there is the default for printing objects is not particularly useful. Oh, there's something I forgot to do here, by the way. I'm a bad person. Namely. Down here it says. We should set the attribute to one. If the argument is less than zero. Oh, I'm so tempted. Yeah, I'm going to, I'm, I can resist anything except temptation. I was going to write code like this. If focal length is greater than zero. Then this. Focal length becomes focal length. Otherwise this. Focal length becomes one point zero. There is absolutely nothing wrong with that code. However, I can also use my good friend, the ternary operator. Remember the question and colon. To compactify the code. There's no such words compactify, but there should be. I'm going to say this dot focal length. If focal length becomes. If the focal length is greater than zero. Greater than equal. I'm sorry. Well, how you can have a focal length that's equal to zero. I don't know, but. It's unfortunately says here less than zero. Okay. So we'll do what the assignment says and rewrite the assignment next time. So that it makes more sense. So if the focal length is greater than equal to zero. Then we'll use the focal length. Otherwise one point zero. There. That's a lot nicer. It's a little bit harder to read than our good old familiar. If else, but it does exactly the same job. Pop that in there. And we'll do the same thing here. If the aperture is greater than equal to zero. Then we use the aperture that you gave us. Otherwise one point zero. And let's come back here. Let's do this again. And I can say three point five negative two. Four point seven and negative three. And we still don't know if it worked or not. And this is where I sort of part company with my UML diagram. Nobody wrote a law or passed a law that says you have to do these in the exact same order that they're listed in the UML diagram. What I do after I've got my constructors. My attributes written up. I go straight to two string and implement that. That way I can see that everything is set up properly. So here we go. Come back to our lens.java. And we'll have a public string. To string. And what is so supposed to do is supposed to display the focal length and aperture properly labeled. Okay, fine. So let's return string dot format of. Focal length set dot one F. Apert we better put millimeters. Aperture percent dot one F. Millimeters. And we'll fill those in with the focal length of this object. And this current objects aperture. Compile our lens dot java and we come back here. And then when we re-execute it. Let's say three and negative two four and negative five. And excellent. It did set it to one. I am a happy person now. Now that I have this and I can see that things are pretty much working. I can go back here and do my getters and setters. And this is the sort of mechanical part of the whole business. Okay. We have a public. Double get focal length. And that's going to return this dot focal length. And then we're going to have a public void set focal length. Of. The new value that we wanted to have. And I'm going to copy this. Because we have to do the same test. We're going to get our aperture. Which will return this dot aperture. And we want a, oh no. Hello. That's not going to be void. It's going to be double. We're returning something. It can't be void. Okay. Public. This is void set aperture. And we're going to give it a new value. And we're going to copy and paste. This stuff here. Let's compile that. And I may want to write some code to test that, even though it's not part of the assignment. So let's do this real quick. Let's go here and save this as. Lens test to. Java. And this is going to be. Check to see that getters. Setters, et cetera. Do what they are supposed to do. Lens test to. I'm not even going to ask for input on this one. That's just tends to be. So for example, what happens if I do a new lens with nothing in it? Okay. So let's just check to see that this does the right thing. All right. Let's put this one as 3.5 and 2.0. And then let's execute it. 3.5 to one and one. Perfect. And now let's try something here. Let's try. Double focal one becomes. Or focal becomes lens to get focal. And then we're going to say. Lens to dot set the focal set aperture. To let's say 1.4. And then system dot print line of. Focal length. Of lens to. Plus focal. And then we're going to say a system dot print line. Revised lens to. Plus lens to, and we should get an aperture of 1.4. Well, that's exciting. Try giving the right name. And yep, it's set the aperture properly. Let's check something real quick here. Let's do a lens to dot set focal length to negative 49. The revised one I tried to get a negative number and it said to 1.0. I'm. This is going great. So my getters and setters look like they're doing the right thing. Okay. Stop for just a moment here. Let's copy all of this. Taste it in here and save this. There's a reason I'm doing this lens before. I'm putting this here so that if. If I screw things up, I've got a copy and B so you can do a comparison later on. Namely, I want to get rid of this duplicated code here. Remember what I've said before. A constructor can call other methods in the class. This code here is exactly the same as that. So I can replace the duplicated code by saying. Set the focal length. To whatever you gave me. And similarly. Line 37. Is the same as line 53. Okay. Fine. That's the same as saying set the aperture to whatever aperture you gave me. I could have done this right away, but I just wanted to do this in two stages. So you get the idea of it. So one more thing I can do. Here I'm saying the focal length and aperture to one. Constructors can call other constructors. I can say let's call lens with 1.0 and 1.0. So this will take a lens. And call the no argument constructor will call the two argument constructor. And put in a one and a one. Let's compile this. Oh, that's interesting. Oh, what have I done wrong? Oh, because I have to say this. Sorry about that. Doesn't work quite the same as a regular method. Remember I said constructors are special. I wasn't kidding. Constructors are special. Okay. So there we go. That's much better. And if I come here to lens to recompile it, everything will work exactly the same as it did before. Okay. Now I've got my getters and centers and my constructors. And I've optimized them a little bit. So I don't have duplicate code. Now let's go on to calculating the F stop. And what is the method for doing that? Divide the focal length by the aperture. Okay. I think I can handle that. So I've got my getters and centers. And then we're going to have. I'm going to double. Calc F stop. And it will return. This dot focal length. Divided by this dot aperture. Now let's go back here into our test code here. Our original test code. And let's just do the first lens here for the moment. Lens. Remember that we were supposed to be printing out the. The lens and we were also supposed to print out its F stop. So I could say plus F stop. And that's going to be lens one dot. Calc F stop. And let's run it. And so if I have three and 1.4 1.4. And seven and two. So F stop is 2.14. Okay. Well, I'd certainly like to do something a little bit better than that. Wouldn't I? First of all, I need it. Okay. All right. Now. I'm going to do something that I shouldn't do. And say, oh, I've got this and I don't want to touch this. And so what I'm going to do is I'm going to say plus string dot format. And now what I have is a mashup sort of between. Concatenation of strings and string formatting. And let's execute this. So if I have three and 1.4. Two and 1.7. And there's my F stop is 2.1. I've just done this. And the fact that it is a mashup. It's my, it's neither fishing or foul. It's, it's. This bothers. This looks ugly to me. Let me comment it out. I say, if you're going to be using something like formatting use formatting all the way. We're going to say, hello print F. First lens percent S. F stop percent dot 1 F. And then we're going to fill in the blanks with lens one. And we're going to have lens one dot. Calc F stop. So now I don't have all those silly plus signs running around bothering me. Oh, there's something I want to do here real quick. And so if I have three and 2.4. One and zero points for three. Well, okay, cool. It does work. Since it worked for the first lens, that means it's going to work for the second lens also. Copy this. Taste that. But second. And lens 2 and lens 2. By the way, notice I use percent S, which wants a string. Print F knew that it had to convert this to a string, but I could have done it. Explicitly saying convert this to a string and that might, if it makes you feel better about doing it explicitly. Be my guest. No, I do not. Okay. By the way, you'll notice again that what I'm doing is I'm not trying to write this whole program all at once. I'm writing it in sections testing as I go along. So I'm not confronted with a gigantic block of code. And goodness only knows where all the errors are. Okay. Last thing I have to do is. Implement an equals instance method. It takes the other lens object and that signature was. Equals. Okay. So. We're going to have here public Boolean equals. To lens other. So if. This. If we can considered. Don't say consider equal or considered equal. Aperture with other aperture. Then I'm going to return true. Otherwise I'm going to return false. I can't believe I just wrote that, but okay, fine. Now I need that considered equal. This is going to be a static. Private. Let's put a start with it. Let's start with the private first. It's private static. Boolean. Considered equal. Double A with double B. And let's have our. Final. Double. Delta the difference between them. As 0.123. If it's less than 110,000. They're considered equal. And I am going to return. The absolute value of a minus B is less than or equal to. That delta. Now. Notice I've written this in a very different way than I did up here. I could have written it this way. I could have said if the absolute value of a minus B is less. Oh, by the way, absolute values in the math. Class. But I don't have to do that. This expression is a Boolean expression. It already has a value that's either true or false. Look at what I'm saying here. I'm saying, well, if this comes out to be true, return true. If this comes out to be false, return false. Let's cut out the middle man. Return the result. So they're going to be a true or false anyway. Now, if you write something like this, I'm not going to take off points for it. I'm just going to say you could have done this a lot easier. Let's say what I'm going to do. I'm going to keep this here. Just so you can have it for reference. I can return. This Boolean expression. I would have needed another parenthesis there, by the way. And this does exactly the same thing as that. Now let's go here and let's just test this real quick in our lens test two. Let's have a lens called lens three. And that's going to be a new lens of. Four and let's say one. Four and two. And then we're going to have a lens, lens four, and that's going to be a new lens with four and two point one, two, three, four, five. And then I'm going to say, let's compile this first to make sure I don't have any. Compilers. And let's come back here to lens test to compile it. And let's run it. And yes, it's true because they're just within. Range of one another. But if I say lens five becomes new lens of four, two point zero zero. Zero nine. Three and five equal. Let's check to see that lens three dot equals of lens. In fact, let's reverse the order. That's another good test. Does equality work both ways? And this should come back false because it's bigger than point zero, zero, zero, one. So again, I'm happy with my results. Okay, finally, I can complete lens test dot Java. If the second lens is not equal to the first one, it will also display the second lenses after beats an F stop. Otherwise it outputs the message. The two lenses are the same. So that means if lens one dot equals lens to second lens is same as the first. Otherwise. I have to do the calculations for lens two and print those out. Get the right number of parentheses. There we go. And let's run this. So let's say my first vocal length is five with a lens aperture of two. I'm using easy numbers because I'll know if the answer for the F stop is correct. And let's have the second one as five point zero and two point zero. And second lens is same as the first. Okay. Cool. Let's run it again with five and two. And five. And 2.5. Is there something I want to do here with this? Yeah. There's a comma here. But there's no comma before F stop. Now I would absolutely never take off points if you forgot the common. Okay. This is just me trying to be really fussy about my output being consistent about it. And let's say I have now three point four millimeters at zero point seven. And the other one is a three point eight with the. The aperture of zero point four or five. And this is where this is nice. F stops, by the way, do not have units. Because I'm dividing millimeters by millimeters. And F stop is a dimensionless number. So I don't want to put the word millimeter after that just be wrong. And there you have the development of a class lens, which we did by starting off with our UML diagram. Then we implemented the attributes first. That's almost always the first thing you do. Plus our constructors. We then went down to two strings that we could create some lenses and check to see that the output at least was reasonable. We then wrote our getters and setters and wrote some code to test them. Once we had our setters, we were able to optimize the constructor here. And the other constructor. Then we did Calc F stop. And finally we doubt our equals. And gave ourselves a nice static method to help us eliminate duplicate code. And that is today's mini lecture.