2015年2月25日星期三

Summary of Object-Oriented Programming Concepts(Week 5)

  It's my first time to know object-oriented programming(also known as OOP) concepts. We already have used the knowledge about this while doing assignment 1.

  "In object-oriented programming, computer programs are designed by making them out of objects that interact with one another." said by two computer scientists. In CSC108, most of the time, we were using procedural programming, which mostly focuses on writing functions. However, as these two scientists concluded, OOP mainly concentrate on both data and functions, which means class (including superclass and subclass), inheritance, interface and methods are of vital importance.

  According to the sentence mentioned before, the very first thing we need to know is "object". Obviously, "object" is the key to understand OOP. Object has its won properties and state, just like every object in our daily life. Also, object could expose its behavior through methods.

  Secondly, what is "class". Class is the blue print of a program. It help us organize our thoughts and makes your codes readable for others. It's like building a house, class is the frame. After building the frame, we need to put things in the frame, which are our functions. 

  Thirdly, speaking of class, one of the most important thing is the relationship between superclass and subclass. They are like parents and children respectively. Children could inherit all the characteristics of parents,which is known as inheritance, and of course, develop their own uniqueness. In my opinion, superclass-and-subclass relationship is one of the ways that make interaction between objects possible.

  The last, "interface" and "method". An interface is a contract between a class and the outside world. Objects define their interaction with the outside world through the methods that they expose. We've already learnt four main methods which are the most common form -- "__init__", "__str__", "__repr__" and "__eq__". Each of them has its own unique role. "__init__" is used for initializing, "__str__" is for printing, "__repr__" is for evaluation and "__eq__" is for comparison. All of them make the class more formal about the behavior that it acts.


  There are a lots of things we need to learn about OOP and I believe it's the main target of CSC148. Although it's kind of difficult at first, I still find it interesting and I'm looking forward to learn more about it.

2015年2月8日星期日

Impression of tracing recursion (week 4)

  This week and last week we learnt recursion and tracing recursion. 
   It is amazing, for example, the method calls itself in its body, which makes a loop. It is more convenient than to use recursion than for loop or while loop, however, the thinking process is much more complicated than for loop and while loop. 
   I found myself have trouble writing complex recursion function, but I realized tracing recursion is quite useful. I guess that's the reason why we learnt how to trace them at first. 
   Here are the details and steps about my solution. When I encounter some problem when writing recursive function, I try to "tracing" it firstly. For instance, try to figure out what I want at each step. For most of the time, I need to guess with my instinct, then trace it. When error occurs, I stop tracing and then fix my guess then restart. After several loops, I can find the right answer. However, it's kind of time-wasting, but is still quite useful.