Wednesday, 25 February 2015

Week 7: Summary of Object Oriented Programming

Hi TA!

Please mark this for my week 7 evaluation!

The topic of inheritance wasn't too difficult to grasp for me. It is pretty straight forward and Danny made life easy on me in understanding it  by starting off with simple examples.


This was an in-class example of two classes - Class A and Class B, where Class A had a method g(self, n), where it returned 'n' and another method in f(self, n), which returned the self.g(n). Note the 'self' is included in the parameter of those respective methods to ensure that the first parameter is in the instance of the Class A. Also note that the methods can be re-written as self.f(n) and self.g(n), which makes it a lot less complicated and confusing for me. This is because the first parameter is still an instance of that class.

Class B was a subclass of Class A, which means it inherited the characteristics(methods) of Class A. Class B also had a method self.g(n), but this one returned '2 * n'.

The result? The two print statements which called a.f(1) and b.f(1), printed 1 and 2 respectively.

a.f(1) is quite simple to understand - it directly calls self.f(1) from Class A, which returns self.g(n). self.g(1) simply returns n, which in this case is 1.

b.f(1) is a lot trickier. Despite Class B not directly containing self.f(n) as a method, it inherits self.f(n) from Class A, and then calls self.g(n)  within its own class instead of Class A's g(self.n), thus printing 2 * 1, which is 2. In this case, Class B's self.g(n) is overriding Class A's self.g(n), only focusing on the method that is occurring in Class B

I found an intriguing way to remember the concept of inheritance:

I was sitting next to a buddy of mine and he explained to me to think of the subclass as the son who inherits the characteristics of a father and has his own techniques to surpass him. Needless to say, I'm a big fan of Naruto and in particular the father-son relationship between Naruto and Minato immediately popped to mind! Naruto(son) eventually learns the 'Rasengan' that his father mastered, but manages to surpass his father by learning the powers of the technique through his tailed beast powers. Similarly, Class B inherited Class A's methods, but managed to 'surpass' Class A's self.g(n) by return 2 * n instead of the n that was inherited.

OOP was also heavily reinforced in Assignment 1, where inheritance played a key role in the proper implementation of future games. There were three main classes - GameState (GS), ComputerStrategy (CompStrat) and GameView - the first two both were inherited by SubtractSquareGameState and SubtractSquareComputerStrategy respectively. Even though me and partner could've completed the assignment without either subclass, the profs emphasized the use of inheritance to help us design and implement future games for A2 and A3.

As such, we planned accordingly and used python's 'NotImplementedError' on various methods for the generic GameState and ComputerStrategy classes with a message stating that certain games were not implemented.

Me and partner designed the specific SubtractSquareGameState and SubtractSquareComputerStrategy so that the specific classes were subclasses to the generic ones. This meant that the methods with 'NotImplementedError' were also passed down, but we used the concept of overriding to complete our specific methods as requested from the assignment sheet.  It was a fantastic learning experience.

Thursday, 19 February 2015

Week 6: Object Orienting Programming (Mark for Week 7)

The topic of inheritance wasn't too difficult to grasp for me. It is pretty straight forward and Danny made life easy on me in understanding it  by starting off with simple examples.


This was an in-class example of two classes - Class A and Class B, where Class A had a method g(self, n), where it returned 'n' and another method in f(self, n), which returned the self.g(n). Note the 'self' is included in the parameter of those respective methods to ensure that the first parameter is in the instance of the Class A. Also note that the methods can be re-written as self.f(n) and self.g(n), which makes it a lot less complicated and confusing for me. This is because the first parameter is still an instance of that class.

Class B was a subclass of Class A, which means it inherited the characteristics(methods) of Class A. Class B also had a method self.g(n), but this one returned '2 * n'.

The result? The two print statements which called a.f(1) and b.f(1), printed 1 and 2 respectively.

a.f(1) is quite simple to understand - it directly calls self.f(1) from Class A, which returns self.g(n). self.g(1) simply returns n, which in this case is 1.

b.f(1) is a lot trickier. Despite Class B not directly containing self.f(n) as a method, it inherits self.f(n) from Class A, and then calls self.g(n)  within its own class instead of Class A's g(self.n), thus printing 2 * 1, which is 2. In this case, Class B's self.g(n) is overriding Class A's self.g(n), only focusing on the method that is occurring in Class B

I found an intriguing way to remember the concept of inheritance:

I was sitting next to a buddy of mine and he explained to me to think of the subclass as the son who inherits the characteristics of a father and has his own techniques to surpass him. Needless to say, I'm a big fan of Naruto and in particular the father-son relationship between Naruto and Minato immediately popped to mind! Naruto(son) eventually learns the 'Rasengan' that his father mastered, but manages to surpass his father by learning the powers of the technique through his tailed beast powers. Similarly, Class B inherited Class A's methods, but managed to 'surpass' Class A's self.g(n) by return 2 * n instead of the n that was inherited.

OOP was also heavily reinforced in Assignment 1, where inheritance played a key role in the proper implementation of future games. There were three main classes - GameState (GS), ComputerStrategy (CompStrat) and GameView - the first two both were inherited by SubtractSquareGameState and SubtractSquareComputerStrategy respectively. Even though me and partner could've completed the assignment without either subclass, the profs emphasized the use of inheritance to help us design and implement future games for A2 and A3.

As such, we planned accordingly and used python's 'NotImplementedError' on various methods for the generic GameState and ComputerStrategy classes with a message stating that certain games were not implemented.

Me and partner designed the specific SubtractSquareGameState and SubtractSquareComputerStrategy so that the specific classes were subclasses to the generic ones. This meant that the methods with 'NotImplementedError' were also passed down, but we used the concept of overriding to complete our specific methods as requested from the assignment sheet.  It was a fantastic learning experience.

Tuesday, 10 February 2015

Week 5: Impressions on Recursion

While I was given the recursion exercise handout sheet from Danny Heap's 10-11 am lecture, I was immediately frightened as to what was going to happen with this topic. I received the page from the back side and when I look at those questions, I was intimidated and lost my motivation for a brief period of time because it seemed all too impossible to learn within a 50 minute lecture. However, just like the complex math concepts like derivatives and integrals, if you showed those to me before I knew the basics, I would show the same signs of 'fear' that I would've when I saw the recursion exercise sheet. Recursion is all about learning from scratch and applying what we've learned to solve more complex problems.

Danny introduced us to the concepts of 'depth' in recursion and how 'depth 1' lists builds upon further depth lists with the same concept. The difficult part to grasp was depth 0 (empty lists), but that was quickly cleared up as Danny worked through the process with the class. Recursion is pretty useful to solve those lengthy tracing problems, but the concept of finding a 'depth 1' list and further applying it to bigger lists stuck with me throughout the week and I did well on the weekly quiz and exercise. Essentially, instead of re-writing each function call manually, you can simply just apply what you learned from the depth 1 list and apply it to sub-lists. I feel tracing recursions is one of my strengths, as long as I don't panic at first sight with a huge problem like I did the first time! Recursion will be a key concept within the course, so it is important that I stay on top of it for the rest of the semester to excel in the course.