Lesson 02

Talking to a Computer

In our last lesson we learned about Binary and Binary Arithmetic because everything to a computer is a math problem.  In our lab however, we saw just how difficult it can be talking to a computer in this way. Today, we are going to start learning a “higher level” language, or more English-like way to speak to computers.  So, where last week we learned math, this week we learn grammar and vocabulary,

Vocabulary

Our first vocabulary word is ‘Script.’  A script is nothing more than a sequence of expressions.  Think of a script as a letter (or perhaps email) with a list of instructions for the computer to follow.  We will use the word ‘Program’ to mean the same thing as script.

Our second vocabulary word we snuck into the definition of our first.  An ‘Expression’ is any meaningful element of a program or script.  There are many types of expressions, just as there are in English.  Like English, we have definitions, commands, questions, equations and even other sets of instructions.

A ‘Class’ is a special kind of program.  It is a set of instructions for creating many similar things.  A class is often compared to a factory. For instance, a car factory may build many of the same model cars, however, not all of them are exactly the same.  As the car is being assembled you can change things such as the color, type of radio, seat covers, etc.

The things created by a class are called ‘Objects.’  If the class is ‘Ferrari 308’, MY Ferrari 308 is a different object than YOUR Ferrari 308 object.  Mine might be red and yours might be black. Sometimes we say an object is a specific ‘Instance’ of a class.  Depending on the context, the words object and instance may be used interchangeably.  

A ‘Message’ is a command sent to an object.  For instance, when you put your key in the ignition of your Ferrari 308 and turn the key, you are sending your Ferrari 308 object the message to start the engine.

A ‘Method’ is a sequence of expressions (like a script) telling an object what to do, whenever a message of the same name is sent to it.  In other words, if you send the message ‘startEngine’ to your Ferrari 308 object, it must have a method named ‘startEngine’ with commands it can execute.

Messages have two parts to them.  The first part is called the ‘Message Selector.’  This is just a fancy way of saying message name.  In our example above, startEngine would be the message selector.

The second part of a message contains the ‘Arguments’ or values.  Not all messages/methods have arguments.  For instance, startEngine might not. However, setCoolAirTemperature might include a number value indicating what temperature you want the car set to.  So an example message might be setCoolAirTemperature 72, telling your Ferrari 308 object you would like the interior of the car to be cooled to 72°F.

Finally, we often need a temporary place to put something so we can refer to it again later.  We call these storage places, ‘Variables.’  Thus, if we want to compare your Ferrari 308 object to my Ferrari 308 object, we might save them in two variables named yourCar and myCar.  In this way we can send messages to each object independently.

yourCar  startEngine.
myCar    startEngine.
yourCar  setCoolAirTempurature: 72.
myCar    setCoolAirTempurature: 68.

The specific arrangement of each term above is very specific.  This is the syntax or grammar of our language and what we will discuss next.

Syntax & Grammar

The above, partial script, shows examples of objects, messages and arguments.  By inference, it also demonstrates classes and methods which are the definitions or blueprints used to create objects and interpret messages.  Each line above represents an expression and the overl collection of expressions is a script. However, we did sneak a few extra things in and we even left a few things out.

First we have the order of terms.  In the above examples, we place the object first.  This tells the computer what object we wish to interact with.  Next we put the message. In the first two expressions (lines) we have one object being sent one message.  In both cases the messages have no arguments. You will notice the end of the expression, just like the end of a sentence in English is ended with a period.

Fundamentally, the next two expressions are exactly the same except the messages in each include an argument.  We seperate that message selector (message name) from the argument with a colon (‘:’).  

The punctuation is very important.  Leaving out the punctuation changes the meaning of expression, just as it does in English.  For example, see how a simple comma changes these two sentences.

Let’s eat, grandpa! 

Let’s eat grandpa!

I am quite sure grandpa would prefer the first sentence.  The same is true in programming languages. So keep grandpa safe and always check your punctuation!

As it turns out, the script above is only a partial script because it left a few things out.  Just like an essay has an introduction, body and conclusion a script must start up, execute and close properly.  It is also important to keep in mind, computers are not very good at guessing or making things up.  

If your mother told you to put your socks in your dresser drawer, you would first need to have a dresser drawer!  If you look at the script above, we use the objects yourCar and myCar but we never said where these objects came from.  Since we want to keep referring to them, we need to ‘declare’ them as variables and then we have to “manufacture” them from a class.  Thus, our complete script would be:

| myCar yourCar |.

myCar   := Ferrari308 new.
yourCar := Ferrari308 new.
yourCar    startEngine.
myCar      startEngine.
yourCar    setCoolAirTempurature: 72.
myCar      setCoolAirTempurature: 68.

The very first line declares our variables.  These are between vertical bars and, just like an introduction to an essay, must come before the variables are used.

The next two lines are where we instantiate (create) our objects.  The class we use to create them is the Ferrari308 class.  The message we send the class, ‘new’ creates our objects.  Notice we invoke the Ferrari308 class twice, once for my car and once for yours.  We then set our two variables (myCar and yourCar) equal to the results. Now, anytime later in the script we want to do something to your car or mine, we can refer to them by their variable names.

The final thing to notice is the capitalization.  Again, just as in English, there are rules about when to use capital letters and when not to.  If you do it wrong, it changes the meaning. To make it clear, look again at an English example.

Say a prayer for me father.

Say a prayer for me Father.

In both cases, the request (message) is the same, but the person (object) getting the message is different! 

 We will talk more about punctuation and capitalization, later.  For now, just remember it matters.