 Hi short mini lecture tonight. I need to talk about call by value and a little bit more about the math methods and The call by value. I'm going to go through the example. That's in the book. I'm changing it a little bit here So here I have a method called cube. It takes an integer number Multiplies it by itself three times and reassigns that value Doesn't return anything. It's void Here inside of main I'm going to set n to three and call a cube of n and then print out what n is now Remember that this is a copy this n gets copied into number That means the original n remains untouched even though number is going to be updated to 27 And in fact, let's put that in here so we can see that we see that that's the case And why don't we do a before and after? start n is So we should see at start n is three Then we're going to call cube of n and that's going to print out that the number inside the cube method is 27 because remember we reassigned the parameter and Then when we come back and will still be three why because number is a copy of n the original remains untouched and That's exactly what happens. In fact, let me change this to say That and n is there we go That would be it will sort of matches now what I'd like to do is I'd like to use that Java visualizer so that we can see this Happening step by step. So let's go here Pop that in there and let's visualize execution So in our main method, we're on line eight That's what this is and we have what's called a frame every time that you enter a method you get a new frame And we'll go forward So n becomes three and then we're going to print out That's start n is three which we expected and now we're going to say cube n cube gets its own frame and The number is three because that is the name of the parameter This three in n got copied into number Then we multiply number times number times number. It gets changed to 27 and we print it out When we hit the end of our method Its frame disappears and any variables that that method used also disappear along with it There's no return value here And now we return and we are back in our main frame and then we're at this statement And we print at end and is three because again in has never changed Now the thing that I also want to tell you is that people think these names are magic Names aren't magic. What if I change this to in? Now this variable in has the same name as this That's going to change everything, right? Nope, I'm sorry to tell you that won't change a thing Let's compile. Oh Well, I give it it gives me an error because I didn't change it everywhere compile and Let's run it and in fact, let's change it to make it look correct value of n in cube method is that and Becomes three inside of cube it's 27, but when we come back and is still what it used to be Just because these have the same name does not mean that call by value stops working There is no mystical connection between this integer name and and this integer name and because they happen to have the same name As I said before these are local variables n belongs to main the n on line 15 the n on line 9 Belongs to cube and they are totally different and let's prove that by looking at the job of visualizer Let's go back here And let's edit the code and let's pop in the one where we have n instead And let's visualize the execution of that and it's going to look up very much the same we start here in main and Becomes three we print out its value now we call cube with n as our argument and We are now inside of the cube method and n here is in a totally different frame. This is a different end It belongs to cubes frame It's different from this variable called n which is inside of mains frame Now we've changed n inside of cube to 27, but look in main hasn't changed why because n The parameter is a copy of the argument. It's not the same as the argument No matter what its name is but then There's no return value because we're void and when we come back to pick things up We're still back in the main frame and the only n available to us is the one that was declared here on line 8 and That's why we see The three So that's the big simis that I want to talk about here. That's the big deal today is that we have call by value Whatever you put in the argument gets copied into the parameter Whether they have the same name or the different name doesn't matter Changing the parameter will not change the original argument Now when we get on to things like arrays, which is later on in the course You're going to see something that looks like I'm lying to you, but I'm not and Well, I'll cross that bridge when I come to it Okay, the other thing that I want to talk about let me pause the recording for a second here The last thing I want to talk about is some of the other math methods. We already know about math dot square root And we know what I don't know if I talked about it, but there's math dot pow for example Let's say I want the cube root of 1728 I Say 1.0 divided by 3.0 and that's essentially 12 now questions Why is it 11.999998 instead of 12.0? And the answer is because Given the number of digits of accuracy that you have sometimes you cannot get an exact answer Now the next question is well, what about trigonometric functions like sine cosine and tangent and there's something important that you need to know Okay, we're all I don't know if we all remember it, but the sine of 30 degrees is 0.5 So if I say, okay, let's do the math dot sine of 30.0 Well, that is definitely not 0.5 what went wrong and what went wrong is that sine cosine and tangent take their argument in radians not degrees Now what's a radian the answer is to convert from degrees to radians you multiply By pi and divide by 180 So let's do this. Let's put a double degrees is 30.0 Now I can have my double radians and that's going to be degrees Times and it turns out that the math module also has a constant for pi divided by 180 Now if I say what's the sine of radians I get something that's very close again because of the accuracy of the machine to 0.5 So the moral of the story is when you have sine cosine and tangent you need to convert from degrees to radians Now you could do it by multiplying by pi and dividing by 180 You can also say here is I could say have said I could have said double radians becomes math dot Hope I get this name right I believe it's called two radians of 30.0 and that does a conversion to radians and then I can say math dot sine of radians And I get the same result So math dot two radians takes a number of degrees and it gives you back a number of radians which you can use as input to Sine cosine and tangent tangent What if I want to say that arc cosine the inverse cosine of 0.5 I'm sorry the inverse sine the inverse sine of 0.5 should be 30 degrees But it comes out in radians So I have to say take the math dot a sine of 0.5 Which is radians and I'm going to put that into math dot two degrees. I Can nest my method calls So the inner one becomes first it takes the arc sine of 0.5 Which will give some number that's in radians and then that will be passed to two degrees And that will give me the number of degrees in the arc sine of 0.5 which is 30 and I don't know if you're going to be using his sine cosine and tangent and arc sine arc cosine arc tangent a lot But in case you do remember They're all radians not degrees and that's why we have the two radians method and the two degrees method So we can do these conversions easily and That is about all that I think I want to talk about for tonight's Mini lecture not sure what I'm going to do for tomorrow's what I might do is I might do some of the examples in the book in the extra exercises book So, yeah, that might be well Here's what you might want to do if you want practice with methods do exercise 4.1 Yeah, this exercise in 4.1 and also do exercise 4.2. I think those will be really good ones for you to practice with Actions 4.3 happens to be the assignment for methods, so I'm not going to do that one for y'all And then if you want a real interesting challenge you can use exercise 4.4 to do the monthly payment on a loan this one don't write it don't just read the formula and Start hammering away on the keyboard not going to work read the whole thing carefully and then figure out Okay, what would I have to do to be able to solve this by hand? And you might need a calculator to do this calculation of one plus r to the negative n. Okay, but Using your calculator will count that as being by hand and then oh this is a whole bunch of good This is some interesting exercise here, so I might strongly suggest that you do some of these Exercises so that you can practice with writing methods See y'all tomorrow