Sunday, 15 February 2015

Summary of Object-Oriented Programming Concepts!

For this weeks post I'll be summarizing object-oriented concepts! In CSC148 we've been using python and it's a very object-oriented language. The course has definitely been helping me understand it a lot more because we've been using it so much in all of our lectures, labs, assignments and tests. I'll first be introducing objects and classes. I'll talk about some important terms related to them, talk about the public interface and then I'll move onto some inheritance.

Object oriented programming is essentially based around objects. An object bundles together data  as well as the methods that can be applied to it. They're stored at a location in memory. In order to actually create an object, we will need a class, which is considered a blueprint for making objects. Each class has attributes (data associated with the class) and methods (or operations). To create an instance (a specific realization of any object) of a class, I have to instantiate it by using the special method __init__ in python. There are many other special methods in python (certain methods available when you use a particular syntax), although in this course we've specifically looked at __init__, __repr__, __eq__, and __str__. We can also override these and define them ourselves if we'd like. In class methods, you always have the parameter self, and this self refers to that specific instance upon which you are calling to use those methods on.

Now that we know what those terms mean, we can now talk about the public interface, which is essentially the features of the class that other code may use. Specifically in python, there's no notion of attributes being private and so it's possible for them to be changed outside of the classes in the client code (the code that runs the classes), which is something we don't want. Something we've learnt in lectures is to use the property mechanism to avoid this by using a specific method to set the value and another to get the value.

Now lets talk about  inheritance. Perhaps we want to create more specific class that has some features of the another class. It would be a bit redundant to retype the entire class again and this can also allow for more room for errors. Instead of doing this, we can create a sub class of the parent class in which the subclass inherits all the methods and attributes that we've assigned for the parent class. It can be thought of as a hierarchy system, with the parent class being a lot more abstract and the sub classes being more specific. For certain attributes you can extend upon them in the sub class and for certain methods you can extend or completely override it if you can't find a way to reuse the code from the parent class.

So here is my short summary of object-oriented concepts. We used it heavily in our first assignment especially when we had to design a program using different classes to handle different games and strategies. I got really good practice because of it by trying to figure out specifically what methods and attributes we had to have and how to organize the program from a more abstract level in the parent class to a more specific one in the sub class. I find it's a pretty neat way of organizing information and very useful as well. Hopefully from my summary you readers can get a good general idea of these concepts as well!

Sunday, 8 February 2015

My Week 4 Recap, Including Tracing Recursion!

Week 4 was a pretty hectic week in every class because of all the assignments and the midterms that were about to happen. CSC148 was no different because of the midterm that would happen the week after and the assignment we had due on Thursday. We also learned recursion which was something we haven't really seen before in CSC108 so that was pretty interesting. Our lab was mainly focused on tracing recursive functions with the last question on  our lab focused on actually writing the function, whereas our quiz was just on tracing the recursive function.

For the assignment, it wasn't so bad that week because my group mainly planned everything out beforehand so we didn't have to rush last minute to get things done, so that made my week a bit easier. We mainly just had to fix up the little things like running it through pep8 for any white space, spacing and all that. All in all I feel we did a pretty good job, and hopefully we do well.

During lectures we looked at recursion, mainly on tracing recursive functions to see the value that would get outputted. We looked at the logic behind it which is focusing on breaking each argument into a smaller problem to try to get the recursive function. I've never really had any experience with recursion so this was something completely new to me. It's pretty interesting because its a lot more concise and elegant than iterative solutions.

Although I found that tracing the recursive function was pretty straightforward, in the lab for the last section where we had to actually write the recursive function ourselves, I found that quite difficult. It was where we had to return the maximum length of either the list or its sub lists. I kept thinking how I would manually trace it back to get each call which ended up making me very confused. I ended up just thinking about how I could break the problem into smaller problems which helped me think a bit more logically.  What I ended up doing was thinking about how if each element was a sub list I would call the function again and from there I would get the max of all those numbers to return.

At the end of the tutorial we had a quiz and I realized I made a mistake on the last question of my quiz for the last question...I guess I kind of got too rushed or panicked and ended up forgetting to check the call on the sub list of the list. I just ended up making the call for the list and totally forgot about the sub list within the list so I know I got that wrong. For the next quizzes I really have to be more careful about things like that.

So that was my week 4! Definitely getting more challenging, but definitely more interesting as the course continues on.

Sunday, 1 February 2015

First Few Weeks (Week 1-3)

The course has been really great these first couple of weeks. Lectures are very interesting so far and I've learnt quite a lot in the course and each week I've encountered something new! Among the many things we've focused on is object-oriented programming and inheritance. We've finished an entire assignment while using those concepts as well!

The first week was pretty good. Mainly review so I didn't have that many questions. It's been a while since I took CSC108 so the first week was a great way of reminding me of things I was a bit unsure of.  An example would be the difference between using is and  ==, which is just one checks their values while the other looks at the memory address.

Object oriented programming was a big thing we looked at it in week two and the overall idea of it was pretty straightforward, but I found implementing it wasn't as easy. During the assignment, the designing process probably took my group the longest time. We had to completely organize things based on their functions and purposes. When we actually got that done, implementing it wasn't as bad. It probably is better that we did plan out some things because if we didn't know what we were going to do and just write the program, then maybe we'd find an error or it didn't really make sense last minute. That would have been a nightmare to change everything around.

By the third week we were mainly looking at inheritance. Again the idea wasn't so bad and it was pretty interesting finding out a new way to organize code so it wouldn't be too redundant. We used inheritance in our assignment too which was great practice of seeing how that worked out. I did learn a new way of writing list comprehensions too which was writing it as one line rather than multiple. We also briefly looked at stacks which at first I didn't really get that much. I thought it seemed very simple and it didn't seem to really do much. Reading through piazza and looking up on google showed that it can be very useful with things such as undoing a command we've entered.

So far nothing has been too overly complicated...but let's hope that doesn't change...