Saturday, 21 February 2015

Object Oriented Programming, Slog #4

Object Oriented Programming, starts off with the concept of classes and objects. An object is something that can perform a set of actions/methods. A class is a blueprint of an object. For example, we can have a blueprint of a plane like the following:

Blueprint of a plane:
-          Wings
-          Cockpit
-          Fuel Tank
-          Instructions on how to combine parts to build plane
-          Etc
Now let us imagine that we have an actual plane. So now we have a blueprint of a plane and an actual plane. The blueprint of a plane is a class, while the actual plane is an object. We say that the real plane is an instance of the plane blueprint. Technically, objects and instances are the exact same thing, but we usually use the word instance to describe an object that is of a class.

Let’s say that we have two classes. One class is the plane blueprint class from above. Another class is blueprint for a fighter jet we have here:

Fighter Jet:
-          Missiles
-          Guns
-          Wings
-          Cockpit
-          Fuel Tank
-          Instructions on how to build fighter jet
-          Etc

Fighter jets are a subset of planes. Fighter jets are planes.  Also notice how Wings, Cockpit and Fuel Tank are all shared by Fighter Jets and planes. We can say that fighter jet is a class that inherits from class plane. So next time we can write a blueprint of a fighter jet like this:

Fighter Jet(Plane):
-    Guns Missiles
-    Instructions on how to build a fighter jet
-    Etc

One last thing to mention is that classes also have methods. On the fighter jet class, we have instructions on how to build a fighter jet. This is an initializer method (__init__) that is called whenever we want to create a fighter jet object. In addition to this method, we can also add methods such as 'Fire Missiles' or 'Fly', that allows this fighter jet object to interact with itself and with other objects.

So after doing some tweaking we have a final Fighter Jet class:

Fighet Jet(Plane)
- Guns Missiles
- Instructions on how to build a fighter jet
- Fire Missiles (This is a method)
- Fly (This is a method)

Why did people invent object oriented programming?

    I’m not an incredibly experienced programmer, so I can’t really give an expert opinion. However, my guess is that objected oriented languages are much more efficient for the programmer. After all, with OOP, the programmer doesn’t have to repetitively write out the same code over and over again (e.g he doesn’t have to keep on rewriting a plane blueprint) when he can just initialize a single object and all the code is automatically done for him.

http://antiaromatic.blogspot.ca/2015/02/lab-5-work.html?showComment=1425265486615#c6709566295146482820

A slog that I commented on. Very helpful info on Lab 5.


Tracing Recursions, Slog #3

    In high school, my teacher made the class write a few easy recursive functions, for example calculating a specific value in the Fibonacci sequence. Some of these programming exercises I completed in high school gave me a half-decent foundation in recursions.

    The recursive functions we learned in class all took in a list as a parameter. In addition they all made use of the isinstance() function and a single if statement so they were pretty simple. The problem I found, was visualizing the function calls. Since we are repetitively visualizing function calls within function calls, it can get quite frustrating to keep track of each call.

     I found that tracing recursive functions becomes much easier if you know how the function works. So what I try to do when I approach a tracing problem, is first to figure out what the function does, and then do the tracing exercises. If you know what the function does, tracing it doesn’t become very difficult. 

    Obviously, the method above only works for very easy recursive functions. Some recursive functions, like quicksort  are much harder to figure out intuitively. Just look at the code we were give in class. 


def quick_sort(L):
    ''' (list) -> list

    Return a list containing the same elements as L in ascending order.

    >>> quick_sort([])
    []
    >>> quick_sort([5])
    [5]
    >>> import random
    >>> L = [1, 2, 3, 4, 5, 6, 7]
    >>> random.shuffle(L)
    >>> quick_sort(L)
    [1, 2, 3, 4, 5, 6, 7]
    '''
    if len(L) < 2: # there's nothing to sort
        return L
    else:
        return (quick_sort([x for x in L if x < L[0]]) + 
                [L[0]] + 
                quick_sort([x for x in L[1:] if x >= L[0]]))



Had there been no description of what this function does, I would be hard pressed to trace a function call. The way to get better at tracing recursions I guess, is probably the same as getting better at proofs in math. Just do more problems.

Reaction to first few weeks and assignment 1, SLOG # 2

Overall, I found that the first few weeks of 148 was not that much more difficult compared to 108.  Still, there is a difference between the courses. For example, assignments and learning styles in both courses were very different. The 148 material seems to be more conceptual and thinking-based compared to 108’s focus on programming knowledge. Also, 148 assignments give students much more freedom compared to 108 assignments. Just take assignment 1 as an example. Assignment 1 was something completely different from any of the assignments in CSC108. In 108, assignments were very rigid and we students were not given much freedom to change anything.  Contrast that with assignment 1, where program design was all on the student.

When I first began assignment 1, I felt a little bit confused since I was used to being given direct instructions. To be honest it was a bit annoying at first, not knowing where to begin and all, but by the end it turned out to be fine. The hints on the sheet helped lot, and once I had a basic design in my head, I became pretty comfortable with the problem.


So after spending 8 hours to finish something I thought would be done in 3. I can say that I feel like programming assignment 1 feels like a consistent process of illusion and disillusionment. When I first wrote out a design for all the classes in assignment 1 I felt that everything would be easy and done within 2 hours. Then I start programming and I see all these holes in my ideas. I go back to the drawing board, redesign a few things and fix my problems. I go back to program and find more problems. Then I go back to my design and it goes on and on and on until eventually I finish. The funny thing is by the time everything actually works, I have no clue what I have done. Well, as long as everything works, I’m happy.