Thursday, February 12, 2015

Week 6: Object-Oriented Programming and Peek at Tree

This week, I learnt about Tree and recursion. Before going over what I did this week, I want to go over few things about object oriented programming.

So what is object oriented programming? Well, based on my knowledge object oriented programming also known as OOP is a code style in which takes a consideration of class, object, inheritance, and many more. This type of programming mainly focuses on object type which act as a data structure. For class, there are two classes which are parent and child. child usually takes what their parent code originally has and modify it. On my week 4 entry, I listed some example of class inheritance as well as how one can be able to use and implement the code of the parent code into child such as using name_parent.__init__(self, example1, example2). Very often object oriented programming also suggests what kind of function you can use on object type defined by the original creator.

My summary on object-oriented programming is that it may look difficult at a first glance, nevertheless, if you keep practice your code implementing both class and inheritance, you may be able to construct a code in which you do not need to type it over again, since the parent class code has the data that is required for the child class code. Since it is my second time learning object-oriented programming, I found very useful how I can just recall what was used from the parent class code and use it on other class.

One more thing I want to talk about is the use of the default in class inheritance. For instance, let's use an example below which is a typical example of class inheritance which I wrote out.

class ParentClass:
           """ Information omitted
           """
           def __init__(self, example1, example2):
                      """ Type Contract omitted, please refer to the week 3 and 4 entry for writing a
                      doc-string for init method.
                      """
                      self.example1 = example1
                      self.example2 = example2

          def add_it(t):
                      """ Type Contract omitted, please refer to the week 3 and 4 entry for writing a
                      doc-string for this function method.
                      """
                      raise NotImplementedError('Subclass needed')

class ChildClass(ParentClass):   #here, we can see that there is a subclass being used.
           """ Information omitted
           """
           def __init__(self, example1, example2, score=0):
                      """ Type Contract omitted, please refer to the week 3 and 4 entry for writing a
                      doc-string for init method.
                      """
                      ParentClass.__init__(self, example1, example2)

           def add_it(t):
                      """ Type Contract omitted, please refer to the week 3 and 4 entry for writing a
                      doc-string for this function method.
                      """
                      self.example1.append(t)     #assume that self.example1 is a list.


Here, judging from the above example, in ChildClass' __init__ method, there is score which says equal to 0. The use of '=' without using space indicate the use of default value. if the user does not type anything, then self.score automatically is assigned to a value 0. Although I learnt about this in CSC108, I often forget to implement the default method when needed, so just to remind myself again, I put it on this entry.

Now onto this week's materials. This week, I learnt about tree and recursion using tree. What is tree then? The best example of tree coding is Fibonacci tree branches example which helped me how tree actually  works in Python programming. Before going into tree coding, there were some terminologies that I had to be familiar of in order to understand this week's lab. Terminologies are as follows:

Terms to remember
Root: Beginning of the tree
Internal node: node with 1 or more leaves
Leaves: node with no child
Arity: maximum branching factor of  tree
Height: maximum depth of tree

Some terminologies were interesting such as leaves which has no child. At first, I had difficulty of learning these, but thanks to going over some problem during a lab, I was able to understand it completely without a problem. I will give some example using tree recursion on my next entry, so please get ready for it!

Just as a side note, I got my term test 1 and I did fairly well. So I am really happy right now. Starting next week, during a reading week, I am thinking of doing my upcoming assignment 2 for CSC148 class. Of course, I am also going to read as well, since it is a reading week.


                   



1 comment: