Saturday, February 21, 2015

Summary of "OOP"

    Hey everyone! Hope you all are getting the most out of your reading week. This week's blog is supposed to be on the topic; "summary of Object-Oriented Programming concepts".
First before going for the topic I would like to say that if you have not started on the assignment yet, you really should soon! I started working on the paper and just play around with some Tippy Games! It will give you a great idea. Also the more you read the section on Minimax in the handout, the better you'll be able to start working on it! If you think about it, I think Minimax is the coolest thing that we can do by knowing the concept of recursion up until now

Back to out topic I would like to go through some of the definitions and things we came across in OOP(Object Oriented Programming)



What Is an Object?

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

What Is Inheritance?

Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their super-classes, and explains how to derive one class from another using the simple syntax provided by the python programming language.

Creating instance objects:

To create instances of a class, you call the class using class name and pass in whatever arguments its__init__ method accepts.

Accessing attributes:

You access the object's attributes using the dot operator with object. Class variable would be accessed using class name as follows

Class Inheritance:

Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent.

#!/usr/bin/python

class Parent:        # define parent class
   parentAttr = 100
   def __init__(self):
      print "Calling parent constructor"

   def parentMethod(self):
      print 'Calling parent method'

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print "Parent attribute :", Parent.parentAttr

class Child(Parent): # define child class
   def __init__(self):
      print "Calling child constructor"

   def childMethod(self):
      print 'Calling child method'

c = Child()          # instance of child
c.childMethod()      # child calls its method
c.parentMethod()     # calls parent's method
c.setAttr(200)       # again call parent's method
c.getAttr()          # again call parent's method

Tuesday, February 10, 2015

Trees, Linked lists!

    I hope you all had a good week considering all the midterms! So As I talked about recursion in the  last SLOG entry, I thought I talk about trees a little bit.
    To be honest I feel kind of bad that I have a two hour lecture day each week instead of two one hour lectures. It's just harder to keep up with the lap handouts since we get the info of each weeks material one/two day before each lab and in a cold winter's night at MS3150! So I decided to do a little digging myself and find out more about trees. I have used them theoretically before using these two books which I'm pretty sure we will encounter in upper years; CLRS and Intro to Graph theory !

    Since we haven't been thought trees yet, I'm not going to go through What they are and the implementations! but as you get to learn it, your reaction at first might be(just like my first reaction); why go through so much trouble and implementing these nasty things? So I will link below an algorithm called "Red/ Black trees"! I recommend reading the Wikipedia link first "Baby click me please!" and then watch this tutorial type video "watch me"

Sunday, February 1, 2015

On Recursion and Inheritance

I feel like after these past two-three weeks of lectures and tutorials, I have learned some of the most useful things in programming! Recursion is one of the best things that has ever happened to me in my opinion! It converts a crazy problem into only a few lines of code and I would give it a 10/10. I wanted to have some practice problems on recursion for our Midterm1(Good luck everybody) in the upcoming week but since I couldn't find any, I just started implementing code to give me the nth Fibonacci number, the sum of numbers (1^2, 2^2, ...,n^n) and these sorts of things which I suggest you to do if you want to have more practice on this matter.
what is the Fibonacci sequence ?

One more thing I am thankful for is inheritance and I know all of you who took csc108 last term agree with me! I can feel how much knowing how to implementing  sub/super classes would have helped us on out assignments!

I am currently trying to write code to play a simple Hanoi tower puzzle using stacks and it is becoming really interesting! I will show you my code probably after the midterm in the next slog entry! I encourage you to also try it and if you don't know what a Hanoi tower is, check out the following link!
Hanoi tower puzzle
happy studying everyone!