 Hi, time for today's mini lecture where we're going to be working with a couple of the exercises in chapter four in the extra exercises book. And let's share the screen. And the first one that I'm going to do is this one with the focal length of a camera lens. Where you have one over the focal length becomes one over the distance from the object you're photographing to the lens and D sub I is the distance from the lens to the image sensor. And I've copied and pasted some of the stuff here from the book into the description at the beginning of the program. And again at the beginning of your program you should have your name and the date and what the purpose of the program is. So someone who hasn't read the book or hasn't read the assignment will be able to understand what the program is expected to do. So I gave all of this information about the formula. And here I'm going to ask for the distance the object in meters and the distance to the image sensor and centimeters and calculate the focal length and millimeters. So let's think about how we're going to solve this problem by hand. So the step one is asked for distance to object in meters, convert that to millimeters, because that's what our final result has to be. The three is we're going to ask for the distance to the image sensor in centimeters. And then we're going to convert that to millimeters also. So now we've got everything in millimeters and we're happy. And now we want to calculate the reciprocal. So let's name some of these here. Okay, let's call this object distance millimeters. And then we'll call this one object. Excuse me. This is going to be image distance millimeters. Why didn't I start with out with the word distance. The answer is because I want the difference between them to be as clear as possible. If they both start off with this, for example, distance in millimeters to the object and distance in millimeters to the image sensor, these things look almost alike and I have to go all the way to the end to see which one is different. Whereas here I know immediately which one's the object and which one's the image. So I want the reciprocal of the object distance in millimeters, plus the reciprocal of the object image distance in millimeters. That gives us one over F. We then have to take the reciprocal of that. And that gives us F. Now because I'm doing the reciprocal so many times I want to have a method that calculates the reciprocal. But it is only a division by one I need to practice with methods because that's the whole purpose of this chapter. So let's do that. And let's write that one immediately. So here we're going to do this we're going to say public static double. And we're going to call it reciprocal. And we're going to give it some double number x. Now normally I say you have to have meaningful names. The problem is that the reciprocal of a number it could be the reciprocal of anything. The x here is truly a generic variable. And that means I don't want to give it a specific name, which would tie it down to how I think it should be used. And this one is a one liner, I can return 1.0 divided by x. Now what happens if somebody gives me a zero in there. Well then what's going to happen is I'm going to get some weird results and I may as well show you what happens here. Let's go into J shell. Let's say I have an integer in and I said 12, and I try to say and divided by zero when I divide by zero with integers. I get an exception division by zero. Now let's do this double x is 0.0 and I say what's one divided by x. And turns out it gives me a special value called infinity. And once I have infinity, I can't do much anything, anything much else with it, but the program doesn't blow out. So what are we going to do if somebody gives us a zero as one of these numbers. The answer is, we're going to give them back an infinity and it's on them. Now, the next thing I want to do here is I want to say let's do a public static double, and this is going to be calculate the focal length. Now it's going to need the distance to the object and the distance to the image. And the question is, do we want them to be in millimeters or do we want them to be in meters and centimeters. This is a design decision. The arguments to this method will be the distance from the lens to the object in meters and the distance from the lens to the image sensor in centimeters. I'm going to take the user's input and I'll show you in a moment why why I'm doing this. I have a reason for doing this. So we're going to have a double. Our first one is going to be called, let's say object distance meters. And the next one is going to be image distance in centimeters. Now, just to make things very clear of what's happening, I'm going to have a static that don't need to say static in here. Hello. I'm going to have a final double, which is going to be millimeters per meter, which is going to be 1000. And I'll have a centimeters, millimeters, excuse me, per centimeter, which is 10. And if I want to be absolutely explicit, I can put a point zero there. Now I'm going to do all these steps that I had here. And what I'm going to say is I'm going to say double object distance in millimeters is going to be the object distance in meters times the millimeters per meter. My object, my image distance in millimeters is going to be the image distance in centimeters times the millimeters per centimeter. Okay, I'm going to call it one over F, which may not be the best possible name, but that's going to be reciprocal of object distance in millimeters. Plus the reciprocal of the image distance in millimeters. Then finally, my focal length is going to be the reciprocal of one over F. And that's a bit more simply by doing the divisions directly, but I want to show that one method here call that mainly calc focal length can call many other methods. And then since I have to return a value, I'm going to now return focal length. Now I do the rest of the code in Maine where I'm going to do the asking of questions. So I enter distance from lens to object in meters, and I'm going to have a double and I'm going to call it put dot next double, enter distance from lens image sensor in centimeters. And this will be the double. Well, okay, I'm sorry object distance meters. This is going to be called image distance centimeters. Again, these names don't have to match. And that's going to be input next double. And then I'll say, okay, my focal length will be calc focal length. And the numbers I'm going to give it are the object distance in meters. And then I can print out my results. The focal length of your lens is percent dot three F millimeters. Now, you'll notice that here, when I did this I said convert them that to millimeters and convert to millimeters also and I did that while I was doing the after I did the asking. I don't want to do that that splits the calculation of the focal length between this method and your input. Essentially, if I were to have done this if I were to have said double object. Distance millimeters becomes object distance meters. And then I would have been doing essentially part of the conversion process. Excuse me, there's a plane flying overhead. Let me pause for a moment here. And this is a very common issue that comes up. And methods should be self contained. They should do all the work. They should not a lot have the work of a calculation being split between the main method and the method that actually does the result you want. That means I really should change my steps here. In order to make it reflect what's going on in my program. I'm going to ask for this. And then step three is going to be call a method to calculate the focal length by doing this. I'm going to have to redo all my numbers here. No, yeah, that's okay. Let's call this step a is going to be converted. Step B is going to be convert distance to image sensor. And now step C is to calculate that. And so now these steps more accurately reflect what I'm doing in my code. Let's compile this and see how many miss types I have. Well, that's nice. Oh, don't tell me I, I call it lens calculations. Okay, so let's call it lens calculations. Easier than saving it with a new file name. Okay, now I can file it successfully. So let's say I have something that's four meters away. And the distance to the image sensor is 1.5 centimeters. And the focal length of my lens and millimeters. You'll notice that I did a whole bunch of stuff here to get the recipe, the focal length. And split into three lines. There's a way to do it as it in one line. Let's save this as lens calculations too. And I'm doing this again so that you can download when you download this, you can see how my programs have evolved. 1 over F is assigned this whole business here, correct? So that means really when I want 1 over F, I could do this. And since I'm returning focal length, which is this, I can copy and paste this whole business and do it as one line. I don't need to semicolons there, by the way, that was copy and paste gone wild. Now you may not like this, you may think, okay, that's a little bit too difficult. By the way, it's still going to give me the same answers here. Let's compile this and run it. I think I had four and I said 1.5 or 2.5, I don't remember. I guess it's two and a half centimeters. Okay, no, it's one and a half. Yeah, let's try that again here. Four years and one and a half centimeters. Yeah, and they get the same answer. So as you become more familiar with Java and programming, you might decide, okay, I want to have these nested function calls. I have a reciprocal call inside of another reciprocal call. You don't want to go overboard with this, don't make something way too complicated. And sometimes it is actually better if you split things up. But if you don't like doing it, there is no problem with doing your calculation stages. Now there would be going overboard. Okay, let's do this. Let's save this under another name. And this is more of a philosophical thing that I'm talking about here. This is the end calculation 3. So I might have here. Double precip distance millimeter is equal to reciprocal of object distance millimeter. And then I have double recipe image millimeter is the reciprocal of image distance and millimeters. have double sum becomes Recip distance millimeter plus Recip image millimeter. Actually that's one over F, isn't it? At this point you've sort of lost the thread of what's really going on. It's split apart too badly or too much. In the original, it's a reasonable blend. I'm doing these additions here and they belong together. Then I'm taking that reciprocal afterwards. That seems like a reasonable thing to do. In the second one, I might be complexifying it too much. In this one, I am definitely splitting the things into subparts way too much, and I'm losing readability as a result. There's your philosophy lecture for the day. Do we have time for one more? Let's take a look at these and see what we can do. I'll always start one of these. I like this last one here. If I have a rectangular prism, what I would like to do if I've got the length, width, and height, and I want to return or be able to calculate the volume, the surface area, and the diagonal. That means I have to know how to figure those out by hand. The volume is going to be the width times the length times the height. That's a fairly standard formula. What's the surface area going to be? Well, we're going to have the width times the height. That's this end piece, and I have two of them. I've got the length times the height, which there's going to be two of those. Then there's going to be the base, which is the width times the length. I'm going to have a top and bottom, and that means I'm going to have two of those. Let me start writing this stuff down here. Let's open up my template file, and we're going to call this rectangular prism.java, Monday's the 14th and not the 13th. The purpose of the program here, given the length, width, and height of a rectangular prism box, calculate the volume, surface area, and diagonal in separate methods to get practice with methods, because after all, that's the whole point of this. Let's write down what the formulas are. Volume, that's going to be the length times the width times the height. Now, the surface area, I'm going to have to have the length times the width times two plus the length times the height times two, plus the width times the height times two. But I can use a little bit of algebra to simplify that into two times length times width plus length times height plus width times height. In other words, I'm factoring out the two and I'm putting it at the beginning. Now, what about the diagonal? Oh, crikey, that's going to be a fun one. Okay, let's take a look at the picture. When I was a kid, my parents would always say, do we have to draw you a diagram? And the answer is, yes, if you draw me a diagram, that really helps sometimes. Okay, if we were to look at this, here's the diagonal that goes from the lower front to the upper back of the box. You'll notice that we have a right triangle here. This is the hypotenuse. This is the height of the triangle and the base of the triangle is going to be the diagonal of the base of the box. If I were to draw the base of the box, on the base of the box, it's diagonal. I would have that plus this plus that. So what I need to do is, I'm going to calculate the diagonal of the box-based. And let's go back and look at the width and length. And then I need to calculate the hypotenuse. And let's call this box-based diagonal. I need to name things, otherwise I get confused. And then we're going to have to calculate the hypotenuse of base diagonal and height. So what is our, how do I want to do this? Do I want to write it in here? Do I want to write it in my formula editor? Let's write it here. So my base diagonal is going to be the square root of width squared plus length squared. That's my base diagonal. And my big diagonal, the one I'm looking for, let's call it the long diagonal, that's going to be the square root of base diagonal squared plus height squared. Let's leave that as it is, because we understand what's happening. It matches our diagram. We have our base diagonal squared plus height squared is the square of that, okay? That looks great. Let's put it on Java there, which would not work. Let's write our methods first. Oh, no, I don't want to do that. You know what I'm going to do? Let's ask for the length, width, and height right away. And we're going to have a double length becomes input dot next double. You'll notice that I'm putting these blank lines in here to make things a little bit more readable into the width. And now let's have to calculate the volume. So the double volume is going to be, and what did I say I was going to call this get volume, okay? That's a nice name. And I'll give it the length, width, and height. Since I didn't say whether it was going to be centimeters or millimeters, they're just units. Now the question is, why am I writing just this part of it instead of all of it? Because I want to be able to build this in parts and test it, rather than having to do the whole thing all at once. So here I'm going to have a method that returns a double. It's called get volume and it's going to have a double length, double width, and double height. And I'm going to return length times width times height. And let's see if that part works. So if I have let's say 1.5 times four is six times four is six, and six times two and a half is 15. And sure enough, I get 15 cubic units. Okay, I know that volume is working nicely. I don't want to use some weird numbers like 15.783, 18.902, and 54.007. How in the world am I going to know if I got the right answer? Whereas if I use numbers that are not trivial like zero, one, and two, and things I can do in my head, I've got a good chance of being able to figure it out. Now let's do our surface area. Again, we're going to give it the length, the width, and the height. I'm going to call a variable area, and that's going to be two times length times width plus width times height plus length times height. And then I'll return that area. I've split that calculation across three lines just to make it easier for me to handle. I could put it all on one line, but I just decided this one, it feels better to do it this way. Because again, it gives me the relationship to what's happening. I've got the width times the height, the length times the height, and the width times the length. And we're going to assist on out of dot print f. Surface area is square units. Oh, goodness gracious me. Let's see if I can bring up K calc here. All right. Let me pause for a moment while I go and get a pencil and piece of paper so I can write this down so I can keep track of what I'm doing. All right, these are the three numbers I'm going to use for my length width and height. So I'm going to need 1.5 times four, which is going to give me six. And then I'm going to have 2.5 times four, which gives me 10. And then I have 1.5 times 2.5, which gives me, and this one, I'm too lazy to figure it out in my head. I'm sorry. Let me go back to simple mode here. There we go. 1.5 times, clear everything. 1.5 times 2.5 is 3.75. So if I add those all together, I should get 19.75 times two, which I think is going to give me 39.5. Let's check that out. 16 plus 3.75 is 19.75 times two, it's 39.5. Okay, great. I like to be able to do this and with numbers that I can at least handle. And let's compile this. Oh, well, that's lovely. I forgot to name it get surface area. Why? Because I didn't carefully read what it said here in the assignment. Bad teacher. So I have 1.5, four, and 2.5. And my surface area is correct. Now I have my public static double get diagonal. And again, I have my length, my width, and my height. I'm gonna set my base diagonal to be the square root of length times length plus width times width. Then I'm going to have my long diagonal is going to be the square root of base diagonal times base diagonal plus height times height. And then I'm going to return that long diagonal. Let's go here and sort of call diagonal is going to be get diagonal for length, width, and height. Okay, this one is going to be interesting. I'm going to have to use my calculator a lot to figure out what's going on here. So this is my length, my width, and my height, correct? Okay, so my base diagonal is going to be 2.25 plus 16, which is, well, 1.5 squared is 2.25. First thing that's 18.25. And I need the square root of 18.25. So let's see here. Does it have a square root button on it? No. Well, curse me. Okay, fine. If K calc isn't going to help me, let's go back into J shell. I'm going to do math dot square root of 18.25, which is about 4.272. Now, my long diagonal is going to be the height squared, 2.5 squared, 6.25 plus this squared, which happens to come right back to 18.25, interestingly enough. And that gives me 24.5. And I need to get the square root of that. And that's going to be, math dot square root of 24.5. Here's my 4.949, which is about what I expect to see as, when I'm done doing, as my result, unless I screwed up my calculations by hand. I'm doing this on the fly. So the chance that I've screwed up my calculation by hand is rather large. So we have 1.5 by 4, by 2.5, 4.95 units. And what did I say that I was going to come up with? It was 4.949, which rounds up approximately to 4.95. So yeah, hooray, it's all working. Now, there's something to notice here. Remember when I said I have the 4.272 and I immediately squared it back up here to get this? Hmm, that makes you think, doesn't it? So the question is, keep reducing this when I don't want to, maybe I made this calculation a little bit more complicated than it needs to be. So take a look at, I'm afraid I'll do this here, let me just do a formula here. By the way, I'm going into a lot of detail here. Don't worry if you don't want to look at this detail. The question that we're really asking is, is there a way to simplify these equations? So I don't have to do as much work to get the long diagonal. And the answer is yes. So here, let me insert a formula and let me make it large here. So my base is going to be the square root of length squared plus time plus width squared. And my diagonal that I want is going to be the square root of base squared plus height squared. So those are my two formulas and you can take a look at those and see if you can figure out how to combine them into one formula that will be a little bit more reasonable. And that I think is going to do it for today's mini lecture and that pretty much does it for chapter four. And what's coming up in chapter five? Chapter five is conditionals and logic. Okay, hooray, finally we have this boy, are we going to be able to do great stuff once we have conditionals and logic? Not that we haven't been able to do some interesting stuff already, but this just is going to expand our possibilities and expand our horizons enormously. This will be good. See you all in mini lectures next week.