Learning to Program

Blog posts in my Learning to Program series

L2P: the “Parts” of a Program - Spinning some mad loops!

At some point in your application, your going to want to do something again. Maybe you will want to do it multiple times. Now its pretty easy to cut and copy the same command code over and over again, but what if you don't know how many times you will need to do something? What if you want to do something until something else happens? Don't fear, Loops are the answer! More »



L2P: the “Parts” of a Program - Decisions Decisions

You would be hard pressed to find a program that did not use some sort of logic or decision making during its execution. There is some form of “IF” in just about every programming language. So lets discuss how we use it in programs and how its implemented in Objective C! More »



L2P: the “Parts” of a Program - Returns, Accessor Methods, and Synthesizing

So far the PrintCount class methods we have created do not send anything back to the code that calls it. What if you want to get something back from a method you call? Well that would be a “Return” in programming parlance. This is yet another Wall-O-Text post that I'm going to hide behind a cut. More »



L2P: The “Parts” of a Program - Hello World with a OOP Twist, Part 5

Now that we have our basic class working, we can play with it a bit. First I want to introduced an easier way of allocating and initializing your instances. In part 4, I used 2 lines of code to setup an instance of a class. One line allocated it, and the other initialized it. Both these steps can be done in one line, as shown below:

  1. PrintCount * phraseb = [[PrintCount alloc] init]

This does the exact same thing as the 2 lines of code used before, just less typing. You can do it the old way if you want, but this is considered the convention when coding ObjC. Remember that, like in math, the innermost brackets are calculated first, then they work outward. Nested expressions can be used in many places in your code, We will discuss them more when we build upon our class in a future post.

So what if you wanted to count the number of times you printed another line of text? Simple, just invoke another instance of our class, then use the same methods we have before. Here is an example using 3 instance of our PrintCount class:

  1. PrintCount * phraseA = [[PrintCount alloc]init];
  2. PrintCount * phraseB = [[PrintCount alloc]init];
  3. PrintCount * phraseC = [[PrintCount alloc]init];
  4.  
  5. [phraseA setText: @"This is Phrase A, Hello!"];
  6. [phraseB setText: @"This is Phrase B, How are you?"];
  7. [phraseC setText: @"This is Phrase C, I am fine!"];
  8. [phraseA printText];
  9. [phraseB printText];
  10. [phraseC printText];
  11. [phraseA printNum];
  12. [phraseB printNum];
  13. [phraseC printNum];

This code should output:
2011-03-16 11:39:53.797 HelloOOP[23059:a0f] This is Phrase A, Hello!
2011-03-16 11:39:53.799 HelloOOP[23059:a0f] This is Phrase B, How are you?
2011-03-16 11:39:53.800 HelloOOP[23059:a0f] This is Phrase C, I am fine!
2011-03-16 11:39:53.801 HelloOOP[23059:a0f] The string 'This is Phrase A, Hello!' has been printed 1 times
2011-03-16 11:39:53.801 HelloOOP[23059:a0f] The string 'This is Phrase B, How are you?' has been printed 1 times
2011-03-16 11:39:53.802 HelloOOP[23059:a0f] The string 'This is Phrase C, I am fine!' has been printed 1 times

Hopefully this is starting to show the true power of classes. The deeper we get into programming for iOS devices, the more we will uses classes. In fact, the foundation library defines a bunch of classes we are already using, like NSString and NSLog. Almost everything we will use is part of a class somehow!

Next up we are going to be building up our class a bit and using the additions to learn about if Statements, For/while loops, and finally a simple iPhone application. This should get us comfortable enough so we don't feel lost starting the Stanford iOS Development course. If the CS193P section this year is not online, we will use the Winter 2010 one. As a bit of a note, From here on out, I’ll be using Xcode 4 unless for some random reason CS193P uses the older version. Even in that case I may still use it and translate everything into Xcode 4 anyway to get accustomed to it.

So are there any ObjectiveC or iOS topics YOU think we should hit? Let me know in the comments!



L2P: Pax East and Xcode 4

So L2P is going on a bit of hiatus till next Wednesday due to Pax East 2011. I’m looking forward to the developer sessions they are going to have, and some of the other dev centric things going on along side all the awesome gaming!

So whats coming up next for the L2P? We are going to be playing around with our class and expanding upon it, then using it to learn some logic. Then we will build a basic iPhone app before hopping into the CS193P Stanford classes. The good news, the Spring 2011 section of CS193p is starting on March 29th, and I’ll do my best to keep up with them.

Also interesting is the release of Xcode 4. There are some very compelling reasons to upgrade, most importantly that the new LLVM compiler should produce faster code. One surprising thing is that its free to registered paid developers, but its $4.99 though the app store if your not a paid dev. I’ll probably upgrade since I know I’m going to need it, but its a odd move considering it was free in the past. It’s still a steal at $4.99 for what you get. It’s just a bit of a barrier for people to get into iOS/OSX development.

Cya next Wednesday!



L2P: The “Parts” of a Program - Hello World with a OOP Twist, Part 4

We have the project, we have the PrintCount class built, Now its time to use it! After this post, we will have our first, real, OOP program. More »



L2P: The “Parts” of a Program - Hello World with a OOP Twist, Part 3

Today we are going to build the PrintCount class. In the previous post I took you though the steps to get a project started in Xcode and add the appropriate .m and .h files to your project. Those files are going to contain 2 sections of code that will make up our new class. The rest of this post is going to be behind the cut since its rather long. More »



L2P: The “Parts” of a Program - Hello World with a OOP Twist, Part 2

So now it’s time for some fun! In this post we are going to start our first, real, OOP program. We are going to start with getting a new project going in Xcode, then add class. In the next post we are going to actually use that class in the program and play around with it a bit. Since this is a rather long post with pictures, I’m going to hide it behind a cut so click to continue reading.

More »



L2P: The “Parts” of a Program - Hello World with a OOP Twist, Part 1

Programming courses usually do a “Hello World” application as a first application. Personally I dislike the “usual” hello world app because it does not represent a real world application. The ObjC book I’ve recommended before even uses a worse way to introduce a OOP program in my opinion . They do a program that displays fractions using a class. The non OOP version is ~5 lines, the OOP version is ~20. Does that really make you want to learn OOP?

So for this blog, we are going to mix it up a bit. We are going to do a OOP Hello World application. We will be using a class to do 2 actions. The class is going to print a string to the console and count the number of times we print that string.

We will need 2 variables:
1. The string we want to print
2. the count of how many times we print the string

We are going to need the following 3 methods:
1. Set a variable to the string we want to print
2. Print the variable to the console and add 1 to an another variable that counts the number of prints
3. Print the number of of times we printed the screen.

In the next post we will introduce Xcode and build the basic class Then we will use that class to demonstrate the power of OOP. We will also be using this program to start introducing logic and other various ObjC features. And if you all want, we can use the class we will create to our first iPhone app.

Questions? Let me know!



L2P: The “Parts” of a Program - Classes, Instances and Methods, Oh my!

So what makes Object Oriented Programming special? The Object of course! But what is an object? I like to think of an object as a a magic little box that does cool things. Confused? Lets use an a common analogy when you talk about OOP: The Car.
For purposes of this example, lets assume you get your car direct from a factory

When you get a car from the factory, it becomes your car. Your car is unique, once it's created it has unique attributes such as Fuel Level and Mileage. While your car may be your own, the controls are the same for any other car produced in that factory. You can drive it, fill it with gas, change the oil, etc. You don't have to concern yourself with “How” your car works, just that it does certain things when you ask it to.

In our example we are dealing with a car “Object”. The factory is setup to produce a certain style of car, called a “Class”. Once the factory produces our car, or an “Instance” of the car class, it becomes separate and unique compared to any other instance of a car class. A particular instance of car can have many different variables that are unique to only that instance. There are things you can do with your instance of a car, like drive it. These actions are called “Methods” of the class or instance. Now the methods are common to any instance of the same class, but they will only affect the instance you call them on.

The magic with objects are that once we or another programmer define the class, we don't have to worry about the internal code of the class. We simply call the method and instance in the code and get the result. Also we don't have to worry about managing any instance variables inside the class, we can just use methods to use them.

So lets go over this one more time.
A Class is like a subprogram template that can be replicated to create an Instance. That Instance has Variables that are unique to it. Their are also Methods that perform actions on the object, such as processing a section of data, or working with the instance variables. These Methods work for any Instance of the same Class, But only modify the class they are called on.

Objects are a pretty important part of OOP. It may be difficult to understand when you come from a “Procedural” language or a scripting language, but once learned it is a very powerful tool. Next week we will do a quick project to show in code how objects work.

If you have any questions please feel free to ask!



Syndicate content