And in today's lesson, we're going to learn about using lambda. Lambda expressions allow us to put calculations directly into lines of codes and call upon them, sending them different parameters using the interface. Let's take a look at some code and see what it looks like. We're in our online GDB compiler this time. And we've got a program that uses some lambda expressions. Let's run it first and see what it does. Click on Run. And as you can see, we've got a calculator. Zero to exit the program, one, two, three, four for addition, subtraction, multiplication and division. If I press the number one, it will ask me to enter two numbers to add up. I'll enter two and a seven, and it gives me an answer that says two plus seven equals nine. If I do subtraction, that's number two, it'll again ask me to input two numbers. This time, I'll input seven and two and it gives me the answer seven minus two equals five. If I wanna do some multiplication, that's number three. It'll ask me to enter two numbers to multiply. Let's use some bigger numbers. 75 times 100. And it tells that 75 times 100 is equal to 7,500. Finally, number four is division. And it asks me to enter two numbers to divide. Well, I'll take the number 10 and I'll divide it by zero. As soon as I divide by zero, it says zero cannot be divided into 10, please enter a new divisor. If I enter a four, 10 divided by 4 is equal to 2. Again, we're not dealing with fractions here, although we could if we wanted to. If I switch this around and did division one more time, and said I would like to divide four and divide it by seven, it says seven cannot be divided into four. I must use a new divisor. The second number that you enter has to be less than the first number. So I'll enter two and it tells me that four divided by two is equal to two. Finally, when the program ends, I type in a zero to exit and it says thank you for using The Knowledge City Calculator. Well, now that you've seen the program run, let's take a look at the code that makes it run. I start off by declaring a scanner and that's going to allow me to input some numbers. I have my main class, and in my main class, and this is critical to using Java's lambda, I have two interfaces. And the interface is the thing that allows me to send parameters to different functions. In this case, I have an interface called doTheMath, and I declare a function inside doTheMath called addSubMultDiv that accepts two arguments, both integers. And returns an integer. My other interface accepts a string that is a message and that's going to allow me to print information on the console. So these two interfaces are the thing that's going to make our Java lambda expressions work. I also declare a function called operate where I'm going to get the int that I'm going to call a, the int that I'm going to call b, doTheMath, and text. And the idea here is I'm going to call upon these two things based on what parameters are sent to this function. I get into my Main argument routine, and I have my four features of the calculator, and each time I'm calling on doTheMath. And I'm setting it up so that it will do addition simply by adding x plus y together, x times y, x divided by y or x minus y. I set up a new object of my calculator, create my scanner and declare the variables that I'm going to need for the program. The next area allows me to put up a menu on the screen. I put that in a while loop so that it will appear for as long as the answer supplied is greater than zero. As soon as they type in a zero to exit the program, the while loop stops and the program will end. Once I get their answer, one, two, three or four, I use a switch statement to figure out how to process that answer and call on the correct item. We'll use the exit first. That's number zero and I will send a message to my speak interface telling the user thank you for using The Knowledge City Calculator. And I'll send it that message. In case they type a one, I'll ask them to enter in two numbers. And I'm gonna add those numbers together, and then print out the answer and here is where the Java lambda works perfectly. I can call this routine, calc.operate num1, num2 and the fact that I want to add these numbers together. What I'm doing here in this Java lambda is I am simplifying my code, otherwise I would have had to create a function for each one of these items. And while you may say well, it's only four items, it's not a big deal, there may come a time where you need many, many more items, and having one function available that accepts multiple parameters and different operations to do saves you a lot of time in coding. The rest of the program works pretty much the same way. The code for each of these operations, add, subtract, multiply and divide is exactly the same except for divide where I have a check to make sure that number two is not greater than number one because I don't wanna divide a larger number into a smaller number. And I can't use zero as a number that I'm dividing by. So if either of these conditions are true, I issue an error message that says we can't do that. Please enter a new divisor. When they type in an acceptable number, the system goes through and calls on the calc.operate and sends it the divide function. Now you know how to use an interface to create a Java lambda expression, and then call on that expression based on what you wanna do in the program.