Welcome to our first video on blocks to text. Hopefully, you have some experience in block-based programming because we think everybody's first language in learning to program should be block-based. But once you've gotten the concepts from block-based programming languages, it's a little bit easier to apply them in a straightforward way to non-block-based languages like Python or Java in this case. So let's start by talking about what is at the core of the difference between most of what we would do and sort of more traditional block-based languages and what we're going to be doing in Java. In block-based languages, most of them anyway, you spent a lot of time providing direct action to sprites or to objects or whatever they might have been called in your system. Here we've got a picture from scratch, it's the infamous catch game and you're giving some instructions to either the apples sprite or the basket sprite and they can each have their own code but you're using that code to direct the action. When we start looking at covering jet languages like Java and particularly in the structure we would in many introductory programming courses at the university or something like the AP Computer Science A, we're going to be moving now not from directing objects immediately but to really focusing on manipulating data. We're going to get objects in as well later. They're just a little different and we're not going to be giving sort of like physical characteristic descriptions like move forward or turn right. Instead, we're going to think about manipulating data, crunching numbers, that's what computers are good at, right? So some of the things we'll do is we'll write programs that would find an average of a bunch of numbers or maybe it will record and keep track of and manipulate students scores on a test or in your average of the entire quarter or semester at your school. Remember this, we're going to go from sprites to data. Now this also hints up by the way one of the very difficult things about doing this book and learning it ourselves and in teaching our students is that sprites are eminently visible, right? Your code is manipulating objects, you can watch it being manipulated. When we're manipulating data that data is stored internally on the computer and in many times, you can't actually inspect it directly. You don't see things changing. We're going to have some tools that will help us and our students learn that. But that is one of the biggest problems. It's more abstract. So just to hint, review and keep this image in mind, we're going from sprites to something not nearly interesting. This is a variable name. It's called scores. In the Unit 1, we're really going to be looking at three key concepts that is variables and how we do things with them like assigning them values and particularly changing their values using mathematical expressions. So we're going to review each of these in regards to experience you might have with blocks so that hopefully it'll make the transition easier. Let's start with variables and assignment, because they kind of go together. I mean, there is a separate thing in most block-based languages where if you want to create a variable, you have to click on a separate button. Then you can have the ability to drag out one of these three instructions for example, set score to zero. That's the assignment version or set temperature to 75.4, that's also an assignment of the number 75.4 to the variable called temperature. Finally, we're going to have one other thing here. We're going to have set player named to Juanita. That's a different kind of variable that can hold text in it. So in fact, here is the equivalent Java code that does each of these three statements. Yeah. So we'll see some things that are sent always got score in zero, we've got temperature and 75 but we now have these equal signs. We've got these words on the left instead of the string, we've got semicolons at the end. These are all going to become problems for us. If you are used to doing sort of App Inventor type things that this might be the block code that you're used to. We'll show that just for those of you who are coming from that lamp. So let's talk about one of these things that's different. First and foremost is this idea that variables in Java each must have their own type. They can only hold certain kinds or types of data. That wasn't true in most of our block-based languages. We can set score to zero or we could set player name to zero. But here, in addition to sort of saying we want to make a variable of type score and give it a value zero, we have to say what type and we have three int which is whole numbers, double which are numbers with decimal points after them and strings which are characters. We're going to get to one more later but we'll talk about that there. So the other thing I want to point out is that there are more than one ways to make or declare a variable and to give it a value. You can do it all at once as we did in the previous. Let's look at that. Here we declared or made a variable int score equal and we give it a value zero. You can break those into two parts. You could choose just to declare or create that declaration as a vocabulary term we want to get comfortable with. You could declare those variables. I'm going to have a variable called score and it's going to hold whole numbers. I'm going to have a variable called temperature and it's going to hold decimal point numbers etc. and you could separately initialize or assign values to it. So score equals zero, semicolon temperature equals 7.5. So you can break those two parts apart. There's no rule on when you should do it, it really will just depend on the situation you're solving problems in. But to tie back to, if you did this, the CS unplugged activity from code.org called variables in envelopes. If you remember we had the robot and like he would make up things like what color is it? This is actually taken from the final assessment. When you're declaring something that's like having the box and saying I'm going to have a variable called color. Then the initialization is when you say color equal pink. So even way back in on that CS unplugged back to who we are already previewing by using this equal sign for putting the pink value into the variable called color. All right. More things that come with this, we had where we're just setting a value to our variables but we can also then change values or sign different values into a variable. So here's three. Snap. Yeah I've been using snap here. If we can set the score to be 10 plus bonus, the assignment there is 10 plus bonus is by the way also an expression, we are evaluating that expression and we're putting that in the score. W you'd see that it's the same as score equal 10 plus both. Then it says, change score by one. Second instruction down there looks a little different. We've got the variable name score shows up twice. But this does reflect what we want to reset when change score by one means increase the value that's already stored in score by one. So we're pointing out that in a second. Let's look at the third one. Change score by bonus times three. So here you can even see we can have an assignment where we're using the change which means, add on to the original value in score but we can have a whole expression like bonus times three. So there's your three Java statements. Let's give it to you people from App Inventor, there you are. There's some of that. What is tricky about this? Well, reality is the change blocks is ubiquitous in block-based languages isn't something that Java itself supports directly. So although actually it does later I'll tell you about that then but in general, the simplest way to replicate a change in instruction would be, change score means, score equal score plus something. So we're policing that change score in each of those by score equals score plus something. So that works. Now, the other thing that what's going to come along with moving from blocks to text is that you're going to have more and different kinds of errors. In particular, I want to talk about the different kinds next because these are things you're going to want to really emphasize with your students. So previously, if you've worked in a block-based language, we really only have one kind of error most of the time that would ever happen. That was called a logic error or a vocabulary word that we want to learn to use as semantic. So what that meant is, you would write your programs, you could hit the green flag or play or whatever it was that caused your code to run and things happened. It might not have happened the way you wanted it to. So we call those logic errors as semantic but the computer could in fact at least interpret your instructions. They can run the blocks of code. The blocks just maybe didn't do the right thing. Once we get to Java, we're going to still have those kind of errors. They're going to come along. We're going to write some code maybe it compiles but it doesn't do what we want but now we're going to have another entire type of errors that we're going to run into and these are called syntax errors. Syntax just means it's about the structure of the instruction. In general, I find that it's easiest to explain to students by saying you can kind of have like typing errors like I literally forgot to type a semicolon or they might be about revealing errors that are based on you're not understanding how language should be used or if you want to think of it it's like not knowing that you need to like an English sentence is structured, subject verb etc. So let me give you examples of those. So let's first look at some of the most common sort of I would say they're typing errors that we tend to make. First and foremost that semicolon at the end of almost all instructions and not all of them that's the problem. But that'll be something that your students will forget an awful lot. Other things are like maybe forgetting to put in a parentheses or something like that, maybe only one of them but not all of them. Like the Alcuin can't just be strictly typing errors. We're going to talk later about how having pair programming happening is particularly helpful in this regard. The other kind of thing you could have that is in fact true about it's about the structure, the typing of the sentence but it really comes from a misunderstanding about how a Java language instructions are structured. So suppose we wanted to say score equal bonus plus 10. So we want to take whatever is in bonus, add 10 to it and then store that variable in to score. Well, this equals the fact that we actually evaluate all the things on the right and then put the result into the variable on the left, that's a very computer sciency thing. Those of you who are remembering algebra or teach that. In algebra, what's on either side of the equals doesn't really matter. They're just supposed to become equal. So students might do this instead, bonus plus 10 equals score. This is a syntax error, it will not actually compile because this instruction doesn't make sense in Java or in a programming language. We always take what's on the right and stick it into one value on the left. In general, the only thing you can have on the left is a variable. We'll get into a few other examples there later. However, you're actually sometimes also going to find typing and language usage errors that may lead to logic errors. That means it may compile and your computer might try to run your program but it won't do the thing that you want it to. This often comes down to various mathematical expressions that students are going to write, at least at the beginning. So suppose we have this instruction and a block, we want to change the score by bonus plus 10 times three. We know it's bonus plus 10 times three because of the way the tiles are stacked on that. The light green tile so we do that first and then we go down to the darker green tile. In Java, the correct solution to this is score equal parentheses, parentheses bonus plus 10 parentheses. That means do that first times three parentheses. You wouldn't have to have the outer parentheses around the bonus and three because of the way order of operations works in Java that we do just like in math. We do multiplication beforehand but it's certainly easier if students just go ahead and fully parenthesize everything, and we don't have to worry about that. But what they need tend to do is not be thinking about, worrying about order of operations or when they're typing something. So maybe they would type in something like the bottom, just leaving out all the parentheses. So this code is syntactically correct. The computer can compile it and understand what to do. But what the answer is going to come out of there is not going to be the same as in that block program above or as in the example above that. So this will cause students problems because they'll calculate the wrong score.