 Hello, time for the mini lecture. Here's a program that we did a day or two ago where we had the discount based on the quantity. And then we figured out the subtotal discount amount and all that good stuff and printed it out. And this program, much as most of our programs is using variables. Why do we use variables? Excuse me, I had to clear my throat there. One reason that we use variables is because we don't know what the value is. We're gonna need to get it from somebody else out of the keyboard. And we have to save it so that we can use it later on. And as you have seen, we can put doubles into our variables. We can put integers into our variables. I haven't done a lot with strings but that's gonna be coming up in the next week or two. And there's one other data type that we have not talked a lot about and that's Booleans. Again, we've been talked about int, we've talked about double, we've talked about string and this one Boolean. And this is a special type of variable that can only have two values, true and false. There's only two possible values that a Boolean variable can have. The most important thing to remember when you're working with Boolean variables and Boolean methods, which we'll talk about today also is that whenever you do a conditional expression, something like, for example, age greater than 18 or age greater than or equal to 18, 18 and age less than or equal to 65 or day is three or day is five. These all give back a Boolean value, namely true or false. Because they give us a Boolean value, we can store that value for later use inside of a variable. Let me go to JShell here. And for example, if I say, what is three less than five? Three less than five is true, that's a value. Just as three plus five gives me a value and I can store it somewhere, I can say integer result becomes three plus five. I can also say I want a Boolean value called, let's say, comparison. And that's going to be assigned the result of comparing three to five. Comparison now has the value true in it. Just in the same way that result had eight in it, comparison has a Boolean value and it's storing that value so that we can use it later on. Now, when would you ever need to do something like that? I'm going to show you an example. It's a bit artificial, but eventually as you keep going, getting more and more complex programs to write, you will find that Boolean variables come in really handy. So here I'm going to ask for an integer, tell you whether it's even or odd and then I'm going to square the number, report that result and then I'm going to deliver an insightful message depending on the original number. So here I'm going to get a number from the user and here's a condition. It's going to either be true or false. When the condition is true, I do the if claw body. When the condition is false, I do the body of the else clause. So that's going to print out whether it's even or odd. Then I'm going to square it and tell you what that is. Then I'm going to again say, okay, is n mod two equal to zero. If it is, then I'm going to say, turns out that the square of an even number is always even. Otherwise, if your original number wasn't even, I'll tell you that when you square an odd number, the result is always odd. So let's compile this and run it. And if I enter an integer like 17, 17 is odd when you square an odd number, the result's always odd. And if I put in something like 256, that's the square of that. And it turns out that the square of an even number is always even. Why this is true is a bit of algebra. I'll leave the exact mathematical proof up to you because we're not doing math proofs in this course. We'll leave that for the math people to do. Rather than having to do this twice, I could have a variable. Now I'm going to show you a way that people do this. Let me save this under a different name again. Even odd two dot Java. And you might, and you also, I see a lot of this code here. I see people say, okay, we're going to have a Boolean variable called is even. And then if n mod two is equal to zero, we'll set as even to true, otherwise is even is false. Then instead of having to do this division again to get the remainder, I can say if it's even and I can use it again here. If is even. In fact, I'm going to do something here. Yeah, let me just work on this for a moment here. Oh, this is interesting. Oh, hello, Earth to Eisenberg. I need to do an assignment there. That's much more like it. And now everything works the same as it did before. So if I have something like 04711, and if I do 1066, yeah, of course. This is what weirds people out. I see beginning programmers always doing something like this. People say, well, if is even is the same as true, and they'll put it in here as well. There's nothing wrong with this. It's fine to do this, but what you're saying, remember is even is already true or false. So you're asking if the true is true, but all I need is an even, why am I getting too excited about this? I'm trying to convince myself too much. Deep breath, backup. Is even already holds a true or false value. We saw that here in lines 21 through 25. Remember when I have an if, the condition has to give me back a true or false value. Is even already is true or false. I don't need to do a comparison again to see if it's true. I can do that, but it's bad programming style. So let me save this as even odd to bad style.java. And that means I have to change the name here also. And again, everything's still gonna work just like a champ. So if I have an integer 15, it's 225, 12 squared is even. This is all working totally as expected. Let me put in a comment. So I see a lot of that happening. Well, there's something interesting here. Remember how I said that this condition is going to be, give us back a true or false. If it comes back true, I said is even to true. Otherwise this must have come back false and I'll set is even to false. I can improve on that. Let's save this as even odd to better style. I can say is even. Oh, I don't know how I can do it all in one step here. Is assigned. Let me change my comments here for false. Then I can get rid of this also. And this is a better style. Again, what I'm saying is let's take n mod two equals zero. That's either gonna be true or false and I'll store it in is even. And that takes the place of having to have that if and else. So you don't need to have an if else to assign true or false to a Boolean variable because it's already true or false. And this is one of these little speed bumps that people run into when they first learn programming. They have to become convinced that this sort of thing works. You will see professional programmers use things like this all the time. They won't use an if else. I'll say I've already got a true or false or why do I need an if or else to repeat myself? Oh, actually, this is not gonna compile until I do that correctly. Then again, I can say 17 is 289, 222 is even. Oh, by the way, these two files here are something I was working on earlier. Let me just close those so you don't have to have to wonder what in the world was he doing with those two? I've never seen those before. I can close this because I don't need it. Now let's go back to our days per month seven, the one where we had the leap year business here. This is a really complicated expression. But again, it gives me back a true or false. That means I could store it in a variable if I wanted to. And in fact, that's what I'm gonna do. Let's save this as days per month Boolean. So I can have here Boolean is leap year is going to be assigned all of this stuff here. Again, this is gonna give me back a true or false when I'm done. Okay, I'll just assign that to is leap year and be done with it. Now I've gotten to the line that's fairly long here. If I ever see my line heading over often to the right edge of infinity, I'll split it wherever there's an operator and then go to the next line and indent that. That way my code is a bit more readable. Now I can say, okay, if is leap year, days is 29, else days is 28. And I can use that again down here. I can say, if is leap year, there's no else clause. If it's not a leap year, there's nothing special to say about it. And goodness gracious, what has happened here? Oh, indeed I have a problem here. What's the problem? Problem is local variables. Here, when I did this Boolean is leap year, I did this inside these braces. But remember, anything that you declare inside of a block is local to that block. That means as soon as I leave these braces, the variable called is leap year no longer exists. Okay, no problem. I'll assign it out here. Boolean, and I'll just grab all this. Now is leap year is defined in this block under there. There it is, down to this block here. And that means that is leap year is available here and here also. And of course I forgot to change the name. And so if it's, let's say fifth month of 2023, 31 days, seventh month of 2024, that's 31 days. Now looking at that, the fact is that February is really the only place where that message is really appropriate. So you know what I'm gonna do? I'm gonna grab this here and I'm gonna put it in there. Then I don't need this if statement. So it will only happen when I have a leap year and I'm in February. I think that's a better place for it. So I have month two 2023, two 2024. So that's a Boolean variable. Now having this calculation sticking out here in the middle of nowhere is a little bit ugly. What I'd like to do is I'd like to delegate that task to a method. We've seen that methods can return integers. We've seen methods that return doubles. We haven't seen anything that returns string yet. I don't think I've done any examples but those are gonna be coming up. Well, if I can return any old data type I have, I can certainly return Booleans from a method. So let's save this as dayspermonthmethod.java. We'll call this days per month method. And here what I'm going to have is I'm gonna have a public static. And again, it's always followed by the data type or void when you don't return any value. We wanna return a true or false value. We wanna return a yes or no. And we'll call isLeapYear a method and give it a year as our parameter. Here's my magic formula. I'm going to have a Boolean result which is assigned to that. And then I'll return that result. I mean, done this to look a little bit better. I could do this all in one line. I may change that in a moment to make that happen. But now isLeapYear takes an integer and returns me a yes or no true or false. And essentially it hides this calculation away from me having to read it here in the middle of my program. Which means here, I'm gonna say if isLeapYear a year, I'm gonna make the method call. I'm gonna take whatever year you gave me. That's gonna be the argument which gets passed into this parameter. It figures out whether yes or no. This is a leap year or not. And then returns that result. And again, it's returning a Boolean result and that's what I need for a condition. I need a true or false value. And that's what's being returned here. Let's compile it. So if I have month two, 2023 and month two, 2024. And that is a Boolean method. Since this is again a Boolean value and that's what I want to return, I could have written it this way. I could have said return this whole business here. And that would work also. If you think that's a little bit too weird, you can always use a variable to hold the result and return the result. If you had a very complex calculation that needed a whole bunch of statements and a whole bunch of difficult calculations, then in that case, you might want to put the result in a variable and return that rather than trying to do it in one expression. It just so happens that is leap year works really well with an expression here. Let's compile that. Let's run it here. And again, 2023, two, two. So that seems to work like a champ. And again, this is a Boolean method. It returns a true or false value. And the key thing to remember is, all give back a Boolean variable. You don't have to compare a Boolean value to true or false to make an if decision. So I say, well, Boolean, let's say, this presumes by the way that day one of the week is Monday. I can say, if weekend day, if you ever see yourself comparing something to true or comparing something to false, then that's a big signal that says, whoa, maybe I can simplify this. Anything else in here that I want to look at? Yes. Quickly, one thing about input is, let's go here real quick. Which one should I do here? Oh, let's take the, even an odd, because that's a nice one here. So if I say F-I-V-E, Karoom, that's an input mismatch exception. Why? Because F-I-V-E can't be converted to an integer. How do I, there is a one way to catch it, which we will learn in chapter 18 called exceptions. But it turns out that the scanner has a nice little way of handling this sort of stuff. We can say, if input dot has next int, and by the way, that's a Boolean method. Oh, this is actually going to be really cool. So let's save this as even odd validating dot Java. So if there's a next integer available, if the next thing that's about to be read is going to be an int, then it's okay to do all of this stuff. Otherwise, so let's compile that and see what happens here. And what have I done here? I missed, oh, because I have the two in there, and it's not in the file name. You'd think that after all the years I've been programming, I would learn to check that before I start compiling it, but no. Okay, this time it compiles perfectly. And now if I say 2048, then that's great, it works. If I run it again and say five, so sorry, we want integers with digits. Along with has next int, there's also has next double. So you can check to see if what the person has entered is a double before you read it in and try and do a calculation that will fall down, go boom. And that should do it for the mini lecture today. See you next week when we will be covering which chapter? Loops and strings, okay? Yeah, I'll talk about it then. Make sure you read the chapter before next week because then a lot of what these lectures say are gonna make a lot more sense. Not that they ever have. Why should I start making sense now? But that's another story. Okay, talk to you whenever, bye.