Monday, 16 February 2015

Summary of Object Oriented Programming

Over the past couple of weeks, we have been working on object oriented programming, which is based on the concepts of "objects". Objects are data structures which contain data in the form of attributes and methods. On a fundamental level, the concept of object oriented programming does not seem so difficult. You build a program which uses objects that interact with one another. The objects are instances of classes. 
In the first few labs, we learnt to design classes, which was an extension of the material from CSC108. Then, we learnt about inheritance, ( a new topic for me), where a subclass is based on another, and we worked on desigining these subclasses.  I think that the worksheets and labs we did were straightforward, where we only focused on designing one or two particular classes. It was easy to get the hang of these problems and perfect the class design. 
The real tricky part came when it was time to do Assignment 1.  This was the first time that I noticed the sheer difference in the transition from 108 to 148. Instead of being spoon fed and told what to do for each and every function, we had to do the hard thinking ourselves. I read through the assignment multiple times, and was still having trouble deciding where to begin. Designing separate classes is easy, but when it comes time to put all this information together and make a program which is supposed to play a game, it takes a lot more intuitive thinking.  The information all made sense to me, but it is crucial to have a perfect understanding of all the classes and how they will be working together, before writing any code. This is one of the mistakes I made when I first started working on the assignment. I tried writing out code first. Then I realized I was making my generic Game State class specific for Subtract Square. I think for object oriented programming, you need to think on the broad scale, generic level, before working on any of the inherited classes. This works like a hierarchy.  I made a flowchart for myself, which I think helped me out the most, and I will be doing this for future assignments. I thought about the main program and how the classes would interact (ie. how the game state and the strategy would be chosen), then i thought about the generic classes, then about the subtract square subclasses. Once it comes time to write the actual code, it was not as difficult, as long as you have a proper layout. This way you avoid running into any confusion, because you could always look back to the flowchart. I think the reason that we were told we must implement __init__, __repr__ and __str__ methods for all of our classes is that these our the most important things to think about when designing the class. If you have a good understanding of how the classes is initiated, and how it would be represented, then you can think about all the other methods or attributes of that class. 
In conclusion, I was really happy with object oriented programming. After finishing up the game and playing subtract square a couple of times, I was happy to see how much I had learnt and how much progress I had made. 

Sunday, 8 February 2015

Tracing Recursion

Recursion has definitely been the biggest challenge so far in 148, and it definitely took me a while to wrap my brain around the concept. I started off having recursion explained to me by a friend in an upper year. As she started explaining, she started stumbling over her own words, and explaining the concept seemed to be confusing her (even though she is a master of recursion). You can appreciate how difficult it may be, if someone who understands it still has a hard time explaining it.
In simple terms, I would define a recursive function as one that calls upon itself within the body of the function. While this may not seem so difficult, it is of crucial importance to be able to learn how to trace a recursive function before you can actually start writing one. (I tried to do the latter first and my brain almost exploded).
The first couple of worksheets we got in class gave a good outline of the steps you would take to trace the function. We had to write out each step of the recursive call. After doing a couple of the same types of problems in our lab, it became easier to understand the concept of recursion. (As everyone says, practice makes perfect).  It is always necessary to break a big problem down into smaller steps. Once you master these elementary steps, you grasp a better understanding of the concept at hand. This is the way that I break it down for myself now:  The recursive function always has a base case, which is a condition that allows the function to stop recursion. If you know what the base case is, you know what the "end goal" of the function is, and what it should return for the simple problem. In terms of tracing, every call of the function is going to move closer to the base case. That means, every time the function is called it takes a new parameter which is pretty much a broken down version of the first parameter. What appeals to me is that even though it is a difficult concept, the code for a recursive function is very clean and concise (imagine having to rewrite the code over and over instead of just using recursion).
Even though I have tracing down, I am eager to see how I will do in terms of actually writing recursive functions.