Saturday, 14 February 2015

CSC148 - Entry 4

Entry no. 4, Week 6:

Topics

1. Summary of Object-Oriented Programming concepts


This week, the topic is about object-oriented programming which is something that I am already very familiar with. Since this is a very broad topic, I will be talking about Instances, Abstract classes and Inheritance. The examples will be in Python3.

First, let's define a class. A class is a representation of an object. They are given names and are composed of attributes and operations (methods) that can be used by several Instances of the same class.

Instances are objects and are occurrences of specific classes. They use the methods that are defined within the class, and also have unique variables that store information. (disclaimer: that does not mean that there can only exist unique variables, but that there are also exist variables which can be accessed throughout all instances of a class.) Instances of classes are frequently used in programming and play a large role in creating interfaces.

For example, we can create a class Student that manages the information and actions of a student. We can give that Student a name and attributes by creating an instance of Student, we can tell the student to enroll in courses and we can take a look at their grades.

Abstract classes are classes that aren't meant to be used. Within these abstract classes, there exist properties and methods as well. Even though these are defined, they represent the framework for subsequent classes. That is, they are meant to be used in subclasses that will extend the functionality of the abstract class. The way that these subclasses extend functionality is called Inheritance.

Suppose we had a class called Vehicle which is a thing used for transporting people or goods. This is an example of an Abstract class because there are many vehicles, but they are all different in their own respects. Subsequent classes may include Cars, Boats, Planes...etc but they are all derived from the same Abstract class Vehicle. 

In one of my previous post's I briefly talked about Inheritance which mentioned method Over-riding, Extending, and Inheriting. Over-riding is a process where an entire method is re-written to fulfill the needs of the current class. In abstract classes, methods that were not fully defined would be over-written and defined in the sub-class. Extending is a process where a sub-class would use a methods from the super-class in the definition of the current method or define a completely new method that is semi-related to the super-class. Inheriting is taking a method that is already defined in the super-class and using it in the sub-class, such that it does not need to be defined in the sub-class.

Still using the Vehicle class, suppose we made a subclass Bus. We would over-ride the __str__ method because the string representation for the two would be completely different. We could also extend the Bus by using the __init__ method from Vehicle, but by adding fare and totalFare attributes, and adding methods passengerOn and passengerOff. The bus would inherit the method move, since Vehicles Buses both move.

Sunday, 8 February 2015

CSC148 - Entry 3

Entry no. 3, Week 5:

Topics

1. Impressions on Recursion, and Tracing Recursion (from Week 4)


Recursion isn't too foreign to myself and some others in this course as we have encountered it before. On my part, I haven't done recursion for a while (2 years give or take a few months) so it was something that I had to get the hang of again. The practice and lab for recursion was straightforward and simple enough for me to get my gears going again. For those who don't have a great grasp of or don't know much about recursion, it is a series of repeated applications of a procedure or definition that re-evaluates until certain criteria are met.

For example of what recursion can look like, below is part of an exercise from a lecture.



At a quick glance, this code accomplishes very much with very little amounts of code. That is, it can sum any size list of ints (nested lists importantly) with 4 lines of code without comments.

What's really key is writing about the criteria needed for the call to start recursion. In this case, if the input L is a list then it calls itself again (that is, it calls sum_list(x) for each element in L) within a List-Comprehension that is later summed. Another key part is that this function returns the input L if the input is not a list. These two parts together will eventually forge and sum a list of Depth-1 thanks to repeated calls on sum_list and List-Comprehension.

Monday, 2 February 2015

CSC148 - Entry 2

Entry no. 2, Week 4: First week impressions

Topics

1. My impression of the first few weeks


Going into the course, the pacing of the lectures felt a tiny bit slow to me, but that much was to be expected with the amount of new-comers from CSC108. Even though the lectures were slow, there were a few interesting concepts that I hadn't come across yet. 

A really useful concept was List-Comprehension, where new lists can be created based on other lists. 
For example in Python it looks a little bit like this.

>>> L = [1, 2, 3, 4].
>>> S = [ x ** x for x in L]
>>> S
[1, 4, 9, 16]

In addition to basic mathematical operators, well-defined functions or methods can be called on the elements inside as well.

>>> from math import sqrt

>>> L = [1, 4, 9, 16]
>>> S = [sqrt(x) for x in L]
>>> S
[1.0, 2.0, 3.0, 4.0]

Another thing that caught my eye was that Python didn't really have a "private" feature like other languages do. A single underscore ('_') on instance variables and methods coupled with the use of property(<fget>, <fset>, <fdel>, <doc>) acts as a way to redirect the access of information from the general public.

Other things we tackled over the first three weeks were Inheritance and ADT's, both of which are less new to me than the above topics. Below is a short run-down of what they are.

Inheritance is the idea of an Initial class (Super Class) giving its properties and methods to Subsequent classes (Sub Classes) where the Sub Classes are free to change (Over-ride), add to (Extend) or simply take (Inherit) from the Super Class.

ADT's  (Abstract Data Types) are ways of storing information. So far, we looked at Stacks, which is LastInFirstOut (LIFO) type of data type. With a stack, users can push in objects, pop the top-most one out, or just peek at it without popping it out.