Ah, Object Oriented Programming. I remember when you were as simple as programming a virtual cash register in CSC108.
The way that Object Oriented Programming was explained to me is that its nothing more than Class Design, at least for the purpose of this course. Originally, class design was one of the topics that I understood the least in 108, but it ended up being the topic I understood the most by this point in CSC148. The concept of class design is simple, at least in my opinion; you create a name for some sort of data type, you determine what variables (usually numbers or lists, at least in this course) are associated with it (usually in the __init__ method), and finally you create an array of functions which perform operations on those variables.
Object Oriented Programming is incredibly useful, because while Python has many different kinds of objects already built into it, it is limited to a handful of general objects, such as integers, strings, lists, etc. With some clever class design, a programmer could implement games, create different data types, and do much more, where they otherwise couldn't using only built-in objects. Working on assignment 2, my partners and I, for example, are adding a game, Tippy, to our already existing game classes. We initialized the board the game is going to use, and we are adding in functions to add moves to the board and to represent them visually to the player.
All in all, OOP is a good way to tackle problems which deal with a central piece of information, like a game board or the contents of a cash register. I'm interested in seeing the many ways we will use OOP in the remaining weeks of class.
Edit: Evaluate this one.
Tuesday, 17 February 2015
Thursday, 5 February 2015
Tracing Recursion
Tracing recursion seems quite mind boggling really.
Think of it this way. There's a base case, where a simple input to a recursive function returns a simple result. This base case is there for two reasons: the first being to trick the tracer into thinking that recursive functions are easy to trace, and the other being that the way a recursive function works is by reducing complicated inputs to that base case.
So what happens if a more complex input is used? Well the recursive function takes that input, performs an operation on it to return a result, takes that result, and uses that as the next input for the recursive function.
Wait, what?
Yeah, it sounds complicated at first, but with a piece of paper and pencil in hand, it becomes rather fun to trace. You basically just keep calling the function until you get to, you guessed it, the base case, and then you pray that the function stops somewhere.
Trust me. It gets easier with practice.
Subscribe to:
Posts (Atom)