 Okay, and today's mini lecture, I'm going to do one of the exercises from the extra exercise book. And in this program, we're going to use an array of three double values to represent a vector in three dimensional space. And when we have a vector in two dimensional space they have x and y coordinates for three dimensional coordinates they're called x, y and z. The first element in our array will be the vectors x component, the second is y component and the third will be a z component. Let's make this consistent. And we write this in the form x, y, z in parentheses. What we're going to do is we're going to have the following methods, each of which takes two three element arrays as parameters. We can add two vectors by adding their component parts. And then we can have the dot product, which takes two vectors and returns a new vector that has the product of the corresponding components space that out nicely here. And finally the distance between two vectors is the square root of the difference of their components squared the sum of that and take a square root of that. So we're going to ask the user to enter two vectors and then display the sum dot product and the distance between them. Let's first implement a get vector method that has a prompt and a scanner as its parameters to return an array of three double values. We're going to return an array of array of double. And we're going to call this get vector. And we're going to have a string prompt. And the scanner, which I'll call input. Now questions, what do we want this to look like when it grows up? Do we want to have the x, y and z components separate from one another on separate prompt separate inputs, or do we want them all at once I think it's probably going to be better to get them all at once. So we can do here is we can have a double. Let's call this a vector one will be get vector and so, in fact, okay now this is enter x, y, z separated by spaces for first vector. And then we'll have input as scanner. We'll have vector two, and that's also going to need a scanner and let's make this spacing consistent. So here what we're going to do is we're going to do a system dot out dot print of prompt. And we're also going to have to create a result. We're going to have a result and that's going to be a new double array of length three. And then we're going to say results of zero becomes input dot next double result one becomes input dot next double and result two becomes input dot next double. And because we're going to be returning a value, we'd better return the results. Let's just see if that compiles okay for now. Compile successfully which is good. And then let's do this. Let's do system dot out dot print line of vector one. Plus arrays dot to string of vector one. And vector two. That way we'll at least know that everything is working so far. And we'll compile that. And run that again test to see that things work so for first vector let's say is three four and a half and three four and a half and six. And the next one will be 7.28.5 and 12.4. Okay that looks great that was working nicely. Now notice this is displaying in his square brackets instead of parentheses. If we wanted parentheses we'd have to write our own method for displaying the vector. Let's go back and do that later for right now. If I feel the devil. I'm not going to promise make a promise I'm not. I might not be intending to keep. Let's see how this goes and if I feel up to it. I'll write such a method that displays the vector in parentheses. Okay. Now what we need to do is we need to write these methods that will manipulate them. For example, adding the vector. We have an add. Okay, it's going to give us back a new vector. And we're going to call it add. I call it add vectors but according to this we're just going to call it add. And we're going to have a double array called that one and double array called back to these are all going to be three long. Okay. Design decision time. This is part of the planning question. What do I do if the arrays. I get for adding dot product, etc. Don't have three elements in them. Or aren't the same length. And the answer is, I'm going to return a vector. I'm going to return three zeros. So there's my design decision. So if that one dot length. Is three and vector to dot length is equal to three. Then it's okay to do this. Otherwise we're going to have to return an array of three zeros and let's call this the sum is going to be a new double. And remember by default. That's filled with zeros. So if both of them are length three, then everything is cool. Otherwise one, they're different lengths, or they're the same length but not three, whatever it is so. Otherwise, there is no otherwise, because we'll have exactly what we want here. Now for this we say for int index is zero index less than vector one dot length, because we know it's going to be three. Then we're going to say the sum for the index is going to be vector one for that index plus a vector two for that index. And let's try that. We'll have a double. Vector some will be add. Vector one with vector two. Again, I don't want to break the whole program and then test it I want to build it up part by part. Okay, let's go here. Well, that was exciting. Yeah. How about closing off my method. So if I have, let's take some numbers that I can figure out easily in my head. And let's say seven, eight and two. Yep, 1012 and seven. Cool. Edition works. Let's just check on this bad vector thing just for the heck of it. Let's call it bad vector is going to be a new double array of length four. And then I'm going to say bad sum is going to be the addition of vector one with the bad vector. And then this is some of the out dot print line of bad some is less raised up to string of bad some. It is nice to test the bad input really gives you bad results. 34578 and two. Yep, and the bad some is 0000 which is exactly what I wanted it to be. Okay, I'm happy with that. Now we need dot product dot product is essentially the same thing as add, except that we multiply instead of adding. So we have vector one index times a vector. And let's call this product because calling it some would be misleading. And we'll return the product. Oh, that was silly. Why was that silly. I'll give you a couple of minutes to think about why didn't I just go with the program right then. So pause the record, pause the playback for a second and give it some thought. Why would it have been silly for me to go and run the program right now. And the answer is, I've written this lovely method here. I never call it. So what's the use of it will never get called just sitting there how am I testing it. I'm going to also comment this out so I don't have to deal with it any further. So now I'm going to have double vector product will be the dot product of vector one and vector two. There we go. And now it makes sense to run this says do three four and five and seven eight and two. And I get 2132 and 10 which is exactly what I should have last method that we have to implement is distance. Again, we're going to grab all this stuff here. Now does that return us a vector or not. The answer is no this does not return return a vector returns just one number. Okay, this is by the way is the book wrong on this one I'm just curious to know. Oh okay it doesn't say anything. This is interesting. Wait a minute. The dot product. Oh I'm sorry I lied. The dot product is not that it is it's just the it returns only one number. Wow I sure misread the book didn't I. I'm not totally. Yeah, okay. My bad as you young people say, we're not going to be returning this. Instead, we're going to start our product as zero. And then we're going to say the product becomes its previous value plus the multiplication of the other indices. So when I described it earlier I described it totally wrong. Sorry about that. Again will still return a zero if nothing is good. Similarly with distance well okay. Again, this is this is me. Making sure I don't try to do too much at once. I want to make sure that dot product works first and so I'm going to comment out the distance because I don't want to use it yet. In this case our vector product is a single entity. Okay, something that I need to talk about here this is unpleasant. I was going to call this dot product. That means I would have the same name for a method as for a variable Java will let you get away with that. But there are a lot of languages that were will either be a compile error or it will do something that is not what you want and come back to bite you. I'm looking at you Python. That means we really would like to have a different name. So let's call it dot vector product. That way we don't have any conflict between the name of our method. And the name of our variable. Let's do three four five seven eight and two. And we have 21 plus 12. 21 plus 32 is 53 plus 10 is 63. So our dot product is correct. Okay, now I can go on to the distance here. Distance is definitely not going to be a vector. It's going to be a scalar. And so we're going to have a double called sum of squares and that's going to start up at zero. And we're going to return the square root of the sum of the squares. Now what we want to do here is we want to say the sum of squares becomes its previous value. Plus back one of index. Minus the back to have index and I'm going to multiply that by itself. Let's raise that to the second power. That's the way we can square it easily. So I'm going to take that one sub index minus back to index. Square that and add that to the sum of squares. Let me check with the book to make sure I'm doing that right. Yes, I take the individual components. I take the squares of their differences, add them all up and take the square root of that whole shebang. I want to compile this just to make sure I didn't screw up anything there. And then finally here we're going to have distance between vectors is going to be the distance for vector one and vector two. And let's run it. And we have three, four, five and seven, eight and two. Well, that's interesting. What am I doing wrong? Okay, let's try this again here. Three, four, five, seven, eight and two. Now distance is 6.4. Now is that correct? Well, let's figure this out. Let's go and bring up our calculator here real quick. Okay, three minus seven is negative four. Square that we get 16. Four minus eight is negative four. That gives us another 16. Five minus two is three. Three squared is nine. So we're going to add a nine. We get a 41. And now we need to take the square root of 41. And I can't see the, do I have a square root here? How about simple mode? Oh, great. Unfortunately, I don't have a square root key here. I've got an x to the y, but okay. Let's do a real quick test test here. We're going to go into j shell and do math dot square root of 41. Six point 403124. And sure enough, okay, perfect. We got what we want. So our distance now works. Okay, so there we have it. Now the next thing that we wanted to do was this arrays dot two string. It was in square brackets and we wanted it in parentheses. Oh, yeah, I got a few more minutes. What the heck? At this point, the material for arrays is that we've done what we need to do. You can stop watching. Unless you're just incredibly interested in seeing how I solve this other problem. Save this as vector 3d version two dot Java. And this will be a 3d version two. So now we're going to need to do is we're going to need a public static. We're going to return a string. And we're going to call it two string. Why not? But this time we're going to say, yeah, okay, this is fine. Double. Okay, we're going to have a string result, which is going to equal that. At the beginning, because we want parentheses. Now what we want to do is for. And index is zero. Index less than vector dot length. And index plus plus. We're going to say result becomes result plus. Vector sub index. Now the question is when do we want to add another comma? We want to do it. Everything except the last one. So if index is not equal to our last one, which is vector dot length minus one. The result becomes result plus comma space. And finally, we say result becomes result plus. Closing parenthesis and return that result. I think this should work. The reason I wrote it this way is because what if we have nothing if we have an empty vector, if I give an empty array, then we won't do anything inside this loop and we'll just have to parentheses one after the other. Let's compile that compiled okay. And now instead of a raise dot two string. I can call my two string method that I just wrote here. So I have three 4.7 and 8.2 5.6 negative 20.1 and 18.84. And it looks like we've got what we came for. So now I have parentheses instead of square brackets. So there we are manipulating arrays and that will finish things for this week. Next week we are skipping chapter eight. So you can read it if you want to it's an interesting chapter, but you're not required to read it and it will not appear on any of the exams. Instead we're going to start talking about objects. And we'll go back when somewhere I must have said that Java is an object oriented language and if I didn't say it the book certainly says it. So what's the biggest shmigeggy about objects, why are we making such a big semis out of it. Well, we're going to find out in chapters nine and 10, and specifically in chapter 11, which is where we're going to be designing our own objects. Right now we've seen only two kinds of objects one is strings and the other one is arrays arrays are objects. We're going to learn more about these fascinating things and we want to be able to build our own objects. So that's what the next two weeks are going to be all about. And in fact, all of these topics like arrays of objects inheritance of polymorphism. All of those are going to be doing to have to do with objects. And then arrays of arrays and exceptions by well let's let's not worry too far into the future. See you all next week.