 OK, welcome to AP Computer Science Introduction. This is basically a video series dedicated to learning a little bit about the topics that will be covered in AP Computer Science. And I'm making this for my students. So let's get started with Unit 1. And basically, I'm just going to show you how to write a basic Java program. Java has a very specific way that a program has to start out. And you'll see that it might be a little complicated. You might not understand what's going on. But just for now, just kind of copy what you see. And you'll note here I'm using a program called Jgrasp, which is a free Java development environment. It's very, very useful for beginners. And you can download it for free. Just do a little googling for Jgrasp, where I'll put a link down below in the comments. So first, everything in Java is a class. Don't worry about what that means right now. But what you need to know is that you need to start out your program with class. You need to give every class a name. And since this is Unit 1's materials, I'm going to call it Unit 1. You cannot have a space in there. It's not going to work. You cannot start this with a number. But you can have letters and numbers mixed. And one thing to note is you see how I capitalize this. This is a Java preferred format. So I'm going to put a curly brace here. And I'm going to go down a few lines and put a closing curly brace here. So this brace matches this brace. So everything between these braces are part of that class. I'm going to hit Tab. And Jgrasp adds three spaces here for a tab. So we can go with that. And the next thing I need to do, since I actually want this to run some code when I actually execute it, we need something called a main method. So I'm going to type public static void. And again, for now, just memorize it. Main string square brackets. And then args. And then again, I'm going to go here and here. Now you might also see the formatting done like this way. Put the curly brace here. But since the AP materials do it this way, I'm going to do it this way as well. So I'm going to show you your first command. System.out.println. And the stereotypical thing to type is hello world. And if you type it all exactly like that, now what I want to do is I want to save this first. So I'm going to hit Command S. And we'll just leave it the way it is. And I'm going to say, notice it went unit1.java for me. So what it says here, unit1 has to be the exact same thing here. You cannot change a name. You can't have different names. So if you want to change the file name, you have to change the class name. This is one of those funky Java rules. So I'm just going to save that. And one thing you might want to make sure is that your character set is set to UTF-8. My computer, I had to change that and basically go with the rest of the default. So I'm going to hit Save. Now to run my program using Jgrasp, you look up here and there is a little running person. And you can just click that. It says find and run main method or applet. So it's going to find and run this main method in the class unit1, which is what's open right now. So I'm going to click that. And then the output actually shows up down here and actually scrolled off. So let me move that up a little bit so you can see that better. And you can see, Hello World came right out for us. So this is a very minimal Java program. So this is what you're going to need if you're going to start writing some Java code. You need a class. And notice the text highlighting gives you a hint as to what words are important here and what words are keys. So you have a class. We named it unit1. Could name it something else. But we're going to call this unit1. And we have then our main method, which says public static void main. Now this is not going to make any sense to you at this point, but just go with that and you'll learn what all that stuff means later. Same thing with strings and brackets and args. And then here we have our main method, which starts here and ends here. And we're using the system class without and the print. I say print line and print line means print a line. And then we put some text inside some quotes. It's actually called a string. And we have a semicolon at the end of the line. So our first section of the AP computer science program is called is 1.1. And it says, why Java programming? And probably, at least for the purposes of the AP program, they use Java. So we have to use Java. And there's a lot of things we could talk about Java. But it does have a lot of really good things to note about it. It's very fast. It is cross-platform. And there's a lot of technical details going to making that work. It does something called automatic garbage collecting, which was something that, say, something like C++ doesn't have. But again, for beginners, you don't really need to worry about those things. But just watch what we do here. And just remember the basic format. So now what you might want to do at this point is even just maybe, if you're typing along, is delete that. And then just save this as just a random file. And then we might want to call it template. Save it as template.java. And that way, each time, you can start from here instead of having to type it over. Now that said, since you're going to be an AP Java computer science student, you might want to type it out every time. Because on the test, you're going to have to write it out from memory. So it's probably good practice to be able to do that. So again, you see the double forward slashes here? This is a comment. And this is 1.1, why Java programming. And that's the first basic unit or section of that unit. And so I'm just going to go here and show you how to print some things out and just some basic things with the way printing works in Java to get you started. So as we said, system.out.println, print line. Now this is also something you might want to just copy because we're going to be typing this over and over again. And you notice this does not have code completion, which a lot of IDEs do. Again, for beginners, because you're going to be doing this on a test in a written format, you might not want to rely on auto-complete. You might want to type it out yourself to get that muscle memory to get it into your heads. I'll leave that up to you as to how you would like to do that. So put our hello world back in there and let me be comma. And then let's do a couple more lines here. Let's do print and say giddy, giddy, and give me, actually. And that's Japanese because I work at a school in Japan. So I use some Japanese stuff in here from time to time. Giddy, giddy. Now watch what happens when I run this. OK, I got an illegal, oh, duh. So you guys probably figured that out as I was typing it. It's actually harder to type and to talk than it might seem. So I'm going to run that. OK, so we have hello world and we have giddy, giddy. Now notice there's a space here in the code, but there's no space here. OK, so the spacing in the code has no impact on the output. So notice, now watch what happens when I type this. Print and print. So I'm going to run that. So now instead of print ln, print line, or print line, it's just print. So you notice instead of, so let's say, what print line does, it prints and it goes to the next line. So here we have print, giddy, and it doesn't go to the next line. So then we have giddy again, and then that does that. Notice there's no space. So if I wanted to include a space, I could put it here, or I could put it here. Let's run that again. So then that's the way you can print different things out in Java. Now here I might want to put a print ln or print line because I don't want my next thing to be at the end of that. So then I'm going to go keep adding some printing stuff. So I'm going to paste because I'm a little bit lazy. I like to do it that way. So I'm going to put a blank line there, and I'm going to print out the following. Now watch what I do here. I'm going to type string name equals Mr. Thompson. That's my name. And it's not my colon. So what this name is is a variable. And so what I've said is I've assigned this data to the variable name. Now it's called a string. Any type of textual data is referred to as a string. Do you memorize that? It's a common amongst programming languages. That's what we call them. And then what I want to do is I want to print it out. So I'm going to say name. And let's watch what happens. OK, so you can see we've got hello world. So I'm going to move that up a little bit again. We've got hello world, giddy, giddy. And then at the end of giddy, giddy, we had print lin, so the space. We printed an extra blank line. Oops, I actually printed a second blank line. That was my mistake. And then we've got string name equals Mr. Thompson. So name. Now if I want to print this along with some other text, it's very, very simple. I'm going to type name, quote, and I'm going to put up. Now I don't need to put a space here, but I like to put spaces. It makes it a little bit easier to read. So notice there's a plus here. This is called string concatenation. And we'll get more to that later at some point. So I'm going to run that. Let's see what the output looks like. OK, so you can see pretty Mr. Thompson by itself. And then here, name, space. So we've got name, sorry, name colon space. Notice that is inside of quotes. And then name, which is not inside of quotes, it becomes Mr. Thompson. Now if I did this like that, what do you think is going to happen? OK, so you can see it printed name. So it printed the exact same characters. So anything inside of quotation marks, double quotation marks, by the way, not single, it's an important point, will be treated as textual information. Next up is 1.2, which is about variables and data types. So one thing you'll find out about programming languages in general is that data comes in certain types. The type of information that you use is very important. Some languages, they're not too strict about it. Other languages like Java are very, very strict about it. So here are the basic data types that we can use. Now watch what I did there. So we've got Boolean, we've got int, we've got double. Now there are other ones in Java. I'm not going to really talk about them here because they are not included on the AP computer science test. So we're going to focus just on the subset of Java that is included in the AP computer science. So a Boolean value is basically one of two values. It is true or it is false. An int is an integer, and that is a number without a decimal. Then a double is a number with a decimal. So for example, watch what I do here. So a Boolean is sleepy, and I'm a little sleepy, a little jet lagged. True. So a Boolean can only be true or false. There's no other possible value for a Boolean. These values are also sometimes referred to as primitives because they are the basic data types. And I'm going to make an integer. I'm going to say the answer equals 42. And if you read Hitchhiker's Guide to the Galaxy, you know where that's from. If you haven't read Hitchhiker's Guide to the Galaxy, stop the video, go get it, and read the book, and come back to us. Enlightened. Next, double. So I might say class average equals, let's say, we're going to do a good class, 93.7. So if you look here, we have variable name. Notice again, no spaces. Notice this is two words. So the first one is lowercase, and the second one is uppercase. This is known as camel casing. So let's say if I want to add is user sleepy, so capital U, capital S. This is the convention in Java. You don't have to do it. This will work just fine, but your code will look a little bit weird. Notice, same thing, lowercase, uppercase, lowercase, uppercase. So please use that convention. Notice what the Boolean I put is because is has a, basically the same thing. Either it's either true or false, yes or no. So that helps us to understand what type of value that we have. So I could then, if I wanted to, I could go ahead and print these out. Let's say print out, quote, the ultimate answer to life, the universe, and everything is, dot, dot, dot, quote, plus the answer. And say that, I'm gonna run it, see what happens. Okay, the ultimate answer to life, the universe, and everything is 42. If I wanna put a little period at the end to make it look nice, plus quote, period, quote. Okay, notice how the text has a double quote before, double quote after. If you forget it, you'll see, let's try and run that and see what happens. You get an error. Okay, so you see line 27, unclosed string literal. So let's see if we can figure out how to add line numbers. Okay, so let's add the line numbers. So we can see where our errors are. So I'm gonna go ahead and fix that. Okay, next up, what we got next. So declaring variables. Okay, so I just declared these variables here. So basically what we need to think about is when we do a variable, is we need to tell the computer, or tell Java in this case, what type it is. So notice this was a Boolean, so I had to put a Boolean value here. This is an integer, I had to put an integer value here. This is a double, I had to put a double value here, which, you know, a decimal number, okay? So variables, let's take a look here. So let's say I have an integer, x, and it equals 11. Okay, now again, I don't have to put spaces here. I can do it that way. I just think it looks a little bit nicer that way. So I could say, and here I could say, let's see, x plus x. Okay, as we saw before, this is literal text, and this is gonna be the value. Now, I only declare it one time. Okay, so now if I wanna change the value, 22, and I just, same thing, I could copy it, x colon quote plus x. So let me run that, see what happens. Okay, so we see x is 11, x equals 22. Now if I try to do this, type int, let's see what happens. Okay, see, we get an error, line 32, it tells you where the error is, so trust the compiler. Say variable x is already defined in the main method. So we've already said it's an integer. So if we wanna change it, we don't need to do that. Alternatively, we can also do this. We can type x, we can do it on two different lines. We say x is an integer, and then we change the value. So let's run that, should see no change. Now let's try this, I'm curious to see what's gonna happen. I think I know, but I've been surprised before. So what happens, we didn't give x a value here. So let's see what happens. Okay, so we got a compiler error, says variable x might not have been initialized. So it is giving us an error, a little warning, and it's saying that, okay, you declared it, but you didn't give it a value. Okay, now my understanding is the default value is zero, but maybe our system does not let us do that. So that is how that goes. There's also something you might see called final. So let me talk about that real quick. So final, okay, so in the case up here, we had x equals 11 and x equals 22. We actually were able to change the value. Okay, so we said x is 11, then we said, okay, x is no longer 11, x is now 22. But if we wanna do something where we wanted a value that never changes, we use the final keyword. So we can say final int, and I'm gonna say speed of light equals 299, 792, 458. And that is the speed of light in meters per second in a vacuum. So if we try to print that out, oops, I didn't need to do that. So speed of light, okay. Now notice these are in capital letters. This is a convention in Java programming. If something is final, we use capital letters. It tells us as a programmer, this never changes. Now, we didn't have to do that. It will still compile, it will be fine. But this is the convention, this tells other programmers, this is something that does not change. So let's run that and see what happens. Speed of light, it comes right out. Now let's try and change it. So speed of light, let's say, we discover Einstein was wrong, I don't know. And speed of light is not constant, or we want it to round it for whatever reason. Let's run that and see what happens. Okay, we get an error from the compiler. It says, cannot assign a value to final variable. So we are trying to change a variable that has been declared as final, and we cannot do that. Okay, so let's undo that. And move on to 1.3. 1.3 expressions and assignment statements. Okay, so in this one, we are looking at using mathematics, mathematical symbols, okay? So I'm gonna say x equals 10. Now if you recall, why didn't I put int here? It's because I already declared it up here. So if I didn't have this line, I would have to put int x equals 10. But since I did, say x equals 10, I'm gonna say int y equals five. I'm gonna say int z, or z, depending where you're from, equals zero. Okay, so what I'm gonna do here, I'm gonna say z equals x plus y. And what I'll do is I'll say, press the system out and x space. This is plus, actually, that's not what I wanna do. I wanna do x, quote, plus. This is where it gets a little confusing. x plus y, plus, quote, equals, because I wanna see the equal sign. I'm gonna put space on each side, plus z. So notice what I did there. Say in the variable x, I'm gonna print out a literal plus sign. Oops, that's that, didn't I? Plus y equals z. So 10 plus five, it should give us 15. Let's run that. Okay, so we see 10 plus five equals 15. So what I'm gonna do is I'm just gonna copy that, again, because I'd like to be efficient, and not repeat myself. I'm gonna do plus, minus times, and I do an extra one. And we'll do divide as well. Oh, and there's one more, I think. We'll do the modulus as well. See here, my planning sheet. Let's see if we have enough. Okay, so we have x plus y, and then we have x minus y, and I'm gonna change this here to minus. And I'm gonna do times. So notice it's not an x, it is an asterisk for multiplication. And then division, it's not division symbol, it's a forward slash. And then modulus, you're not sure what modulus is. It's basically, it tells you the remainder of an operation. Once we see the results, I'll explain that in a second. So I'm gonna go ahead and run that again. Okay, so 10 plus five is 15. 10 minus five is five. 10 times five is 50. 10 divided by five is two. And 10 percent five is zero. So 10 divided by five is two, as you see here. And there's no remainder. So if I change this, and this is kind of probably, let's change that to three, for example. I'm gonna run that and see what happens. So 10 plus three, 13, check. 10 minus three is seven, check. 10 times three is 30, check. Now here's where it gets weird. 10 divided by three does not obviously equal three. But because these are integers, we lose the decimal point. So you have to be very careful. So if you're doing an operation and you expect to have a decimal leftover, you gotta make sure that it's not an integer, or it's a double instead of an integer. And then here, 10 divided by three is three and we got one leftover, because three times three is nine, plus one equals 10. Let me put that back to five, just to make sure that looks comfortable for people. The order of operations is very important. So you don't wanna get weird results. So we call it order of operations. So let's say, for example, I have z equals 10 plus five times 10 minus 10 divided by five. So what do you think the answer is going to be? So oops, I put my system.out.println. Get a new one here. Okay, so I'm gonna say z, oops, z colon plus f, plus z. Okay, so I wanna see what the value of z is. Now, what do you think, stop the video if you have a second and think about what do you think that number is going to be? Okay, and let's see what the answer is. Now I'll go through and I'll explain how. So the answer is 58, okay? So where did that come from? Okay, so the order of operations is parentheses, exponents, multiplication, division, addition, subtraction. So it is done in that order. Now in this case we don't have any parentheses so we can ignore that. We don't have any exponents, so we can ignore that. We do have multiple multiplication. So we first do 10 times five. So that gives us 50. Then we do division, so 10 divided by five is two. We do addition, which gives us 60. And then subtraction, which gives us 58. Okay, so that is the order. So you can either, depending on your formulas, you can leave it at that order. But let's say we wanted, if we wanted to change it, we could say, okay, well I don't want that to happen. I wanna do this operation first, then do the other two. Actually that didn't give us the same answer. Go figure, let's see, let's try something and it'll give us a different answer. Okay, and run that. Okay, so that gave us 18 because it starts with parentheses. Okay, so that is PEMDAS, P-E-M-D-A-S. I learned it as please excuse my dear Aunt Sally and I actually do have an Aunt Sally so it worked out really well for me. But it's, again, it's parentheses, exponents, multiplication, division, addition, and subtraction. Okay, moving on, this is gonna be a bit of a long video but you can digest it in chunks if you like. 1.4, this is the compound assignment operator. There's more than one. Okay, so let's say I have int speed and let's say it's 1,000 kilometers an hour. It's an airplane or something. Okay, so what I can do is I can do this. Speed plus equals 10. Okay, so what that does is it adds 10 to the speed. Oops, sorry, I can do it this way. So I'm gonna put speed, I'm gonna get the formatting right. Let's run that. Let's see what that does. Okay, well I spelled speed wrong. And what does the error say? So system out print land, let's speed plus equals 10. Where did I go wrong? Identifier expected. Oh, let's see, try that again. Oh, I see right there wrong. It's gotta be inside that. So I couldn't put that outside the main method, sorry about that. And again, hopefully you're following along at home, we're in class, and you are learning to see these little things that errors that matter. Okay, so I've added 10 to speed. I could have also done speed equals speed. And it looks a little weird if you're coming from an algebra background because you couldn't do that in algebra but it does work in programming. But what we usually do is the compound operator plus equals. Okay, and it'll come as no surprise that we also have equivalents such as, and we can do minus equals. We can do, oops, this trackpad's very sensitive. We can do times equals. We can do divided equals. And we can do percent equals. And again, you'll see all of these things, especially later, especially when we do recursion and stuff like that, you will see these types of problems on the AP exam. So let's run that and see what happens. Oops, that should be not speed. What was I thinking? So 10. So why didn't somebody tell me that I was watching that? 10, 10, 10. Let's track speed for myself. 10 and 10. And some people might be watching and say, why didn't I just re-shoot that and film it again? I don't do that because, A, you're gonna spend a lot of time debugging your programs. And I think it's one of the problems a lot of tutorials have is they show perfectly written code. And nobody writes perfect code the first time. You need to really need to learn how to read the error messages and to learn what's going on. Okay, so let me go ahead and run that again. Okay, so 10,000 plus 10 is 1,010. 1,010 minus 10 is 1,000. 1,000 times 1,000 is 10,000. 10,000 divided by 1,000 is 1,000. And then 1,000 divided by 10 is 100 with zero left over. So we do see that that works the way. We're getting closer to the end here. I didn't realize it was gonna be so long, but that's okay. Again, I'll put the links to the each section down below in the description. So we also have increment and decrement. Okay, and this is very, very common in programming. So let's say int index equals five. And what I can do is I can do index plus plus. And then system.out.print.lin. And this is index plus index. So let's see what index is gonna be. And again, try to think ahead. What do you think it's gonna be? You see two pluses. I think it's gonna be a bigger number or is it gonna be a smaller number? Yep, that isn't very bright. And then we'll also do minus, minus and see what happens. If I run that. Okay, so index started is five. So plus plus gave us six and then six minus minus is back to five. So this increments it by one or decrements it by one. Now there's something you'll see. If you're looking at other people's code, you won't see it on the AP exam, but just so you know, in case you're looking through some code, you might see something like the following. I'm just gonna copy this because I already typed it on another screen and time is getting short here. Now remember, index is back to five. So plus plus is six. Minus minus is five. And now notice here we have plus plus here first and then minus minus. So what's gonna happen here? What do you think is gonna be the output? Okay, so we had our first index, which was six. Then went back to five. Then we have five, then six, then six, then five. Okay, so this is really weird. So post fix means the plus plus and minus minus are after the item or after the variable. And plus plus and minus minus before is called a prefix. So what happens in this case, if it's after, which is a post fix, it'll print index first. So index is five. So index five, then it adds the one. Once the printing's done, then index six, then it subtracts. That brings us back down to five. Now here we add one to five. And then that's why we see the six. And here we subtract one and that brings us back to five. Now it's a little confusing, but just so you know, if you see that, this is why that happened. So if you put this inside of here, if it's after, it does this whole line first and then finally it does the plus, the plus plus increment. So that's just something to be aware of as you're learning to code. And I think this is the last one. This is called casting. Casting is a fancy computer term for converting a variable. Like casting a team. Casting. Converting, let me call this converting types. So for example, there's different types. One is called implicit casting. So I type int a equals 22. And I say b double b equals a. System.out.print all in. So let's say a plus a plus space b plus b. Now looking at this, what do you think's actually gonna happen? So we said a is 22, but it's an integer. b is a double, but we set that equal to a. So what do you think's gonna happen here? Okay, so a is 22 and b was automatically converted to 22.0. This is called implicit casting. So it actually did that for us. Now let's try and do that backwards and see what happens. Double a 22.0. And we'll say int b equals a. So we got an error. So it says incompatible types, possible lossy conversion from double, probably says double to int, yep. So going from here, we might actually lose some information from a double to an int. So this was 22.3, we would lose that 0.3. So it's trying to protect us from that type of problem. However, in the opposite case, int equals 22, and then double, there's no chance of us losing information. It's just gonna convert it to 22.0. So that's basically how that works. So if you're going from, so you can go from an int to a double with no problem because you're not gonna lose any information, okay? But you cannot go from a double to an int without explicitly casting. That's what we're gonna get to in a moment. So explicit casting, again, casting is just a fancy word for converting. So double c equals 22.0. Int d equals, and watch what I do here, int c. Notice the parentheses go around the int, not around the c. And you might see that in some other languages. Okay, so I can go ahead and copy that. Let's base it the same format. C, C, d, and d. Now let's see what happens here. Okay, so we had 22.0 and 22. So what we did was we converted that 22.0 double to an integer. Now notice the zero is lost. So what if that was a different number? What if this was 22.6? So let's run that and see what happens. What do you think's gonna happen? Is it gonna be 27? No, it's 22.2, 22.0, or 22. So it does not round for you. It just lops off the decimal point. So you have to be aware of that. But if you did wanna round the number, I'll show you what to do there. And this is kind of interesting. So rounding numbers. So we say double e equals 8.6. And we'll say int round e equals int e plus 0.5. So we're taking e, we add 0.5. So that gives us 9.1. And then we change that to an integer and that's gonna give us nine. So system.out.println, quote e plus e plus round e. And quote e plus e plus round e. Put space here, round. Notice I like to think that my variable names make some sense. So e and then we have rounded e. So let's run that and see what happens. Oops, got an error in, okay. So what did I forget there? parentheses around the int. So 8.6 rounds up to nine. Now, if you're doing a negative number, it's gonna be a little bit different. And we're gonna use double f here and round f. Now we're gonna make that negative 8.6. Now if it's a negative number and we add 0.5, it's gonna be basically going the wrong direction. So let's just look at that and see what happens. And we'll show you how to fix that. So it rounded to negative eight when it should have rounded to negative nine. So with a negative number, you have to use minus 0.5. Okay, that gives us negative nine. So I think that was a lot of information, especially probably for beginners. But these are the basic kind of mathematical functions we can do at this point with what we know. This is, we learned a little bit about how to convert values as well. And a little bit about using a J grasp as well in this lesson. So these are kind of the big things you're gonna need to know right off the bat. So how to print some information onto the screen using printlin or printline and just regular printing. And we are gonna be looking at three different variables and data types. In this course, there are others, as I mentioned, but for the AP subset, it's Boolean int and double. That's what we need to focus on. And we talked about declaring variables, the final keyword, oops, and I spelled that wrong. Expressions and assignment statements and oops, compound assignments, orders of operations, increment and decrement, prefix and post-fix. But notice again, this prefix will not be on the AP test. So you don't need to worry about that. But you might see it again as you're Googling stuff. And then casting, we talked about converting. Some things convert without a problem. That's implicit casting. Some things convert, you have to do it yourself. That's called explicit casting. And you learn a little bit about rounding numbers using these methods. And I think, again, that was a lot for a lesson. This has gone on very, very long. I didn't realize it'd be so long. But again, if we break it down, it's only about eight minutes per section, give or take. Okay, I'm gonna stop there and hopefully you'll come back for unit two.