« Learning to Program | Main | Nanny Knows Flow »

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d834208b9953ef00d8345ddaa353ef

Listed below are links to weblogs that reference Starting to think like a Computer Scientist:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Eric

Glad you're getting into it! One more item that should probably be on your list of basic steps is "definitions". One of the most powerful aspects of a program is being able to define complex functions or classes of objects and then using them to achieve the goals of the program. By defining functions and objects, you are abstracting the program into a more high-level purposeful language which should be clearer to understand (for the human) and easier to work with and update.

Ted Bongiovanni

Thanks Eric! What would an example of a definition be--would it be something like: Workout: a session of vigorous physical exercise or training. Similarly, what would an example of a function and object be?

Eric

A definition is what Python was complaining about when you tried to use the identifier "apple" without defining it. There are two major types of definition that come to mind for me.

One is a "definition" in a simple algebraic sense: you say "let x = 5". When you define variables, that's what you're doing. You are creating a named identifier that will represent a particular value. This value can change (that's why it's called a variable), but you will be able to easily refer to it by the name you gave it.

The other kind of definition is more abstract. It's "definition" in a linguistic/functional sense. You can define a function which will perform some operation on its input and may yield an output. For example, "f(x) = x + 4" is a mathematical function that has "x" for an input, and it will yield x+4 as its output. The same kind of thing (but more complex) works in programming:

def plusfour(x):
return x+4

That's a python function that does the same thing. Here is a java function that does the same thing:

int plusfour(int x) {
return x+4;
}

Note that these functions have a name "plusfour". This allows you to call that function easily. To make use of this, you first define the function in the beginning of your program, then afterwards if you'd like to use that function, you can call it:

print plusfour(5)

That will print "9". Obviously it's foolish to define such a simple function, so you'd be more likely to define a function that did something more useful, like converting fahrenheit to celcius:

def c2f(c_temp):
return 9.0/5.0*c_temp+32

That way, whenever you want to convert, you can say:

c2f(c_temp)

instead of:

9.0/5.0*c_temp+32

It's also useful to create functions to isolate frequently-used duplicate code. It's like mass-production: If your machine can correctly assemble one gizmo, the other 1000 will also be correctly assembled (in a perfect world).

Class/Object definition is a bit more complicated, so I'll just briefly describe it. First you define a class of objects which you will use to represent some concept in your program. For example, a Tempurature class. When you define the class, you describe all the generic attributes of that class. Tempurature only has one attribute: the tempurature! However, you can also define functions for the class such as "getFahrenheit" and "getCelcius" and "getKelvin" which will convert this particular tempurature to the scale of your choice. After you've defined your class, you need to create an object of that class. In the example case, I'll say we need to define our tempurature first in degrees fahrenheit:

tempToday = Tempurature(55)

Then if I want to use today's tempurature on various scales, I can say:

tempToday.getCelcius()

or

tempToday.getKelvin()

Note that tempToday is a variable of type "Tempurature". So we've defined a class, and then made a variable which stores a particular object of that class. The tempurature example doesn't quite demonstrate the true usefulness of classes and objects, but imagine the class for "annotation" in the Image Annotation Tool. It holds the text, the x coordinate, the y coordinate, and a hyperlink. All of that information is organized into one object which can be referred to with a single name. Again, it's like building a machine for mass-production, except this allows each item on the assembly line to be configured slightly differently (e.g. different colors, custom monogramming).

Also, aside from defining functions (also known as methods) for your class, you can define functions elsewhere which are designed to have objects of your class as inputs. For example, a "saveAnnotation" function that will save an annotation to the database.

Hopefully that gives you an idea.

Jonah

I know you are interested in writing some web apps, but some of the nonsense involved with writing web applications (sys admin type stuff, configuartion, etc) might be distracting from core CS principles.

Check out this approach:
http://tech.canterburyschool.org/pycon/ using http://www.pygame.org/

Might be more gratifying earlier on than jumping straight into http-land.

/jsb

Ted Bongiovanni

Thanks Jonah! Checked out the paper---seems like a great interim step and fun too!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

pictures

  • www.flickr.com
    This is a Flickr badge showing public photos from teddyb. Make your own badge here.

Twitter Updates

    follow me on Twitter

    CAUSES

    Good Reads

    Triathlon Training Books