Friday, 20 February 2015

Object-Oriented Programming !

So to start off I'd like to say, I initially thought I was writing too much and then did some checking and found out people have been writing far more than I have and far more intensely. Oops ?
Anyway, quality over quantity and all that so I'm going to continue my own thing.
Another thing I'd like to mention is how awesome were the midterm grades ???? I hope everyone did well on it and isn't mad at me for being all cheery about it !
Moving on ..

OOP !
I kind of feel a little silly writing this because let's face it if anyone wanted the definition they'd google it not look at my blog but anyway  Object-Oriented Programming makes use of several concepts all inter connected that we've been using so far in csc148 and even csc108. It revolves around the concept of Abstract Data Type and some of the important terms that one comes across in OOP are object, class, attributes, methods, and inheritance. 
Objects are data structures that contain data and can be assigned to a variable name.
Classes are basically the structure or blueprint or the base of the your program. It is used to perform actions using its methods and attributes,in a cleaner and consistent manner. For creating objects within a class we use the syntax :

                 post = blog(4 , '20th Feb 2015')

Here, 'post' is the variable used for the object,
          'blog' is the name of the class,
          '4' and  '20th Feb 2015' are both attributes 

Inheritance, like its name suggests allows one class, the subclass, to inherit the methods and attributes of another class, the parent class. This is done to avoid redundant and duplication of code. Simply put if there is one class that has methods and attributes in common with a few other classes, it would be more simpler to write that code only once and let it be inherited by the other classes.
For example :

class Worker:
    def update_salary(self, salary):
        self.total_salary = self.total_salary + salary

class HourlyWorker(Worker):
    def calc_wage(self, num_hours):
        self.wage = num_hours * 11.5

class SalaryWorker(Worker):
    def calc_salary(self, commission):
        self.salary = self.salary + (10/100)*self.salary

Here, 'Worker' is the parent class,
          'HourlyWorker' and 'SalaryWorker' are both the subclasses 
          'update_salary' , 'calc_wage' and 'calc_salary' are methods 

Both kind of workers would need their total salaries updated and so that method stays in the Parent class, however the way of calculating said salary is different and so it would be done in separate classes.
The example also shows the syntax that we use to execute inheritance.

So I hope this was simple enough to understand. I didn't try to paste actual code because I'd rather type it out here fast so I hope that didn't bother anyone.

Until next time then..

PS- Doing this during reading week took lots of coffee and a lot of motivation by my friends.
PPS - Is anyone reading this ? ...

xx

Friday, 6 February 2015

Recursion and Stacks&Queues !

So if anyone is actually reading these posts, you probably know by now that I'm not a 'formal' writer.
Especially since we're technically 'blogging', it's impossible for me to write in a style that suits an essay.
Now that that's out of the way..

Tracing Recursion !
Let me start by saying I have some experience in programming since I had C++ in high school.
So when we started recursion I had a mini panic attack. As far as I remember recursion is one of the tougher concepts and I wasn't so keen starting it.
Recursion is basically defining a function in such a way that it works for the simplest problem, and then calling itself so it then works for larger problems of the same kind. (Yes, it is as annoying as it sounds.)
Luckily for us recursion isn't on the midterm completely and only tracing is. I was disappointed in my lab exercise(test?)  since I think I got the idea but I was too hasty and messed up. Anyhow after discussing my answers with other students after the midterm I know I did it right there so yay!
Tracing seems simple enough and one important advise that Diane gave us, that I find extremely helpful is "Don't over think". It really works !

Stacks&Queues..
As I mentioned, I've done C++ and have previous knowledge of how they work. Even though I was sick and had to miss the classes for this particular topic, I found the lab work simple enough to follow. The basic idea behind them is the same and honestly seems even easier in python.

The only thing I worry about is recursion during a test or exam. It always takes several tries (and sometimes not even then!) to get it right and I honestly don't know how we're expected to write code for it without actually running it.
I'm hoping it's a practice thing and I will eventually master the art of recursion.

Assignment 1 and Midterm 1 were sufficiently good for and I have high hopes for my GPA to survive thanks to this course!

Until next time..!

PS - Cannot wait for reading week...