‫Welcome back.
‫In this video we're going to create the function that solves the quadratic equation.
‫Here I remove this binding variable and I'll pass it to the primary constructor.
‫I need this binding variable to access the views of this layout from edit text to the result text.
‫Under this variable I'll create function solve equation and this equation function will take the view
‫as a parameter view view parameter.
‫Because I'm going to execute this function from the XML file, because I'm going to use data binding
‫with this equation.
‫Make sure whenever you create any new function and you need to execute it using data binding, you need
‫to pass the view as a parameter.
‫Now I want to convert the strings to integers.
‫I'm going to get the strings a, b, and c from the edit texts, and the edit text input will be in
‫form of strings.
‫So I need to convert this string into an integer.
‫In order to execute mathematical equations and functions.
‫I'll create variable a equals to a dot two.
‫Int a is referred for this getter for this variable, and two int is a function called on this string
‫a to convert it into an integer.
‫The same I'll do with b and c, so b dot do int and var c equals to c dot two int.
‫The second step is to calculate the discriminant.
‫The discriminant D or delta equals to b square minus 4AC to create it.
‫Here encoding I start with var discriminant equals to b times b minus four times a times c.
‫The third step is to find the solutions or the roots.
‫I'll create two variables x1 and x2, and I'll start checking for the discriminant value discriminant
‫greater than zero.
‫Then there are two real roots and two distinct roots for our quadratic equation.
‫So when calculating the discriminant b squared minus four AC.
‫When d greater than zero, the discriminant greater than zero.
‫The quadratic equation has two real roots.
‫If d less than zero, the quadratic equation has no roots.
‫When d equals to zero, the quadratic equation has a repeated root one root but double root, and the
‫general solution for the.
‫The root is x equals to minus b plus radical b squared minus four AC over two a and x equal to minus
‫b minus radical b square minus four AC over two a.
‫So let me implement it here x one which is the root, the first root and the first solution minus b
‫mod plus math dot square root.
‫Math dot square root is a general function that is commonly used in Android development, and in Java
‫in general, and Kotlin to calculate the square root of a given number.
‫This method takes a single argument, which is a number for which you want to find the square root,
‫and it returns a double value representing the square root.
‫So if I pass, for example, 25, the square root of 25 is five.
‫So it's returning value would be five.
‫Here I want the first root is minus b plus radical the discriminant over two a.
‫So b squared minus four AC represents the discriminant square root of the discriminant over this values
‫over two times a.
‫Here I need to change data type of this root x1 and x2.
‫I'll change it to.
‫Float is good and float here.
‫Also, you can change it to double in order to prevent any possible error.
‫And here I need to convert the discriminant from integer to double.
‫You can use directly to double method.
‫Also, I can get rid of math dot, and I can use directly the square root function and you can choose
‫Kotlin dot math the same way I'll create x2 equals two minus b plus or minus square root.
‫The plus sign is replaced by minus sign the square root of this quantity.
‫And this is good.
‫These are the roots.
‫Now we're going to display.
‫The results in the text view, I'll use the binding object that it is passed here.
‫I'll make it as var and I can access it.
‫Binding dot text view the result text dot text equals to x one equals 2X1 variable plus comma x two
‫equals to x two variable.
‫So here I am displaying the routes.
‫Second possible state of discriminant is less than zero.
‫So else if the discriminant less than zero there are no real roots.
‫So binding dot result text equals to no real root dot text.
‫Set it to no real roots.
‫There are complex roots okay.
‫It's very simple.
‫And the last state, if discriminant is equal to zero there is only one real solution.
‫It's called double root.
‫So x one equals to minus b over two times a.
‫This is the solution.
‫And here I need to convert it to double because this is an integer.
‫And here binding dot result text view dot text equals to x equals to x one.
‫These are the three states of the discriminant greater than zero.
‫Less than zero equals to zero.
‫This is how we solve the quadratic equation.