Sunday 22 February 2015

A Summary of Object-Oriented Programming and Abstract Data Types

Object-oriented programming, as its name suggests, deals with objects that contain data, and  code, which is implemented as methods.  In this course, we dealt with classes and inheritance. 

A class is an object that contains code in the form of methods, that can be implemented for any instance of the class.  An example of a class and its methods is a class for a day planner.  Every instance of the class is a day, and the class might contain methods that add an event, remove an event, and one that checks to see if any events overlap, in addition to an initializer method which creates instances of the class.  These methods execute properly for any instance of the class (which is a day) and this class structure is much more efficient than writing separate functions that are not connected as it often requires less code and is more ordered. 

Some problems, however, require more complex solutions that just one class.  In the case where instances of a class share methods, but not the implementation of those methods, inheritance may be used.  A simple example of inheritance that deals with shapes was shown in lecture.  The shape class has a method that initializes a shape, one that stores the starting coordinates of the shape, one that calculates the perimeter, and one that calculates the area.  While all shapes share these properties, only the method that stores the starting coordinates of the shape will execute properly regardless of the type of shape created.  Both the perimeter and area methods are specific to the type of shape created.  In this case, subclasses for each shape are created and inherit shared methods from the general shape superclass.  The intializer and coordinate methods would be implemented in the superclass because they are shared with all shapes, while the perimeter and area methods would be left unimplemented in the superclass and would raise an error if called saying that a subclass is needed.  In the subclasses, the coordinate method would not need to be implemented because it is inherited from the superclass, but the area and perimeter methods can be implemented differently in each shape's subclass.  This process is much more efficient than creating separate classes for each shape, especially in larger cases, because it prevents duplication of shared code (the coordinate method would have to be duplicated in every separate class without inheritance) and is more organized. 

This course also introduced three new abstract data types so far: stacks, queues, and trees.

Stacks are a last-in-first-out data type that can be envisioned as a stack of paper on a desk.  Items can be added on top of the stack, but only the top item can be removed first, or the stack of paper will collapse.  Generally, stacks have three key methods aside from the initializer: a method to check if the stack is empty, a method to add an item on top of the stack, and a method to remove and return the top item on the stack. 

Queues are similar to stacks except they are a first-in-first-out data type, similar to a line at a cashier.  They contain the same key methods as stacks, except the implementation is slightly different. 

Trees are a recursive data type, meaning each tree is made up of smaller subtrees.  An example of a tree can be created by flipping a coin and recording the possible results.  After one flip, the possibilities are only heads and tails, after flipping the coin again, each possibility from before now also has two possibilities.  Due to the recursive nature of trees, many of the methods implemented in a tree class also require recursion, such as determining the end values of the tree (the leaves) the number of intermediate values (nodes), or the maximum height of the tree. 

Sunday 8 February 2015

Tracing Recursion

When recursion was first introduced in lecture, I was a bit confused about the syntax of tracing it, even though I understood the concept.  The handout that we completed together in lecture was very helpful as it gave me concrete examples to work from when tracing other recursive functions.  As well, the lab exercise that actually required us to write a recursive function was extremely useful as it gave a further understanding as to how recursive code works.  After the lab exercise and quiz, I felt comfortable with tracing recursion and had no problems completing the test question that dealt with this topic.