Object-Orientated Programming (OOP) was the main focus of our first unit in this course. One of the features of OOP is inheritance. Inheritance allows us to be able to take and use methods from a previously written class rather than rewriting it all over again in a similar class. Suppose we made a Shape class which may have a method to get its area. Then we decide to create a Square class, which will have new attributes such as having defined side lengths. However, Square is certainly a Shape and also has an area. By having Square inherit Shape, we are able to use the previously established method to get the area from Shape in Square.
OOP allows us to organize our classes that are similar. For example, a Square and Triangle class both can be organized under an abstract class Square. We can define things that all shapes possess in Shape or at least lay out the foundation to be overwritten in one of its subclasses by implementing a NotImplementedError. Then we will define things specific to Square and Triangle in their own class. By having both of these classes inherit Shape, we can simplify our code by having other parts of our code work for generic Shape objects. Since Square and Triangle are both also Shape objects, any of code that works for Shape objects will work for them. Imagine making thousands of specific card classes for a card game. We would not want to implement code to deal with every single type of card classes. Instead, we can have every specific card class inherit a superclass Card then implement code to handle Card objects.
In inheritance, a subclass inherits methods and attributes from the superclass. It is then possible to extend a method from the superclass in the subclass by reusing the same code from it buy altering it in a way to better suit the subclass. It is also possible to completely override a method from the superclass by rewriting the whole method if a way to alter the original code to fit the new class is not readily available.
I thought the course's approach to teaching us OOP was okay. I generally found the examples to help me better understand what is going on when I decide to read over the course notes. I didn't really like how fast and rushed the first assignment felt. We were still going over many things involved in the assignment during class when it came out. In the end, my group ended up finishing the assignment during the last few days before it was due.
Monday, February 16, 2015
Saturday, February 7, 2015
Tracing Recursive Code (Week 5)
As I had said last week, recursion was a new subject to me. My computer science teacher in high school never even mentioned about recursion. The only thing that I knew about recursion at the start of this course was that Google search easter egg where if you search recursion it says "did you mean: recursion". Moving on, I find that recursion really makes code look simpler but a bit more complicated. I have noticed that all the recursive code that we have traced so far always includes an if structure. I wonder if it is possible to design a recursive function that doesn't use an if structure and doesn't run into an infinite loop.
Anyways, onto tracing recursive code. I didn't really understand the first recursion tracing handout that the professor gave in lecture. Well, I just simply followed along and wrote down whatever was on the screen. When lab03 came around, I decided not to bother with looking at it until the day of the lab. During the lab, I noticed how simple and similar the example tracing were to the first handout. I immediately used the first handout to help me complete the first few lab questions. The recursive code without proper docstrings weren't very different, since I don't always pay attention to docstrings anyways. Tracing these recursive code suddenly seemed so much easier. However, recursion still appears to me as simply a way to complicate and beautify code.
Also, that last question on implementing that length function using recursion was difficult for me. I tried all sorts of ways to do it but failed. Mainly, I was unable to compare the length of the current list to that of each sublists, thus I was only ever comparing the deepest sublists. I eventually gave up on it. It was probably the first time I ever gave up on completing a piece of code. I asked a friend to help explain it to me, which was very helpful. I will try harder next time.
Anyways, onto tracing recursive code. I didn't really understand the first recursion tracing handout that the professor gave in lecture. Well, I just simply followed along and wrote down whatever was on the screen. When lab03 came around, I decided not to bother with looking at it until the day of the lab. During the lab, I noticed how simple and similar the example tracing were to the first handout. I immediately used the first handout to help me complete the first few lab questions. The recursive code without proper docstrings weren't very different, since I don't always pay attention to docstrings anyways. Tracing these recursive code suddenly seemed so much easier. However, recursion still appears to me as simply a way to complicate and beautify code.
Also, that last question on implementing that length function using recursion was difficult for me. I tried all sorts of ways to do it but failed. Mainly, I was unable to compare the length of the current list to that of each sublists, thus I was only ever comparing the deepest sublists. I eventually gave up on it. It was probably the first time I ever gave up on completing a piece of code. I asked a friend to help explain it to me, which was very helpful. I will try harder next time.
Subscribe to:
Posts (Atom)