Sunday, 15 February 2015

Object-oriented programming concepts (week#6)

Object-oriented programming(a.k.a. OOP) is a programming paradigm based on 'object'. It's just like a model of the objects in the real world. We generalize and abstract the real-world objects, and then define them in classes using attributes and methods.

In our daily life, every object has its properties(states) and behaviors (for example, dogs have properties having claws and fur, and they have behaviors bark, eat and run), so does the 'object' in OOP. Basically, it has two main concepts(components):

1. Attributes(Properties)
2. Methods(Behaviors)

These two components are usually presented in the module of the objects -- class

As is taught in the lectures, when we see a handout for coding using python (a typical object-oriented language), we firstly pick up all the verbs and nouns, and then analyze them -- the verbs as methods and nouns as attributes.

Similarly, the first step in OOP is to identify the objects we want to manipulate and analyze how they are related to each other. Once an object has been identified, we generalize it as a class of objects.

Objects are instances of classes, and classes determine the type of objects. To be more specific, let's see the dog example I mentioned before:

Name of the class: Dog
Attributes: claws, fur
Methods:   bark, eat, run




In addition, OOP has a very important and useful property, that is, inheritance.

Inheritance makes it possible to define subclasses of data objects that share some or all of the main class characteristics without rewriting them.

Let's continue the dog example:

Subclass of the class Dog:

Name of the class: PomeranianDog
Attributes: small claws, golden fur, smart
Methods:   bark, eat, run, play

In the example, the Pomeranian is also a dog, and it has all the characteristics of dogs, so it inherits many things from its superclass (Dog) such as attributes claws, fur and methods bark, eat and run. However, since it is a specific kind of dog, it has more detailed things than a dog, which includes small claws, golden fur, smart and play. A subclass inherits some(or all) attributes and methods from the subclass and then extents or modifies the attributes and behaviors.

Inheritance provides OOP many benefits:

1. A programmer can write a subclass inheriting the common features from the superclass instead of changing the module or rewriting all of the code, which makes object-oriented programs easier to modify as well as saves a lot of time and effort.
2. It is reusable not only by the program for which it is initially created but also by other object-oriented programs, so that it is easier to distribute for use in networks.
3. It uses encapsulation(modular objects have their own data and are responsible for their own behaviors), which means changes in one object do not affect other objects, providing greater system security and avoids unintended data corruption.


references:
1. Celina's SLOG: My summary of object-oriented programming concepts
http://celinajiangslog148.blogspot.ca/2015/02/week-6-my-summary-of-object-oriented.html

2. Wikipedia: object-oriented programming
http://en.wikipedia.org/wiki/Object-oriented_programming

3. Margaret Rouse: object-oriented programming
http://searchsoa.techtarget.com/definition/object-oriented-programming

Monday, 9 February 2015

recursion (week #5)

Last week we talked about recursion and had a little practice in the lab.

Recursion means 'Defining something in terms of itself'. It works by calling itself for solving smaller subproblems.

It's very useful as without recursion, code can be very complicated, while recursion reduced the lines of code.

After practicing, I find myself comfortable with tracing a recursion. First of all, we work on the simplest example, and once we convinced about it, we can apply it in a more complicated case directly instead of breaking the complicated cases  and tracing the subproblem of it again.

But I still have some problem writing the recursion and I'll keep practicing it.

Sunday, 1 February 2015

Week #4

About working in pairs and the assignment:


In the 4th week, we finally handed in our assignment 1. It's a big jump to compare with CSC108. In CSC108, all the docstrings of required functions were given and we just needed to implement the bodies and add some helper functions. However, in CSC148, we need to decide which methods we need and combine them, which was kind of hard, but also, what makes computer science course more interesting. Designing a program by ourselves is much fun! I can't help asking my friends to try the Subtract Square Game out!

I have to tell that it's such a good idea to work in pairs! First of all, my partner did programming for a year before and he's very good at writing code. The problems that were difficult for me seemed like a piece of cake for him. I learn a lot from him. For example, I had no idea of how to swift between two players, but he told me that we can change the player's turn by keeping track of odd and even numbers, which I believe is very useful and will be used in the future. He is also very patient and careful. Before working with him, every time when I worked on a program for about 3 hours, I had a rest, especially when I could't find the bugs and then felt upset. But he didn't stop until he tackle a problem. That made feel ashamed and I'm trying to do the same thing with him. Secondly, discussing during working together enhances our ability to communicate as well as helping with sparking good ideas. After discussing, we had a better understanding of the handout and found better ways to write the code. I really enjoy working with my partner and appreciate for it.

During the assignment, we found that we were not very comfortable with the special methods __str__, __repr__ and __eq__. Following are some notes (posted by the instructor on piazza):

These methods are most often used by the developer for debugging.  It is often helpful, as a sanity check, to test whether two objects are equivalent (or not).  If the result of that check is surprising, you'll often want to see the output of __str__ to confirm what __eq__ says.  Sometimes the output of __repr__ is useful to allow the developer to cut-and-paste into a Python interpreter in order to "poke at" an instance of a class.
This assignment involves a text-based game interface, so a well-written __str__ may help you implement this.
Finally, we instructors think that these methods are an important part of good code development.  So we require you to do them.