 Hi, time for the mini lecture and I'll share my screen and let's pick up where we left off when we left off yesterday We had this program where we're using variables and it's okay. It works as advertised But if we want to change somebody to having a different age We have to have them change the program and that's not really ideal I guess we said yesterday at the conclusion what we'd really like is we'd really like to be able to ask the user Hey, how old are you? And then when they tell us we use what they tell us in the calculation In order to do that we need to do input and we do input with something called a scanner Now it turns out that Java has a lot of little libraries of code to help us do stuff Not everything is built into Java one of the things that's not built into Java is a way to do input so we have to say Import and we're going to import a library from the Java util library package And it's going to be called capital S scanner Your import always always always goes at the beginning of your program Now what we need to do is we need to connect the scanner to the keyboard so that the scanner knows Hey, we want to do input from the user's keyboard The way we do it is to create a scanner object and we'll call it input and That's going to be a new scanner based on system.in System.in is A special object that's built into Java that is connected to your keyboard So I'm saying take the keyboard and Make a new scanner based on that And save it in a variable called input Remember just like I have integer years I give them data type the variable name and the value My data type is scanner My variable name is input and its value is this new connection to the keyboard Now what I'm going to do is I'm going to system.out.print and not go to a new line How many years old are you and then here instead of putting 48? I'm going to say input.nextint and that tells the scanner input To read the next integer that the user gives us Whatever that integer is will go into years Then we'll use those years times 365 to calculate the days and print that out let's compile that and Let's run it and Now it says how many years old are you and it's waiting for my input? So if let's say I have somebody who's 23 years old they type 23 and press enter and It calculates their age and years and days excuse me. Let's run it again Let's say somebody comes in who's oh, I don't know 19 years old and then somebody else can run it and maybe they're 80 years old and That'll tell them approximately how many days old they are So now we have a very nice generic program that will work for anybody a couple of things to note here I used print here instead of println gee. I wonder what would happen if I did use println let's compile and run it and Now my input cursor Where I put the age is on a separate line from the prompt the By prompt. I mean the text that tells the user what to input There's nothing particularly wrong about the program will still work fine But it's ugly. I always want the cursor to be on the same line as my prompt Now you'll also notice I put a blank here after the question mark. Why did I do that? Let's take it out and see what happens and by the way, I'm gonna go back to the same line here and again the program still works But again, it's a little bit ugly This number is too close to the question mark and That's why I'm putting in this extra blank here because that gives me some breathing room for my cursor And that's how we do input. There's one other thing I want to do here I created the connection to the keyboard when I'm done with my program I'm going to say input dot closed which is closes down the scan It breaks the connection with the keyboard You don't have to do that when your program ends the system will automatically close it But the reason I have put it in there is because on some integrated development environments If you don't say that you are closing the input, you'll get a little warning message It'll say you didn't close your input. Yeah, okay fine. So this explicitly closes it That's input Let me take a quick look here at what else is in chapter three here Oh literals and constants. I think we talked about that earlier namely, um Did I use constants here before last time? I don't remember Oh, yes, I did cool. Okay. So what I might want to do just for grins. I might want to say um final integer Days per year becomes 365 and then here Days per year Yeah, that's what makes things a little bit nicer Cool, let's go with purchase dot job. And now we want the price of the item Well in this case, we don't want it to be always 12 95 So we're going to say in this time. Oh, we're guess we're going to need our scanner again, aren't we? We're going to have to import java dot util dot scanner And we're going to have to put our scanner in here And I'm going to put in there scanner Input becomes a new scanner object connected to the keyboard And then when I'm done with my program I'm going to close my input You know what? I'm going to be doing this a lot because I'm almost always going to be asking for input from now on Time to change my template That way I don't have to retype this every single time and And just for cleanliness. I'm going to get rid of some of these extra blank lines that aren't helping me any And I just want to compile this to make sure that it compiles. Okay, and when I run it Yeah, it doesn't do anything, but that's cool. I've got a good program here Let's go back to purchase now in this case First of all, and now that this is an error that I see a lot. I'll say okay. We want input dot next double And let's compile that And run it and now it's waiting for input, but I forgot the prompt So how do I know what I'm supposed to enter? So I always remember to put up a prompt So here let's go and put here System dot print Enter the item price Now here I'm not putting the extra space and there's a reason and you're going to see why Because I want the Numbers to be right after the dollar sign Although if you do put the blank in there, I'm not going to complain If you want to be consistent and always put a blank in there if you think it looks better this way I'm perfectly happy with that So there we have our input I'm putting in some blank lines here. So the things that belong together are grouped together There we go. And let's see what else I need to talk about here Oh typecasts and error messages and a whole bunch of other stuff. Oh my goodness. Okay Formatting output though. That's important So let's go on to formatting output here. Um, when I run this program You'll notice I get this really ugly business here I would like to have things as exactly two digits after the decimal point The way I'm going to do this instead of using print l and I'm going to use print f So I'm going to Actually, let's let's stop here for a moment I'm going to save I'd save this under different names so that we can have purchase with format dot job Purchase with format We're going to use print f which means print with formatting And what we do is our first Argument the first thing that we call put inside the parentheses is going to be a format string What we're going to do is we're going to say we're going to want the words your item cost and a dollar sign And then we're going to say I want a placeholder a fill in the blank and a percent sign introduces a placeholder Then we would say how do we want that placeholder? Formatted when we print it out and the answer is we want dot two Which means two digits after the decimal point and f because this is a floating point number Then we are also going to say Percent sign n which means new line So let me put a comment in here for that percent sign dot two f means floating point number with two digits after the decimal And percent sign n means Give me a new line The reason we have to do that is because print f does not give you a new line. It's not like print lm. It's more like print So if we want a new line, we have to put it in ourselves Let's compile that and run it try compiling it. There we go And running it and now by the way if I say something like 34.1 Notice it gives me my two digits How cool is that? And we'll do the that comes out to we're going to need to also another percent sign dot two f Percent sign in we'll put a period here This period here is not part of the format. It's a period in the sentence So here's our format string. We'll change this to a print f says that comes out to this And then after the format string comes the thing we want to print Let's compile that And if we have 34.95 cents notice We get only two digits after the decimal point Now I need to work on this second line here And we're going to again say percent dot two f percent sign in comma tax Now what happens when I compile this? It compiles successfully But when I run it I get a conversion mismatch exception This is what's called a runtime error What I did in java we didn't have a syntax error We've seen those before like if I leave off a quote mark or if I put in too many parentheses The compiler will stop us right away. We won't even be able to run the program Oh, let me show you an example of that So if I had accidentally put a closing parenthesis here And compiled it it would say well, I'm not even gonna let you run this program dude. It's wrong Yeah, but everything I've done here has been syntactically correct the grammar and spelling so to speak in java is perfect The problem is when I try to run the program It can't run it because there's some problem when I run the program and you get this thing called an exception And we'll talk about exceptions much later in the semester Now some of these are very Weird and confusing and you're going to see a whole bunch of stuff here And one of the things in there is always going to be your program So it says okay this happened in purchase with format It happened in the main method And it happened on line 27 And the problem is I can't format this correctly And the reason is on line 27 here Because remember I said percent sign introduces a placeholder and then I have to follow it with what kind of thing there is Oh knows I have a percent sign here and there's nothing to follow it. It thinks I have a placeholder So the question is how do I get a real honest to gosh percent sign? And the answer is I put two percent signs in a row, which means give me a percent sign This is not a placeholder So if I put two percent signs in a row here Now when I compile this And run it For 34.95 cents I get my seven and a half percent there Which is exactly what I want and I get two decimal points for everything, which is again exactly what I wanted You know what? This seven and a half remember. Oh, wait a minute. It's not seven and a half. It's seven point seven five Oh bummer We want to print The tax rate and the tax here. So now I have two placeholders My first placeholder Will be filled in by tax rate These percent signs are not a placeholder And my second placeholder this percent dot 2f Will be filled in by the tax. Oh, we had a problem here. Okay Good. I'm glad I made this. Oh, this is perfect. This is exact I did not do this intentionally but boy did this work out great Wait a minute. This is 7.75 percent and it comes out as 0.08 percent. What the heck went wrong here? What I have here is called a logic error. In fact, let me write these three kinds of errors. We have a syntax error That's sort of like grammar spelling And that is a things like missing semicolon too many parentheses, etc Compiler catches these for you We have a runtime error and that is The syntax is correct But when you run the program Java can't do what you told it to do That's when we had that percent sign hanging out in the middle of nowhere. Java couldn't handle it And the last kind the hardest one to fix is a logic error The syntax is correct Java can do what you told it What you told it Isn't what you really want to do 7.75 divided by 100 works out to 0.075 But I don't want 0.075 percent. I need to multiply that by 100 That's a logic error I'm printing the wrong thing I don't need the tax rate because I'm printing a percent sign instead of a decimal here I need to multiply by 100 to convert it to a percent That's the whole definition of percent So there's my logic error Which I now think I have corrected I have $50 And there's my 7.75 percent tax. Yes, it works The last thing I'm going to do to this program is I'm going to make this one long print one print f instead of a whole bunch of print fs but appear here with percent dot 2f Tax of dollar sign percent dot 2f percent n That comes out to percent sign dollar sign percent dot 2f Oh, I don't need a percent sign in here Yeah Now I have a lot of placeholders. So I have to fill them all in The price is the first one The tax rate times 100 Fills in this The tax that I calculated here on line 19 fills in this placeholder And The total fills in my last placeholder To all this And my period is outside of the percent sign in I screwed that up a little bit here The percent sign has to come at the very end There we go. That'll that'll that'll fix that and that's formatting. Oh gosh, I guess we can do some formatting here as well So instead of using all these plus signs, we can say print f your age of percent sign d Years is about percent sign d days old and then a percent n And we're going to fill that in with the years and the days in this case Percent sign d is used for an integer Now there's a lot of other stuff that you can do with formatting And let me see if I can find it here under pages View all pages Actually, I want to go back here. Let me just check something here real quick I want to do this in student view. I want to make sure if sure it works for the student So if I go here to pages Yeah, okay, you can view all pages too. How wonderful Somewhere down here we have Ah, let's see. Where was it here? Formatting output there we go And this will give you another recap of this So if the you got confused by my mini lecture, no problem you can Go through here and you can read it at your leisure Now this is using backslash in instead of percent sign in So I may as well put that in here to let you know Let's do this here backslash in Also gives a new line But percent sign in is preferable So I I wrote that page about formatting output before I learned about percent sign in So this is wonderful. That means I now have come back and covered this thing in chapter two about Compiler error messages and other types of errors knew how I would be able to find that eventually We've talked about the remainder operator. I talked about reading error messages here um And finally we want to type a cast operators So let's do this Let's um, oh, let's use our template now to have my template. I can use that hooray And let's call this average sale dot java And here's what we're going to do. We're going to ask the user how many people Bought items And how many items were sold total And then we will print the average number of items per person Which we want to have decimal places in So there's the purpose of my program And because I called it average sale dot java. This has to be called average sale So we're going to say system dot out dot print You know what no stop stop stop stop stop Let's plan this program. Let's write down the steps that we're going to have to do Step one is prompt the user for the number of people Read it into a variable Which we call it uh, how about call it n people number of people Not the best variable name of the world, but it'll do Then we need to prompt user for a total number of items sold Step four will be read that into a variable Called items sold Now what we need to do is we need to calculate the average As items sold divided by n people And then print the average Properly labeled So those are our steps now you're saying oh my gosh, why are you going through all this Fancy schmancy rigamarole For a simple program like this and the answer is because I went y'all to get in the habit of planning your program before you write it So there's six steps. We have to go through here Okay step one prompt for Um the number of people how many people bought items And then we're going to have an integer called number of people becomes uh input dot next int Then we're going to do our step three How many items total? Did you sell and then we're going to say our integer? This is step four items sold becomes um Input dot next int also Now i'm going to have an average which is a double because remember I went to have the decimal point double average Is going to become items sold divided by the number of people and then I can say print f average items Per person is percent sign dot one if i'm going to have one digit after the decimal point And then a period to end the sentence a percent n And i'm going to fill in the blank with average And let's compile that And let's run it And so how many people bought items? Let's say I had five people bought items And they had 17 items so so total Uh, no, that's not right. I don't want 3.0. It should be 3.4 I believe Yeah, because I want an accurate average So the question is How do I get that to happen? Here's my problem. This is an integer and that's an integer. Remember 17 divided by five is three Not 3.4. And that's why I lost even though i'm assigning it to a double on the left hand side Remember this happens first So i'm taking integer 17 divided by integer five, which gives me integer three And that gets promoted to 3.0, which is not what I want So the question is how do I make this a double? Here's one way to do it I could say 1.0 Times items sold divided by n people now because I have a double times an integer that gets promoted to a double And as soon as I have a double Everything else is a double So I have 17 people five Okay, I got I got those backwards. Okay. Let's try that again Uh I have five people buying 17 items. There we go. And now I get my 3.4. Cool. That's what I wanted Then that's one way to do it Another way to do it is something called a cast and this is in the book here in chapter um three called type cast operators And what we're going to do here is we are going to say This is going to say take items sold and convert it to a double Then when we divide it by an integer, we're going to be cool. Everything's going to still come out as a double I have people with 17 items and 3.4 I could also do cast them both It wouldn't hurt All right, so I can take the double items sold divided by double n people Yeah, so it's like people with 17 items Now once I've got a double by the way, I do not have to do casts anymore I don't have to say double average. It already is one. So don't go overboard with these casts, please Now you've got to be careful. I'm going to tell you this right now If you do this, then all of a sudden that undoes all your work Because what happens now is I'm going to do the items in parentheses first, which will be 17 integer divided by Integer five, which is integer three and then I'll promote it to double 3.0 Which is not what I wanted Then I'm right back where I started So be careful when you use casts that you cast the right things Speaking of which, let me go into here Jay Shaw The other kind of you can cast things to other data types. For example, let's say I have a double x and I said to 37.9 And I say int um Whole number is going to be int x That says convert x to an integer. This is again I'm casting x to be an integer and that will not round instead what it will do is it will get rid of the decimal part entirely So here's a cast to integer and here I have let's say um integer a is 12 integer b is uh, let's say seven And then I can say double c Is double a divided by b Again, I don't have to all as long as there's one item That's a double the whole thing is going to be a double but what the heck So here I'm casting to a double to make everything work out, right? I think this pretty much covers what we need to know in chapters two and three Um, you may want to look at this thing called the scanner bug. It's really not a bug. It's just the way the scanner is built Read this at your leisure. It's not going to come up in any of the in the our assignment Let me just do a real quick look at what the assignment is though And the assignment is going to be variables so Here we have a staircase made of concrete that's shown in this diagram There's four steps There's the width The run and the rise those are the dimensions that you need to know when you're building a staircase And we want to know the volume of this staircase because we're building it out of concrete How much concrete will we need? We're gonna need a cast by the way here um To figure out the volume what we want to know is that we we can figure out that the volume of one of these Blocks here like this bottom block is the width times the run times the rise That's the volume of this bottom block Now on the second step, we have one block two blocks, don't we? And on the third step, we're going to need three blocks stacked on top of each other and for the fourth step We're going to need four blocks So we're going to need four plus three plus two plus one which is 10 blocks altogether So the question is how many blocks the size of the bottom step do you need to build a staircase? And you can draw a diagram that will really help you If you have n number of steps The total number of blocks is the sum of the numbers one through n if I had five Steps in the staircase that need one plus two plus three plus four plus five for the fifth step To get the sum I can calculate it with that would be five times six which is 30 divided by two I need 15 blocks So here is your But the program output might look like You're going to ask the user how many steps in the staircase you're going to need to use it a scanner They'll tell you that now is that going to be an integer or is it going to be a double? Well, my question is can you have 3.79 steps in a staircase? Probably not so we probably it's going to be an integer For the width the rise in the run in centimeters could those be double or the integers? Well the answer is here since I'm using 7.5. Yeah, I should expect to have decimal points or double for the rise the width in the run And then the total volume is going to be an integer And there's some style hints here for doing user input and talks about doing the prompts Printing blank lines again and blank lines and source code So you'll definitely want to read this so that your style is nice And the reason you want to do that is because That's worth two points is your programming style And that I think is going to do our Mini lecture for tonight What I'm going to do tomorrow if I have a mini lecture is I might just do a couple of programs From the extra exercises book or I'll just make up some new exercises So that I will practice these things and by the way, you should practice these things as well And I'll see you all around