2014年1月19日星期日

CSC148 SLOG - Object-oriented Programming (week 1- 3)

According to Wikipedia, Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods.

Basicly I learnt about how to build a Class object properly and how to use some specific methods and modification tools to make my class object more consistent during the past three weeks. Though creating a class object was from csc108 mainly, I enhanced my skills of creating Class objects during my 148 classes.

I find it very useful and more efficient to write the type contract in the function header. For instance: "def __init__(self: 'Point', coord: [float, ]) -> None: " I was not very comfortable about writing this way at first, but after I practiced over and over again, I realized it is truely a better way to construct the code which saves both space and time.

Exercise 1 and the lab exercise also helped me a lot to improve my way of thinking like a computer scientist. During the lab exercise, I was at first confused about what indeed should I do to arrange the information i got and apply them properly in my code-writing process. I then discussed with my partner and figured out that the first step is to name the class and two crucial methods in a precise and brief way. Then we struggled between whether to make the file we got a list or to make it a dictionary. We figured out it is better to make it a dictionary where keys are the number of occurrence of the words in text and values are the words that occurred that many times. It made everything easier to proceed the first main method which is to get the top n word occurred in the file. With my partner, I learnt how to think independently like a computer scientist and think ahead to make things flow more efficiently.

During the lectures of week 2 I learnt about some ADT types - lists, stacks, dictionaries. Also I gained some knowledge about some comprehensions like [expression for x in items] where x is a variable and items can be a list, dict, set, tuple, etc. 'Ternary if' is also very useful in coding. It appears in form of "expression1 if condition else expression2". It does save a lot more space than writing in old 'if' structure and is easier to understand.

During week 3 I gained some knowledge about how to extend the features of Stack, a class object we created before, by writing a subclass of the existing class. We can name the subclass the way how we want it to function. Then we add conditions to the methods inherited from the original class to create the new methods for our subclass. More excitingly, I learnt that we can even create our own Exception Type. For instance, the PopEmptyException that is raised when try to pop an empty Stack.

I'm so glad that I learnt those new stuffs so that I can apply them to my future coding exercises and assignments.

1 条评论: