 Hey, time for tonight's mini lecture. And here's a program that we developed some time ago, which you give a distance that something falls in meters. And then we figure out how long it takes to fall that distance. And in fact, let me just change something here real quick. Let's have a final double gravity is 9.8. And then we can get rid of this magic number and replace it with something more meaningful. There, I'm happy now. Doesn't take much to please me, I guess. And let's compile this and execute it. And if something falls, let's say 50 meters, 3.194 seconds. Okay, no problem there. It's pretty straightforward. Now let's try this again and both have a distance fallen of negative 45 meters. And we get not a number seconds. That's what NAN stands for. And the reason we get not a number is because when you try to take the square root of a negative number, you can't do it, and that's not a number. Gee, wouldn't it be nice if we could tell people, hey, we don't allow you to fall a negative distance? Let me repeat that. Wouldn't it be nice if we could tell people, hey, you can't fall a negative distance? And that's why we're going to have something called the IF statement in Java that lets us test conditions and do one thing if the condition is true and something else if the condition is false. So right here, before we go and do this call to the time to fall method, we're going to have an IF. The IF is followed by a space, always, always, always, at least according to our style guidelines. Now you may work for some of the company in the future where they say, no, don't leave a space after IF and then before the parentheses. In this course, you always leave a space after the word IF. Then we have in parentheses a condition to test. We want to test if the distance is greater than zero and if the distance is greater than, actually we can say if it's greater than or equal to zero. If it's on the ground, it won't take any time to get there. If the distance is greater than zero, we're going to open up a new block of code and inside that block, we're going to calculate the time and print it out. Otherwise, which means the distance must be less than zero, we can print out a reasonable error message such as distance cannot be negative. So there is your classic IF statement. If some condition is true, we do the block also called the IF clause. Otherwise, else, we do the code in the ELSE clause. And let's compile that and run it. And this time if I had to fall 45 meters, everything still works fine. If I try and fall zero meters, that's fine also. But if I try to fall negative 288 meters or 298.55, what the heck, let's be fancy. It tells us the distance cannot be negative. So again, this is your standard IF and ELSE. Yeah, let's go back and look at our pricing. I think that actually I'm not sure if I ever wrote this one before. So let's take a look at this and what we're going to do, actually let me open up a simpler one here, pricing one dot Java. I'm going to build up to this later one, other one later. We have a tax rate of seven and a half percent. We're going to ask you to enter a unit price and a quantity. And depending on that, we're going to give this subtotal, the amount of tax and the grand total, and then print all of that out. Let's run it to show what it looks like first. So if I have, let's say $5 and I buy 30 items, it's 150 and the tax at seven and a half percent is $11.25 and the total is 161.25. I just want to make a quick comment on this print F. Notice first of all, the tax rate is a decimal, 0.075. So when I'm going to display it as the percentage, I have to multiply it by a hundred. That fills in this percent 4.1 F, namely four decimal places with one to the right. Now I wanted to follow it by a percent sign. Let's go and run this again here so we can see it $5 and 30 of them. And notice I'm putting a percent sign here on the screen. The problem is, and I think I've talked about this before, percent sign is special. It means introduce a placeholder. So if I want a true percent sign, I have to put two percent sign in a row that says, no, no, no, this isn't a placeholder. I really, really want a percent sign to appear on the screen. Well, we know how to use our if statements already and I'm going to put those in later, but I'm going to show you another version of the if statement. And let's say that if you order more than a thousand of something, then you're going to have to confirm a purchase. We'll still give you all the invoice information, but we need to give you a warning message. So after we've given all of this stuff with the total here, I'm going to say if the quantity is greater than 1,000, then we are going to say system.out.print line. Please confirm this large order by calling 1-800-555-1212. There's no else clause here. If the quantity is greater than 1,000, then I'll ask you to confirm it. If the quantity is not greater than 1,000, there's no else, we don't have to do anything. We don't have to give a message. So let's compile this and run it. And if I have, again, $5 and I buy 30 of them, there's nothing special about that. But if I have something cost $7.95 and I order 1,024 of them, I'll get the message to please confirm the large order by calling 1-800-555-1212. And in fact, I should probably put a system.out.print line here, again, for readability. Going back to our falling object, there's something I need to talk about, semi-unpleasant. And that is, notice there's only one statement as the result of an else here. All right, I'm going to write this in the notes. I'm going to put some notes here. When there is only one statement in the if clause or the else clause body, you can omit the braces, but don't do it. That is against our style guidelines for one. And also, if you leave them out, you can run into some situations that are ambiguous at best and wrong at worst. So even though technically I could get rid of these braces here. In fact, I'll do it just to show you that it does work. Let's say negative 20 meters, everything's fine. If I say 25.3 meters, everything works still. But again, don't do that. The reason I am telling you about this is because you are going to see a lot of code that other people have written and they will take advantage of this shortcut, even though it's a really, really, really bad idea. Let me save this file here real quick. Back to our pricing. Here, again, this is what is called a unary else, unary if statement. There's no else clause. Whereas what we have here in falling objects, this is what is called a binary if, as both an if and an else clause. Now, there are some people who just can't stand the idea of having a unary if. They just say, well, it just doesn't look right. It's incomplete. It's bugging me. If it is bugging you, I'll show you how to take care of that. Let's call this pricing one v.java if statement. Well, now we're going to have a binary if statement because we're going to have an else clause. Otherwise, I'm going to open up a block and inside of it, I'm going to put a comment. You want the comment there to let everybody know that you intentionally, really, really, you do want to do nothing in the case that the quantity is less than or equal to 1,000. And now, everybody's happy because they have a binary if. Still compiles fine. If I have $5.30 of them, no message. $5.00, 1,024 of them, I have the message. So yeah, you can write either unary or binary if statements. Most of the time, you'll be writing binary if statements. Most of your decisions that you make when programming have a natural this or that. Okay, I'm sorry. This is one of the things that I'm trying to find the right word to write here. So just to be able to let you know, you need to know about both of them. I can tell you right now that 99% of the time when you write your statements, you're gonna have an else clause. That's just the nature of the beast. Okay, let's go back to our pricing here and let's talk about what to decay. I'm gonna keep this here. Now, here's the first question. Remember, okay, well, let's back to do this here. Let's say my unit price, if I put in negative five and 3,000 or 300, okay, that's clearly ridiculous. I would like to have an if statement or an if else to allow me to say that the unit price has to be positive. So what we're going to do is as soon as we get the unit price, we're gonna say if the unit price is greater than zero, can we have a unit price of zero? I don't think it makes much sense. Then it's okay to do all the rest of this stuff. Now, I could indent all of this by hand, but it's a lot easier for me to select all the lines and press the tab key. And voila, Genian does the indenting for me. It keeps everything nice and neat. Otherwise, system.out.print line, price must be greater than zero. And let's go and look at this. So if my unit price is $5.34, I order 17 of them. Great, everything works wonderfully. If I say negative $5.43, then price has to be greater than zero and it doesn't even ask me for the quantity. I've got bad data right away. You'll notice something interesting here. Here I have an if statement and there is an if statement inside of it. This is what's called a nested if statement. So ifs can have ifs inside of them. How cool is that? I can, that means I can also do something here for the quantity. I'm going to read the quantity in and now I'm going to ask you another question. If the quantity is greater than zero, then it's okay to do all of the rest of this stuff. Let's try that again. I intended one line too much. Otherwise quantity must be greater than zero. Let's see what I can do here. Let's hide the message window. So you can see this thing in all of its wonderfulness. This is why indenting, by the way, is incredibly important. So we can see things as they are nested. So if the unit price is greater than zero, then it's okay to ask for the quantity. If that's greater than zero, then it's okay to do all the rest of my calculations. If the quantity is not greater than zero, by the way, you'll notice this highlighted in blue, this opening brace on line 23. On line 41, you will see the closing brace. Jeannie does that for you as it can help you see where things are. So if the quantity is not greater than zero, then we print out the message on line 42. And again, if the unit price is greater than zero, everything we can proceed. Otherwise, well, this is interesting. It doesn't seem to be highlighting anything. Ooh, it's highlighting this thing down here. That means I'm missing something somewhere. Am I missing something? Let's see, is it else? Oh, yes, I'm missing something. I forgot the closing brace here. There we go. Now when I do this if statement here, by the way, the compiler would have caught this for me. And there's the else that goes with this if. So let's say I have $5.34 and I want negative 20 of them. Can't do it. I have $5.34 and I want 20 of them. Everything's still fine. And again, my unary if is still working. If I have $5.34 and I want 1,567 of them, I still have to confirm that large order. This is what's called a nested sequence of if statements. But the next thing I'm going to talk about something called an if else chain. I'm gonna do a different version of pricing. I'm not gonna put in all of this data checking to check if our unit price and quantity are greater than zero. I might add that later, but I don't wanna add too much to one program because then things get really hard to follow. So let's look at pricing too. Now this one's quite different. We have a unit price and quantity and I wanna figure out the subtotal, the tax, and the grand total. You also get a discount before tax based on the quantity. So if you order less than 10, you get no discount. If you go between 10 and 30, you get 3%. 30, but from 30 up to 70, but not including 70, you get 8%. And if you order 70 or more, you get a 12% discount. And here I have put in all of these levels. So here's the quantity level that you have and the amount of discount that you get. Level three is greater than or equal to 70 so it doesn't really matter. There is no upper limit. Now here's my plan. I think this one deserves being planned out. We need to concentrate on asking for the quantity. So after we get the quantity, if the quantity is less than 10, then we're going to set our discount to zero. Otherwise we have another question to ask. Well, what if the quantity is less than 30? Then we're going to set the discount to 3%. If it's not less than 30, then it must be greater than or equal to 30 in which case we're going to ask, well, is the quantity less than 70? And if that's the case, we're going to set the discount to 8%. And if it's not less than 70, it's greater than or equal to 70, it's our last case. We don't have to ask. We don't need to indent one more level, luckily for us. Otherwise we're going to set the discount to 12%. So we're going to be marching off to the right-hand side. That's my plan. Notice we have a lot of nesting going on here. I'm going to write this the long way, exactly the way I have this here. So after I get the quantity, I ask if the quantity is less than the level one quantity, well, first of all, we're going to have to have a variable for our discount, aren't we? Let's call it discount percent and let's set it to 0.0. If we have less than level one, then we set our discount percent to be level one discount. Otherwise we have another decision to make if the quantity is less than level two quantity, I'm sorry, I have level zero here. I lied. Oh, this is going to be a lot of typing. Bear with me, okay? So we have level zero discount, which is zero. Perfect. And level one discount, level one quantity, then our discount percent is going to be level one. Discount. And if that's not the case, then we have to ask you another question. Well, maybe is the quantity less than the level two quantity? And in that case, we're going to set our discount percent to be the level two discount. Otherwise it's more than, oops, otherwise it's more than that. And I set my discount percent to be level three discount. And again, notice the genie is very nice about keeping the indentation all straight for me. Wow, that's a lot of stuff to do, but that's okay. So now what we need to do is we need to say our double discount amount is going to be the subtotal times the discount percent. And our double discounted subtotal is going to be the subtotal minus the discount amount. Then we're going to use our discounted subtotal for the tax, because remember we calculate the tax after we take the discount, not before. And then our final grand total is going to be the discount subtotal plus the tax. Now we have to print all of this stuff out here. Oh, this is going to be fun. So we're going to print out discount and that's going to be our discount percent times 100 because remember all of our discount percents are decimals here. Then we'll print out our, let's call it new price, I guess we'll call it, which is going to be the discounted subtotal and our tax and total remain the same to print out. And boom, oh, lovely. We have a whole problem here. Yes, I put these in the wrong place. These do not belong outside of Maine. They belong inside of Maine. Let's see if that compiles better. And now finally, I can't find something called discount subtotal. Why not? Oh, because I call it discounted subtotal. Fine. Actually, what I do want to, I just realized I want to show you your discounted price, don't I? Don't want to show you how much you saved. Do I want to show you the discount amount? Tell you what, I'll do that after I print the total. All right. So now let's try this. Let's first of all do $5 and buy nine items. There should be no discount. And the discounted is zero, which is correct. We are new prices still $45 and you save nothing. By the way, this discount should be a percent, shouldn't it? Let's compile that. So again, $30 by five of them, and I don't save anything. That's correct. Now let's say I have $5 and I buy 10 of them. I should get a discount on that. I get a 3% discount, which gives me $40 and $8.50, because I saved $1.50. What if I buy $5 and I had to buy 40 of them? Then my discount is 8%. I save $16, which is indeed 8% of 200. And let's buy 80 of these. I get 12%, which saves me $48. That's not bad. And so this is now all working. It's not lined up quite as nicely as I would like to, but I'm not going to spend a lot of time on that right now. This works. There's nothing wrong with what we've done here. However, some people might say, oh, I don't really like the idea that we're marching off to the right-hand end. What happened would happen if I had seven different levels of discount that I'd be marching further and further to the right, and it would just be really ugly. So when you have a sequence of else followed immediately by an if, there is a shortcut that you can use. Remember how I had that said earlier in my notes? I said, don't do it. It's against our style guidelines, except when you are building an if, else, if chain. Whenever you see yourself writing something like this, let's save this as pricing to b.java, which means I also have to change this. Since this else, all of this can be considered one big if statement, in which case I can omit these braces and put the else immediately with the if. Then I can unindent this. Similarly, I have an else immediately followed by an if. I can put them together and line things up nicely here. And then this else, it doesn't have an if after it, but I certainly can unindent everything. And then I don't need these two braces here. So I'm taking the advantage of the fact that if is a statement and I can line up all my else ifs, and it looks a lot nicer. Now if I had seven levels of discount, I wouldn't be marching off to the right. They would be lined up one underneath another very nicely. And does this still work? Well, let's find out. Again, $5, I buy 15 of them. I get 3% discount, $5, I buy 35, I get 8% discount, $5, I buy 100, and I get 12%. So this is called an if else if chain. Only one of these will happen. The first one that matches is the one that's going to be executed. I'm going to open this pricing to a pricing 211.java. Pricing to, excuse me. I'm just curious about something here real quick. In fact, let's do this. Let's save this under a different, I just went with sometimes, even though you are working with Genie, your indentation can get really messed up if you start copying and pasting or if you start making changes by hand and don't listen to what Genie says. So let's call this pricing bad indent.java. And we're going to call this pricing bad indent. And we're going to do is we're going to, remember how I moved all of these over? Well, let's say I forgot to do that and left it here. That would not be good. And let's say for some people, I've seen them do this in previous semesters. They'll indent that just because they think it looks better even though the indentation is not quite correct. And let's basically fool around with the indentation so that it looks pretty weird. Now, notice by the way that Java doesn't care about any of this. So for example, I want to take these two and indent them further. And then let's compile this. Still, if you ever end up with a mess like this and you can, it's easy to have that happen unintentionally but you don't want to have to get it all rearranged by hand. Let me show you a terrific little website here. Let's see, Java online indent. The online Java formatter. What I'll do is I'll get rid of everything on the left-hand side, paste this in and then I'm going to say beautify. And here on the right-hand side, everything's nice. It's all indented, quite well indeed. Now let me come here again and grab the URL and let me put that in the notes here for today. Now that I have shown you this, there is no real reason for you to ever give me a program that is not indented correctly by the way because it's the matter of only maybe a couple of minutes. Copy, beautify or a copy and paste, beautify, copy and repaste. So please take advantage of it. If you ever see that you're indenting is just totally out of control and it happens. It's happened to me a lot. And you say, oh my gosh, how am I ever gonna fix it? Don't do it by hand. Put it into the Java formatter and it will get everything formatted for you and you will be happy and I will be happy. Okay, let's see where we are here. That is the relational operators. Very quickly, there's one thing that I didn't talk about. I've used greater than and I've used greater than or equal and less than or equal, this is not equal. Now, this is really important and before I end the mini lecture, I definitely have to talk about this. If you wanna compare to see that two things are equal to one another, you use two equal signs in a row. All of these relational operators are asking questions. This asks, is it true that X is greater than Y? Is it true that X is less than Y? And this is asking the question, is X the same as Y? Do they have the same value? So for example, let's say I have an integer X as 37 and an integer Y as 39. This is the single equal sign is the assignment operator. If I say X double equal sign, Y. Comes back false, X and Y do not have the same value. If I say X becomes Y, that changes X. And now if I say is X the same as Y, we're gonna true. If you accidentally mess this up in an if statement, for example, if I say if X is equal to Y and I use only one equal sign, same. Otherwise, system dot print. And that should be a print line, but yeah, I need to have a closing brace then. Man, you'll get an error. So if you use the wrong operator, it will tell you. But if I come here and use this and ask the question, hey, are X and Y, do they have the same value? I don't know what happened there. Hit the wrong arrow key there, I suspect. Okay, let's try that again. And let's put these in as print LN. JShell is pretty nice, let's edit your mistakes out. And I use, again, up and down arrows to get to other things that I've typed in. Then everything works great. I'll put that in the notes here. These are always gonna give you back a true or false answer. And by the way, the true and false are called Boolean values named after the British logician, George Boole. And you can look them up on Wikipedia. In fact, here in JShell, whenever I do something that involves a relational operator, I'm always gonna get a true or false back. So it's three less than five, that's true. Is three less than zero, that's false. Is five greater than or equal to five, that's true. Is five not equal to four, that's absolutely true. Five is indeed not equal to four. Okay, so tomorrow, I guess I'm gonna talk about the switch statement and logical operators. And I might get into the De Morgan laws briefly because they're sort of interesting. But I think I've given you enough to think about here. If you want something, an interesting challenge, take this pricing two B where I have all the discount and add the stuff to make sure that the unit price is greater than zero and that the quantity is greater than zero. So we're gonna have ifs inside of ifs inside of ifs and it's just gonna be one great if festival. And see you all tomorrow.