Lecture 1 of CS106A was an introduction to the course which covered what things you will be learning over the next 10 or so weeks. It also included details which are needed to complete the course. You can find my notes over here and as well as my suggestions on how to work on iTunes U with no official dead lines and no one to report back to.
Lecture 2 is where the course starts to get interesting. In this lecture you get to see a program being written from scratch as well as successfully run. You also get to see how a basic program is designed as well as an explanation of why you need to write it with a specific style. After the simple program demonstration, the difficulty level gets notched up very slightly and a demonstration of a program with loops and control statements is used.
What is Covered in the Lecture
I found the second lecture content was taken from the first three chapters of the Karel the Robot book. For this reason, I recommend you read chapters 1 – 3 of that book prior to watching the lecture so that what is mentioned in the lecture makes more sense.
A Brief Summary of Karel
In this lecture you get introduced to Karel the Robot. The Karel class (see: what is a class?) contains four methods (see: what is a method?) that allow you to control a program. You will learn from reading Karel the Robot that a method is a set of instructions that you can call on by name. These methods are defined by the Karel class. You can also add custom methods to your own class which I will explain later on in this post. The four methods Karel responds to are:
move()
turnLeft()
pickBeeper()
putBeeper()
The first allows Karel to move forward one space. The second turns him left. The third picks up a beeper and the forth puts a beeper down. By using these four methods, you can move Karel anywhere in his world as well as put down a beeper and pick one up, assuming there are beepers available to put down.
The turnRight problem
You might have noticed that Karel only understands how to turnLeft… what about turning right though? This can be resolved in a couple of ways. The first way would be to have Karel turnLeft 3 times within a program. The second way would be to create a private method within your program that you can call upon within your program. Lets take a quick look at a sample program that includes a run method and a manually created turnRight method:
import stanford.karel.*;
public class MyTurnRightKarelProgram extends Karel {
public void run() {
move();
turnRight();
move();
}
private void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
}
A quick breakdown of this sample code.
Line 1 contains an import of Karel. The Karel class is defined at stanford.karel.* which means you need to import this so that your program understands what Karel is and what all the commands mean that you provide. If you didn’t have the import then the computer would not understand move(); and would crash. Note that all statements (which is a line of code) end with a semi colon.
Line 3 is defining this program as a class called MyTurnRightKarelProgram. Note that it “extends Karel” which means this program is running a copy of Karel. Any methods we add outside of the run method actually improves and adds to the functions of the basic Karel.
Line 5 is where the program starts. When you run Karel, it is programmed to look for the Run method and execute what is in there.
Line 6, 7 and 8 are the commands we are giving to Karel. These are executed in order starting with the move() method. When move() is invoked, it understands that because we extend Karel on line 3 and Karel is imported on line 1. However, line 7s turnRight() is not understood by Karel, so we need to define the turnRight() method which we can see on line 11.
Line 11 contains the beginning of the new turnRight() method. This is declared as private because it cannot be invoked from outside the program… more will be written about that later. void can also be ignored for now. We then have three consecutive lines of code which are turnLeft()… times 3. The method is then closed off on line 15 with the closing brace.
So, when the program runs line 7, it jumps to line 11 and runs to line 15 at which point it jumps back to the run method and then executes line 8 and then closes at line 9 which is the point the run method closes out.
The ability to create your own methods makes the program easier to understand and can also save time at a later date should you require Karel to turnRight on a number of occasions.
What is SuperKarel?
Although I just covered on my notes what Karel is, there is another class that you can extend called SuperKarel. SuperKarel is a more advanced version of Karel… perhaps Karel 2.0 which includes a turnRight() method and a turnAround() method. As SuperKarel includes these methods, there is now no need to define your own turnRight method. Instead you can simply edit line 3 to say “extends SuperKarel” and then delete lines 11 to 15. When line 7 is executed from the example above, it knows what turnRight is because the SuperKarel class already defines it and line 7 is executed and then line 8 without a jump out to another private method.
How to begin writing your own programs
In Lecture 2 of the course, you will learn that there are many ways to solve a problem. If we only used the Karel class in the example above, we could solve the turnRight() problem several different ways such as a method which we already used to an iterative statement using a for loop repeating the turnLeft() method three times in a loop to other ways in which I haven’t mentioned here.
Some refer to this as the art of programming which means the way you write the code. The idea is to write perfectly syntactically correct code but also write it in a way that allows the program to be enhanced, updated or modified at a later date. Code also needs to be readable by people. Write it in a way that is easy to understand.
Although the very short and simple program above wouldn’t take much work to enhance, modify or upgrade… a larger program would. If we wouldn’t have defined the turnRight method and simply put turnLeft (3 times) each time we wanted to turn right, a complex program would become a pain to enhance as every 3 sets of turn left would need to be manually changed. Instead, when SuperKarel enhanced Karel, we could simply just delete the manually created turnRight method, extend SuperKarel and that was the extent of the changes regardless of how many times we invoked the turnRight method. So, when creating code, think about what you are doing, think about what ways would make the program better designed and think about making your program do what it should even if the surrounding world is changed.
A good way to write a program is to put it in English first. The lecturer starts by simply putting the incomplete commands in a text file such as move, pickBeeper, move, turnLeft, move, turnLeft, turnLeft, turnLeft, move (each on a separate line). He then notes that turnLeft 3 times could be just written as turnRight which would then invoke a private method… which will be defined. This is what is called the algorithm… it is simply writing out how the computer would solve a problem. When done, it needs to be converted in to a language that the computer understands such as adding opening and closing brackets after each statement as well as a semi colon.
For loops, while loops and if conditional statements are all covered in the lecture along with samples given. Rather than re-write that in my notes here, you can look at my notes from reading chapter 3 of Karel the Robot which has a few examples and a more detailed explanation of what loops are and control statements.
Your first CS106A assignment
At the end of lecture 2, it is time to start your first assignment. Make sure you have read Karel the Robot chapters 1 – 3 as well as installed Eclipse and read all other handouts which can be found in the iTunes U course.
Leave a Reply
You must be logged in to post a comment.