Sunday, 22 February 2015

Week 6: Object-Oriented Programming

Object oriented programming is a must known programming style in order to be a better programmer.
Many people recognize this term by thinking of class and subclass, and that is correct.

Object oriented programming is a programming style where programmer initializes data structure, data type and functions that can be applied to data structure while coding.  (For detailed version of this go to the website:  http://www.webopedia.com/TERM/O/object_oriented_programming_OOP.html
OR the wikipedia site: http://en.wikipedia.org/wiki/Object-oriented_programming)

The benefit of object-oriented programming is that there is no need to create new modules every time when new types of objects are added.  If certain objects share same functions or methods, it can be stored in superclass and programmers can use subclass to specify certain functions or methods that make them to be used in their unique ways.  

Object-oriented programming is used big in languages Java, C++, Python, and more. 

In my opinion, object-oriented programming makes coding much convenient and more organized.  
There is no need to create classes every time I need new objects even though some aspects or functions of them can be shared.  

Relating to my course that I'm taking (CSC148), here is an example of convenience of using object-oriented programming:

class Student:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

class Jeff(Student):
    def __init__(self, age, number):
        “””
        “””
        Student.__init__(age, number)
       
class Jae(Student):
    def __init__(self, age, number):
        “””
        “””
        Student.__init__(age, number)

Not using OOP:

class Jeff:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

class Jae:
    def __init__(self, age, number):
        “””
        “””
        self.age = age
        self.number = number

    def get_age(self):
        “””
        “””
        return self.age

    def get_number(self):
        “””
        “””
        return self.number

As you can see, the more function subclasses share, the handier OOP comes in.  Example above only shares 2 functions so it is hard to notice but still you can tell non-OOP ones have longer code lines.  Moreover, non-OOP code lines increases gradually compare to OOP code lines (non-OOP needs to rewrite functions every time in need of new class).   


Therefore, a programmer who wishes to be a better coder and his/her program to run faster and better, he/she will have to be fluent in Object Oriented Programming.





Wednesday, 4 February 2015

Week 5: Recursion function

When I asked my friends about what recursion function was, their response was: "You'll have fun" or "Good luck bro." (What a great thing to hear in the breakfast table before class)
I was little intimidated before I went to class but as the lecture went on, I found it not that difficult.
The concept was easy to understand and it made sense to me.
Though, if someone asked me to write a recursion function at that time, I would have had a hard time doing so.
Basically, what recursion function is a function that does not stop running until it meets certain criteria.
In class, list or non-list condition was dealt.
The function returns a number when its parameter is non-list type and counts the length or gets sum of items in the list if the parameter is list type.

Here is an example to guide you to visualize,


def sum_list(L):
    """
    I'll skip the docstring
    """

    if isinstance(L, list):
        return sum([sum_list(x) for x in L])
    else: #non-list type (e.g. str, int, and more)
        return L


count_list will return the sum of all the items in the list or itself when the parameter is not list type.
sum_list won't stop calculating sum of list until there are no more list type items left in the initial list.

Variation of this would be this:
What would happen if we draw a pointy hill every mid-point of line?
It will look like this picture below. (It is known as Koch fractal)
















After couple exercises of writing recursion functions, I am now a bit more comfortable with recursion functions.

For people who wants to understand better in recursion, watch these videos:
1. https://www.youtube.com/watch?v=72hal4Cp_2I
2. https://www.youtube.com/watch?v=s12IdMeVpw0

Week 4: My thoughts over past 2 weeks of classes

The first thought I had when the class started was filled with confusion, a massive one.
I was used to 108 class style where we learn things through PCRS program.
Unlikely, learning in 148 is based on reading course notes and it took me couple weeks to figure it out.
Good thing about learning materials through reading is that it improves my reading skills and I believe that it is essential in the future where I need to learn other languages by reading books.
Also, it is now possible where I can study ahead of things (limited to what's available on the website though) while PCRS wouldn't let us.
However, PCRS was better in getting us to understand materials better since we got to experience the real coding environment. (Now we need to do it in our own benefits)
To solve this problem, I tend to work on lab problems ahead because I think it really helps me understanding better what we learn in the class.
Also, I had a hard time understanding fully in Classes(self / Superclass & Subclass).
The solution I came up with was…….. Youtube!
(Here is the link to video I learned a lot from in case you experience same problem:
Understanding self - https://www.youtube.com/watch?v=M1BAlDufqao
Super/Subclass - https://www.youtube.com/watch?v=pVjHVfzYKn0 )
Honestly, there are numerous (more than you think of) videos explaining programming language in Youtube.  People are willing to share their knowledge and actually want you to be as smart as them! (seriously.)
Maybe if you feel confident in what you learned in class, try to make a video about it.  When you can explain things to others, that is when you know you understand the material fully.

When the assignment due date was getting closer, my team agreed on start working on it as soon as assignment is posted next time since we had a hard time editing and going through multiple failures.
Even though we did our best to modify our codes and such, we would have appreciated more time.
One time, I was fixing & adding docstrings for strategy class but I could not find my file when I tried to upload it.
When I did find it, the only code I found was blank and I nearly wanted to cry and panic(I only had 2 hours left to submit at that time).
Luckily, I figured that the name of file was incorrect and I had all my works back.
Though, lesson was learned; we will be more prepared for assignment 2 to avoid rushing things.