>> Hi, I'm Cay. >> I'm Sara. >> We'll be the instructors for this course. The way most people interact with their computer is in the same way that I just did with my Word Processor. There were a lot of buttons, but if there isn't a button for what I want to do then I'm stuck. >> However, there's a secret language that you can use to make your computer do whatever you want it to do. >> In this course, we teach you that secret language. When you learn it, you will have power over your computer. >> We'll get you started with the Java programming environment, and you'll write your first programs. >> You might make a few mistakes. That's okay. Actually, it's good, because you learn from your mistakes. >> We're really excited to teach you the secret language, so that you can make the computer do what you want it to do. Let's get started.
My name is Kylie. I'm a junior at Stanford studying Symbolic Systems, which I joke is CS with a social life. It's computer science with philosophy, psychology, linguistics, and communications. I came into school thinking that I wanted to do public policy because I wanted to change the world. And then, I had a realization that, why do I have to do that with politics when I could do it with technology? But I still needed that push. my brother, who's a developer, told me he would disown me if I didn't take and enter the computer science class. And so, I took it and I was immediately hooked.
Let's start with a very basic idea of what programming is. Look at this toaster, what can it do? Okay, the toaster can toast. Good toaster. What about this computer? The computer can run programs, why is that better? Why do we say we are in the computer age and no one says we are in the toaster age. A better way to describe that is that a computer can do any instructions that you give it. Whatever you tell the computer to do, it will do it. And that makes the computer such a universal device that can manipulate images, text, sounds and much more whereas a toaster can just toast. There's just one catch in order tell the computer what you it to do you have to speak it's language. You're going to learn how to tell the computer what you want it to do in the language that the computer understands. And that's a great thing because now you're the master of the machine and it will do for you quickly and reliably any number of tasks that you would find tedious or difficult to do on your own.
The language that you will learn in order to talk to the computer, is called Java. The name Java was chosen for fun. Many programmers drink lots of coffee. And some people call coffee, Java, because of the island of Java. They grow coffee. Whatever. We've chosen Java, for a number of reasons. Java is very, very popular. There are millions of programmers who are familiar with Java and who use it every day on the job. Java is as programming languages go, pretty simple for the most part. There are a few rough edges because Java was not designed as a training wheels language for students. It really is a real word programming language. But, that's okay. Java is safe. And, that's great for students. When you make an error, you'll get a very clear indication of what went wrong, and it'll be easy for you to fix it. And finally, Java is very useful for the kinds of everyday tasks that you might encounter. Processing text, processing images, dealing with the web. In this course, you will learn. To use Java in such a way that you can simplify your own work. So again, what is Java? Java is a programming language, a language to write instructions for the computer that is popular, simple, safe, and useful. Let's get going to write some programs in Java.
Our first program will follow a long standing tradition among programmers and print a simple greeting to the world. Here is the java code. Well it doesn't look all that simple may be. But I'll show you a little trick. It turns out that this line here is much more important than the other two. And, in fact, let's hide those for now. Now let's have a look at this statement here. And give me your best guess, what greeting do you think will it print?
Well, the greeting will be Hello World, the text that's enclosed inside these quotes. Let me show you how that works. I will be using this programming environment, called BlueJ. BlueJ is a great environment for learning Java, and I hope you have a chance to download and install it on your computer. If not, it's okay. You can use a simpler environment that's a part of the web course. BlueJ is really nice, and I really encourage you to go ahead and download it. You'll find a link with installation instructions in the instruction notes. We also gave you all of the code that we're using in this class, so you should download that too.
Cay and I are going to help make sure you get BlueJ and the course code installed and downloaded. Installing BlueJ will have two main parts, installing Java and installing BlueJ. You might not notice this part if you are on a Mac. There are links below that will help guide you. There are written installation instructions and some videos of the installation process. You'll need to pick the right instructions for your operating system, Mac or Windows. If you can't get BlueJ running, it's not your fault. It just means your computer's set up differently from ours. We can help. Post your questions on the forums. Below this video, there's a section called discussions. You can read other people's questions and answer, or you can ask a question yourself. When you're asking a question, give us as much information about what you did and what the result was, as you can. The more you tell us, the clearer our advice will be. Once you've successfully installed BlueJ, open BlueJ and go to the Help menu. Select About or Copyright, whichever one you see. Who's the first contributor on the team? In other words, what's the first name that comes up?
If you got that working, you just completed the first question on homework zero. Lucky you. You can now navigate to homework zero and do the rest right now if you like. To do that you would use the drop down up there. Right now it probably says lesson one objects, but that's what you'll use to move around between lessons and homework. If you don't feel like switching to the homework right now, that's fine. You can finish up this lesson first.
Once you've installed BlueJ, you'll need to download the course code. There's the link below, click it, it'll prompt you to download a zip file. Save the zip file under Documents. Unzip the directory. It contains the code for lesson one. It also contains a file secretCode.txt. The video links from the last question also show the process of saving the Zip file, and unzipping it. What does secretcode.txt contain?
If you were unlucky and installing the software didn't work for you the first time, that's okay. For most of the questions, the Udacity IDE is a good backup. Keep trying to get BlueJ working though and definitely reach out for help. We want this to work for you.
Afterwards, go ahead and start BlueJ, and follow along what I'm doing here. Select the Project Option, open a project, navigate to the folder into which you have unzipped the starter code, go to lesson one and then to hello, and here you get an icon that represents out first program. Double-click on it, and a window pops up with the program code. Here again is the line that I said is the most important one. We'll have a closer look at the bits and pieces of the statement later, but right now let me show you how to execute a program. Click on the button that says Compile. Now your program is translated from the Java language into a lower-level language that the computer can execute efficiently. We don't normally have to worry about that process, except we have to remember to hit the Compile button before running the program. Now back in the main display, right-click on this box. Select the second line that contains the word main, and that launches the main part of our program. You get a dialogue here where you should simply click OK. As soon as you do that, another window pops up and [UNKNOWN], here's our greeting, Hello World. Now, of course, I want you to change the greeting and print something else, and I want you to show me what you did. Here we'll use the Udacity coding environment, and not BlueJay to give us any of the code that you have developed. Let me show you that. Whenever we want you to submit programming work, you will see a screen such as the following. Here we give you a bit of starter code, and some instructions on what we want you to do. Right now it says, please print Hello Udacity. You can type your answer in here. Or even better, you can first do the answer in BlueJ and then just paste it in here. To test your program, scroll down and click the Test Run button. And here you see the program output. Once you're happy, click the Submit button. That way we get your work, and get you feedback. In this case, that it was correct. Now, it's your turn to try it. Sarah will walk you through your very first programming assignment. Good luck.
When you want to just try an idea out in BlueJ, you have a couple options to start. One is to open the test prog BlueJ project that we made for you. It's in the files for lesson 1. The other option is to make a new project using BlueJ. To open the test prog project, open up BlueJ, click the Project menu, and then from the drop down select Open Project. Use the window that pops up to navigate to wear ever the lesson 1 code is. Double click, and open Testprog. It should look something like this. If you right click, you can open the editor. And then you have a main method all set up and ready to go for you where you can put whatever code you want to try out. If instead of using the test project that we made for you, you want to make a new project. You would go to the same Project menu, select New Project. And then give it a name. Some examples are compiler runtime, errors, hello, whatever your project is going to be about. I'll just call this one a new project. Now I close whatever project I had open before and I can see my new project. Before I can write code in it, though, I have to click New Class. And then come up with a name. I'll just call this one Testing An Idea for now. Now you edit this the same way you always would. Open Editor, and there's a lot of stuff in here. I always just delete it. Except for the class definition. And then add back whatever code I need to try out the thing I want to try. Again, you can put whatever code you want in here. Now that you've seen how to make a new testing project, let's put that skill to use. We're going to try out some code.
Let's experiment a bit more. What if we wanted to print Hello World on separate lines, like this. Hello on the first line, and World on the second line. Can you write this program? Try doing it in BlueJ, and then pasting it into our IDE. If you can't get BlueJ working right now, then just use our IDE.
Let's look at the code that I wrote to do this. Here's all that stuff that we had to do just to make the computer happy. And then here's the interesting part. I wanted to print on two lines, so I used system out print line twice. On the first line, I wanted to print hello, so I put hello in quotes. And I passed it out to system out print By putting it in the parentheses. On the second line, I want it to print out world, so I write world in quotes and again, put it in the parentheses. To run this in the Udacity IDE, I just copy this. I'm going to use Cmd+C or Ctrl+C depending on whether I'm on a Mac or a PC. And then I'm going to go to the Udacity IDE and I'm going to paste it in. Now I can test run it. And like I expected it prints hello and world on separate lines.
Let's try printing something different. If I were to program with these two lines, what will it print? You may want to try this out in BlueJ or in Udacity IDE. Does it do what you expected? You should put your answer in this box.
It should print 12, and 3 plus 4 plus 5 with all the spaces. This might have been different from what you expected. We didn't use quotation marks here. The quotation marks mark literal text. If we use quotes, it means that we want to write 3 plus 4 plus 5 as it is. But when we don;t use quotes, the computer reads 3 plus 4 plus 5 as numbers. It knows how to add the numbers, so it added the numbers and then it printed the result as you can see here. You can use system.out.println to print numbers and text
You've just worked with Sarah on a few modifications of our first program, and I hope you found that you can make changes to the program without understanding all of the bits and pieces. But still, it's a good idea to have a closer look and know the lay of the land. First off, as I've said before, these two lines are pretty routine pronouncements that you have to do with every program. And most programmers don't think about it very much. You know, it's like when you're in an airplane and the flight attendant says we're about to leave put all your seats in your upright position, put the tray tables back. turn your cellphones off. And most people just don't even pay much attention to those things anymore. It's worth noting the word main here, that means it's the main entry point into the program. It's worth noting the name of the program here, and all the other things are kind of noise for us right now. Let's focus on the important statement. Text like this. That's enclosed in quotation marks is called a string. That's just a technical term for the text. This statement here prints the string, in the exercise with Sarah you've seen that one can also print numbers. If you put a number or a mathematical expression inside the parentheses and that gets printed. System.out over here is technically called an object. As you'll see in the next lesson, objects are things that do work for us in a programming language. This particular object represents the terminal window, the window in which a string or a number is printed when println does its job. Finally, this part is called a method we'll talk much more about methods in the next lesson. For right now, just think of it as an instruction that's being executed. As you can tell from this part of the name, the instruction is to print something on the terminal window. What's with the ln back here? That says that it should also be prepared to make the next printout appear over here. So if we first print ln Hello World, and then we print three plus four plus five that printout will appear here on the next line. Without the ln it doesn't make the next printout appear on a new line. You'll practice working with both of these methods. With Sarah in just a minute.
Welcome back.What should these two lines of code print? Make a guess and then try running this in bluejay or the adaste id and then put your answer here
They print 39. The cursor starts right here. And then, we print 3. But since it's just print and not print line, it doesn't go down to the next line. So then, the cursor is right here. And then, it takes the 4 plus 5. And since there aren't any quotes, it adds them together and gets 9 and prints right here. And then, since its print line our cursor would go down to the next line. But then, we don't print anymore, so we don't notice that. We can see that if we make a program. So, we add all these pieces that we need to make the computer happy, And then, we add our own code then we close it all up and run it, and it prints out 39.
When you did your programming with Sarah, it's quite possible that some things went wrong and you got some nasty error messages. When you learn a new langauge, be it a human language or a programming language, errors are a fact of life. Let me show you some common errors so that you can recognize and fix them. Here's a typical error. Have a quick look and see if you can tell which part is wrong. That can't be right, the terminal window object was system.out not system.ouch. Let's see what happens if one makes such an error. You may want to work along with me here in Blue Jay. Open the errors project and open error 1. Here is the error. Let's compile and the compiler, the program that translate Java into lower-level code complains and says it has no idea what the variable ouch is. The compiler knows system.out, it doesn't know system.ouch. Such an error is called a compile time error because it's found when you compile. Some people call it a syntax error because there's something wrong with the syntax of this statement here. As you can imagine there are lots of syntax errors you could make. You might forget the semicolon. You might forget one of these quotation marks or both. You might forget one of these dots. Any time you do that the compiler will complain and you just fix that particular error and recompile. It happens to the best of us. Now here we have a different kind of error, again have a look and see if you can guess. The programmer misspelled world, let's see what happens in this case. When you compile, there's no error, the compiler doesn't know English. And it has no idea that this part has been misspelled. We can run the program and it runs. It just does the wrong thing. When a program compiles and runs but it produces the wrong output, then that's called a run time error. Some people call it a logic error because there's something wrong with the logic of the program not just it's syntax. Now have a look at this statement, it also has an error. What do you think will happen when you try to compile and run a program containing this statement? Do you think it doesn't even compile? Do you think that it compiles and runs but then it prints the wrong value? Like in this case. Or will it compile and run and then print an error message? Or will your keyboard melt into a puddle of plastic? If you have BlueJ open right now, just give it a quick try.
Well, I hope your keyboard didn't melt. When I ran it, got an error message. Let me show you. The class compiled with no syntax erros. So I clearly don't have a compile time error. But when I run the program, down here, I get an error message. And Bluej tells me that something very bad happened in the highlighted line. First off, it is a run time error. It's an error that happened at run time. And it is so bad that it terminated my program and displayed what's called an exception report. And the report is that a division by zero was attempted. And that's of course very, very bad. you've seen how to deal with a number of different kinds of errors, and you'll get to practice that with Sarah right now.
Let's make sure we understand these different kinds of errors. This line of code has an error in it. Is this error a compile-time or syntax error, or is it a run-time or logic error? It might not be easy to see what's going wrong. You can always try writing and running the code in BlueJ.
This one's a compile time error. Let's see how this works in BlueJ. I'll make a program and I'll put this line into it. Now I compile it and it says, cannot find symbol, variable Hello. What do you think went wrong here? You can pause the video and try and think about it for a second if you want. How would you fix this error? The computer doesn't know what we mean by hello. And what we wanted was hello Sarah to be literal text. In order to do that we need the quatation marks. Let's see if it works now. Okay. Now there are no compile-time errors. Let's try running it. Oops. It spelled my name wrong. There's no h in my name. This would be considered a run-time error. This is a logic error, not a syntax error.
Let's look at some more buggy code. What is the error in these two lines of code? Is this error a compile time error or a run time error? If you don't see the error immediatley, you can just try it out.
This one's a run-time error, and it's pretty small. So it might have been hard to find. I can test it out in BlueJ. Into my program I'm going to write the lines. Then I'll compile it. It said no syntax errors. So right away we can see there aren't any compile time errors. And when I run it, I get my lucky number is 12. With no space right here. Since the program still compiles and the error occurs when we run the program this is a run-time or a logic error. How would you fix this one? You don't have to submit an answer, just good to think about.
I think the most important thing to do when you encounter an error is to take a step back and breathe for a second. Usually it's some small inane thing, so I try to look back through whatever part seems to be compiling incorrectly or running incorrectly. And determining if I've, you know, misnamed a variable or done something really small or inane and usually it's that. If it's not then I like to proceed through the code line by line. And just sort of look at what every variable is assigned to, and what, what's happening in each line in the code. And usually by going step by step and slowly, I'll find the error pretty quickly.
You've just learned how to write some very simple programs and how to correct errors and to deal with all sorts of fussy details. Let's step back a little bit and think about what a computer can do for us. A computer can execute an algorithm. That's a technical term for a recipe of instructions that are unambiguous, executable, and terminating. Well, those are big words. So let's see what's going on. Unambiugous means that the recipe can be interpreted in only one way. Executable means that its steps are those that a computer can actually carry out. And terminating, of course, means that they come to an end. That's all pretty abstract. Let's look at a concrete situation. You need to buy a car. You have two choices: this beauty over here, it's cheap, but it has terrible gas mileage, or this sleek one over here, expensive, but great mileage. Which is the better buy? I'll show you an algorithm that makes that decision. The first car costs $15,000. And the second car costs $30,000. So, it looks like we should buy the first car. But, not so fast. The first car gets 10 miles per gallon, and the second car 50 miles per gallon. So, what do you think? Which car should I buy? Do you think I should go for Car 1 or for Car 2? Or, in the end doesn't it really matter? Or maybe you don't have enough information to give an exact answer. Tell me what you think.
Well, we don't have enough information to figure this out. If you drive a lot of miles, at some point, it must be better to get the more fuel-efficient car. So, let's say we figure we're going to drive 50,000 miles before we sell the car again or it falls apart. And, of course, it also matters how expensive gasoline is. If gasoline costs nothing, then the fuel efficiency doesn't matter, but we'll assume that gas costs $4 per gallon. Now, that's actually enough information for, to come up with the answer, but how will we decide? I will give you an algorithm, that a computer could actually execute, to settle this question.
What you see here is an algorithm to answer our question. It's a sequence of steps. Each step is unambiguous, and so simple that a computer can execute it. The sequence of steps will terminate and give us an answer. Now, I want you to play the sequence of steps and execute the algorithm yourself. And when you're done, tell me what you computed for a total cost one and for total cost two.
All right, here is how I did it. I go through the first step, miles driven is is 4. 5000 times 4 is 20,000. Now moving on to the next step, the purchase price for the first car is 15,000 plus the 20,000. From over here, gets my total cost of 35000 over the life time of the car. With the second car, again we drive a 50000 miles. Now the miles per gallon is 50. 50000 over 50 is a 1000 times 4 because we have $4 per gallon, is 4,000 for the gas cost. The purchase price was 30,000, plus the gas cost 4,000 gets me to 34,000. Now let's go on with the algorithm, is total cost one less than total cost two? No it's not, what a relief. I never wanted to buy that one in the first place. But now I have a procedure that is unambiguous, executable, and terminating that tells me what car to buy. So, this is a great example of an algorithm It is a recipe of steps each of which is so simple that a computer or a human can execute them and it reaches an answer. Now, you will notice that this algorithm here was written up in some mixture of plain English and Math not in Java code or not in any other computer language. This is called Pseudocode, and Pseudocode is really important. When you want to develop an algorithm before you get to coded up in Java or another programming language. What most programmers do when they need to solve a complex problem is they first get out a sheet of paper and describe the problem in Pseudocode and then they write in Java. Pseudocode doesn't have anyway near as many rules as Java does, but so it's a good idea to restrict oneself to simple constructs in Psuedocode. Let me show you what those would be.
In your pseudocode, you may want to write a computation. Such as the total cost equals purchase price, plus the cost of gas. You might want to have a decision. If the total cost 1 is less than the total cost 2 to one thing, else to another thing. You might want to repeat certain steps. While the bank balance is less than your target, or repeat some actions say, ten times. Those are the three essential elements of pseudocode. Computations or other actions without any regard of the syntax of a particular programming language. When you're doing programming there are two things that you need to learn, that's syntax of the programming language and how to solve problems. And we'll do this in parallel on two tracks. In the next lesson, you'll learn quite a bit more about how to work with the programming language. But right now lets learn a little bit more about algorithms, pseudocode, and problem solving. You've just seen an algorithms that manipulated numbers to give you a useful answer. But I thought it would be more fun to show here a few example that don't have to do with numbers. So here is Robbie our home robot. A robot is a computer too, it carries out tasks and one has to program it to do the right thing. Right now Robbie's task is. To clean up a bunch of legos, Robbie needs to put all the blue legos before all the yellow legos. Here's our first attempt at an algorithm. It's a sequence of steps. Robbie will wave his magic wand and he will say, make it so. Well, that's not really going to work. An algorithm has to be executable. And not involve magic steps. Our pseudocode, here, asks Robbie to repeatedly execute these statements. Namely, why all the blocks are not yet in order. Each time, Robbie is supposed to scan the sequence of blocks. Find the first one that's yellow before blue one, and swap. So Robbie would go along starting at the left, until it finds the yellow one before blue one. Swap the two. And then it would do it again. In the next step, it would scan from the left until it finds the yellow one before blue one and swap them, and again. Now I'd like you to try this. If I write a b for blue and a y for yellow, tell me the sequence of the blocks after the next slot, and the next one after that.
That may be Robbie. I'm looking for the first yellow, followed by blue, and swap them. That would be bby, byy. And again, the first yellow followed by blue, and swap them, and I'm at [BLANK_AUDIO] yyy and I'm done. Now, the sequence is in order, all the blue ones come before all the yellow ones. So, Robbie has used this algorithm here to clean up the Legos. Now, this is a great example, again, of an algorithm. You have a sequence of steps, each of them is unambiguous, each of them is something that Robbie can do. And eventually, the Legos are all cleaned up. It's actually not totally obvious why the Legos are always cleaned up when you execute the sequence of steps, but they are. Now, you've seen algorithms and pseudo code first with numbers, then with Lego blocks. And now, you get to practice the Lego blocks a little bit more with Sarah. Have fun.
I think with pseudocode you always need examples. Here's some pseudocode. We'll have two items with prices 1 and 10 and we'll set the cost initially to 0. Tax is the item price times 0.08. The tip is the item price times 0.18. And I'll set the cost to whatever it was before plus the item price, the tax, and the tip. And print the cost. What should this print? And when might you use this process?
This should print 13.86, and you might use this process when splitting up a bill for dinner with friends. In case you're not sure how I got 13.86, let's just go through this together. There are two items, and we initially set the cost to zero. So initially, cost is zero. And now for the first item, which is $1, we're going to say that the tax is $1 times 0.08, so 0.08. And the tip is $1 times 0.18. And now the cost is going to be updated to be zero. Plus the item price, plus the tax, plus the tip, which would give us about 1.26. Now, we're going to go through for the other item, which is $10. So this time, the tax will be 0.80. The tip will be 1.80. And the cost will be what it was before plus the item price, plus the tax, plus the tip. Which if you add it all up should give us 13.86. And if I did my math wrong, maybe that's just proof that I should be having a computer do this for me.
I've found that with pseudocode it helps to have a couple examples. So let's look at this example. So we'll start with n is ten and then while n is greater than one, we'll print it and if n is even we'll say that n is n over two. Otherwise we'll say that n is 3 times n plus 1. What does this pseudocode print? Put your answer here.
The pseudocode should print 10, then 5, 16, 8, 4, 2. We start out with n is 10. And then, check if n is still greater than 1, print n, which is 10. Then, if n is even, which it is, because it's 10. We update n to be half of n, and then we skip the stuff in the else. We come back here, n is still greater than 1. This time, it's 5, so we print n 5. And then if n is even, which it isn't, we would have done that. So we go to the else, and we update n to be 3 times 5 plus 1. So 15, 16. We come back up to the while and check again. And it's still greater than 1. 16, so we print 16, n is even. So we update n to be n over 2, so it's 8 now, skip the else. Check again. 8 is greater than 1, so we print 8. n is even, than 1, so we print 4. If n is even, it is. So, n becomes 4 over 2, so it's 2. Skip the else. 2 is greater than 1, so we print n. Print 2. n is even so we update it. n becomes 2 over 2, so it's 1. Skip the else. Now, n is no longer greater than 1. So we drop out of the bottom, and we're done. If this still looks a little foreign, that's okay. We're going to do more practice with pseudo code later.
The algorithm you just looked at would work if Robby were a person. But Robbie is a robot, and only knows how to do very simple tasks. Moving to the first place where he sees a yellow before a blue is too complicated. He can only do simple tasks. So, he could move all the way to the left. Or, he could move all the way to the right. He could move one space to the left or one space to the right. He can check if the block right in front of you is yellow or blue. And he can check if the block, just to his right is yellow or blue. I'm going to have you write some pseudo code, but so that I can help you check it, I'm going to give you some of the structure up front. Use the pseudo code commands over here to write pseudo code to move Robbie to the leftmost yellow brick that's followed by a blue. These lines are scrambled. Can you type them into the right positions? You might need to type a line more than once.
There might be a few possible answers here, but I'll talk about the one I came up with. If you came up with a different one, that might be right, I'd like to see it. We'll start by moving all the way left. And then, while we haven't found what we're looking for, if the piece you're looking at is blue, you'll need to keep looking. Move one space right. Else, if the piece to the right is blue, you should stop, because you found it. Else, if the piece to the right is yellow, move a space right. I think it's a good way to start by moving all the way left because that means the first time we find a yellow followed by a blue, that'll be the left most one. Now, as long as we haven't found it, we're going to keep looking. While we're looking, if the piece you're looking at is blue, like right here, that means that we haven't found what we're looking for yet, and we should move one space right. Otherwise, that means the piece that you're looking at is yellow. And if the one to the right of it is blue, so if we were right here, we're looking at a yellow, the one to the right is blue. You would stop, because you've found it. But if you're looking at a yellow and the piece to the right is also yellow, we haven't found what we're looking for and we need to keep moving. So, in that case we still move a space right. So, the way this would work on this input is we would start all the way to the left and we haven't found it yet. So, we would look at the piece, it's blue. So, we move right. We still haven't found it, so we look at this piece, it's blue. So, we move right. We still haven't found it. So, we look at this piece, it's blue, so we move right. We look at this piece. It's not blue. So, we check what the piece to the right is. The piece to the right is not blue. So, we check if the piece to the right is yellow, which it is, then we move a space right. Okay. Now, we need to check again. If the piece you're looking at is blue, nope. Else, if the piece to the right is blue. Yep. You want to stop because you found it. So, it looks like it works on this input.
Sometimes a set of instructions is not an algorithm. This is a slight modification of the algorithm that Kai gave you before. Why is this not an algorithm? Is it because the description is ambiguous? The description contains steps that cannot be executed? Or the procedure does not always terminate? If you'd like a hint, think about what happens if you apply this algorithm to this pattern.
This isn't an algorithm because the procedure doesn't always terminate. Let's try running it on the example I gave. Initially, it's not an order. So, Robby would locate the first two bricks that don't match and swap them. Now, they're still not in order. So again, Robbie would find the first pair of blocks that doesn't match. And swap them. We're now back exactly where we started. If Robbie keeps doing the same thing, again and again, he'll always end up right back where he started. The bricks will never end up in order. So this is not an algorithm. Robbie's batteries will run out before this algorithm finishes, or all the batteries and generators in the world would run out before this process finishes. He will never finish.
How about this one? Is this an algorithm? If so what does it do? You'll need a little bit more detail. This is a two by eight block and this is a two by two block. And I can stick blocks together like this. Is this an algorithm? Yes or no? If not, why not? If so, what does it make? An L, an H, a doorway or something else?
This is an algorithm, although this is maybe up for debate. And when it works, it makes a doorway. You might have felt like my instructions were ambiguous. I'm going to pretend they were perfect, though. If you can write clearer instructions, post them on the forum. I'd like to read them. And now, I'm going to do my best impersonation of a robot and follow this algorithm. So, first I'm going to place an 8 by 2 block horizontally. I'll do it this way, so you can see what I'm doing. And I'm going to stick a 2 by 2 block on the top of the left end of the 8 by 2 block, so right here. Now, 3 times, I'm going to stick a one, two three. And now, I'm going to stick a two by two block on top of the right end of the 8 by 2 block. And I'll repeat three times, putting a 2 by 2 block on top of the previous block. And now, I put an 8 by 2 block on the highest two. And we have a doorway.
I think the most important realization that I had when I got into computer science. Or the best advice that I got, was just somebody telling me, you're not bad at it. And by it, I mean programming. It's really hard because programming is a completely new way of thinking. It's like learning a language. And, it's really easy to struggle and people always seem like they know what they're doing. because some people have been programming since they were like three in the basement with their little like calculators and stuff. And I wasn't one of those people. And so, it took me a long time to just sort of realize that I could do it just as well as anybody else. And just sticking to it is the best thing that you can do. And there's so much help out there between, you know, online courses. just sort of forums on the internet or your friends, anything. And just being vulnerable and asking for help is the way that I've gotten through it.
Congratulations. You've made it to the end of lesson one. >> Believe it or not, you've learned a lot already. >> You've learned about the tools that you use to write programs, and you've written your first program. >> You've also learned how to structure your thoughts. So you can write pseudocode and algorithms that a computer can understand. You now speak the basics of the secret language. >> In the next lesson you'll learn how to write interesting programs with pictures, text and dates. >> Not those kinds of dates. We look forward to seeing you there.