Object oriented programming (OOP) is based on creating objects and writing methods to create data structures that can store and manipulate data. OOP allows the encapsulation of data, meaning it allows us to restrict the access to some of an object's components. Python provides the property mechanism, which re-directs access to an attribute. So for example, you could make it so an object's attributes cannot be changed once it has been set once.
An abstract data type (ADT) is a model of a data structure defined by the methods/operations that it can perform (the semantics) rather than code details of how they are implemented, so ADTs can be implemented in multiple programming languages. In lecture, we have explored 3 specific ADTs: stacks, queues and trees.
A stack stores items and has an operation to remove (pop) the top item, an operation to add (push) a new item to the top of the stack, and a way to tell if the stack is empty. It operates in a "first in, last out" fashion, and can be compared to how a stack of books or plates would work in real life.
A queue stores items and has an operation to remove (dequeue) the bottom item, an operation to add (enqueue) a new item to the top of the queue, and a way to tell if the queue is empty. It operates in a "first in, first out" fashion, a lot like a real life queue of people lining up at a ticket booth of some sort.
A tree is a storage of objects on which a hierarchical ordering is placed. Each object in a tree is a node. A tree has many operations, such as access to the root node, determining the height of a tree, the depth of a node, the number of children etc.
When working with classes, sometimes a new class that adds a few features to an existing class is required. Inheritance can be used to create a subclass of the existing parent class that inherits all the methods and attributes of the parent class. This way we do not have to rewrite the code again, and can just focus on writing the additional features that we want.
I personally find OOP and ADTs to be pretty amazing because of how it allows countless possibilities. With OOP, you could create any kind of object you want to do whatever you want so that you are not just limited to default object types built into a programming language such as lists, strings or dictionaries in python. OOP has a clear structure, making it simple for you to code things, and easy for anybody else that will be using your code to understand what it does. You can easily modify or create similar objects. I have always had an interest in video games, and learning about ADTs made me look back on games I used to play, and recognize ADTs - mainly the concepts of stacks and queues being used as inventory slots to store items that are used in the game. As well as this, our CSC148 assignment 2 asks us to implement a new strategy for games that looks at all the different possible moves and chooses the best one - the concept of trees. All of this is of great interest to me, and I am having a lot of fun in the course learning all these things and establishing a better understanding about how things that I found so awesome on the computer work.
Saturday, February 14, 2015
Friday, February 6, 2015
Tracing Recursion (Week 4)
During week 4, we were introduced to recursion - functions that call themselves. I looked recursion a little during my spare time in the Christmas holiday before the term started, and found it quite confusing. However, the lab exercise provided an opportunity to look at and trace through many examples of recursive functions and even have a go at writing a recursive function, which allowed me to establish a far better understanding on the topic. I have no trouble tracing recursive code in my head now. However, I haven't exactly gotten the hang of writing out the all of the steps and function calls when tracing code. I thought that being able through it in my head and figure out the output of functions would be enough, but I realized I was mistaken when I got my lab quiz back and I had lost half a mark for not writing down the steps. I think this may have cost me some marks in test 1 as well.
I plan on looking back at the lab exercise examples to familiarize myself with the format for writing down the steps when tracing recursive code so that I will be prepared if questions like that were to come up again in the future. I haven't done this quite yet because I have more midterms coming up, but I will do so as soon as I find the time.
I plan on looking back at the lab exercise examples to familiarize myself with the format for writing down the steps when tracing recursive code so that I will be prepared if questions like that were to come up again in the future. I haven't done this quite yet because I have more midterms coming up, but I will do so as soon as I find the time.
Subscribe to:
Posts (Atom)