Object-oriented programming (OOP) is a style of programming which, as the name suggest, is based around using objects. An object is an instance of a class, which can either be a basic class defined by the programming language, or a new class created by the programmer. OOP focuses on using custom classes to write code in a more intuitive way.
Each class can have attributes and methods. An attribute is another object; it could even be the same type of object, as in trees. Having all the relevant data bundled into a single object makes it easier to work with, rather than keeping track of a group of different variables and what they refer too. A method is a kind of function associated with the class. By importing a class you can then also gain access to all the methods of that class. A method is typically something that every member of that class should be able to do: for instance any Rectangle object should be able to compute its area (assuming it had length and width attributes).
An important part of OOP is subclasses. A subclass is a specialized version of another class. It might have more attributes, more methods, or even alter the methods of the parent class. Subclasses are used when there is a conceptual link between two or more types of objects. Many times the parent class is not actually usable, but by using multiple subclasses of it, code can become simpler. If the parent class has a method, your code can call that method on an object without knowing which of the subclasses that object is (of course you have to be sure that it is one of them rather than another class or None). Classes and subclasses make code easier to understand and are useful tools for programmers.
Geoffrey's SLOG
Saturday, 28 February 2015
Sunday, 8 February 2015
SLOG Week 5 - Tracing recursion
Recursion is similar to using helper functions. In both cases, you use a function to accomplish part of the main function's task. The difference is that a recursive function is its own helper function. Obviously this could cause an infinite loop, so the recursive function must be designed so that it will eventually not call itself again - that is, it can figure out the return value on its own. This is the base case.
In the examples we've worked on, this takes the form of an if-else statement, where we check to see if this is the base case (in several examples, the base case was when the input value was not a list), then return something simple - the input value, or a specific int if we are counting something.
Tracing recursion is a way to understand recursion for humans. By working out what a function does for the base case and simple cases, we know we can trust the function for these cases, allowing us to quickly work out what the function does for more complicated cases.
In the examples we've worked on, this takes the form of an if-else statement, where we check to see if this is the base case (in several examples, the base case was when the input value was not a list), then return something simple - the input value, or a specific int if we are counting something.
Tracing recursion is a way to understand recursion for humans. By working out what a function does for the base case and simple cases, we know we can trust the function for these cases, allowing us to quickly work out what the function does for more complicated cases.
Sunday, 1 February 2015
SLOG week 4: thoughts about first weeks
In the first three weeks of class we studied classes and subclasses. This has not been too difficult for me; I think these concepts are logical. This past Thursday the first assignment was due, which I worked on alone.
There is something interesting I did with my implementation of the assignment. This has to do with the __init__ method of the GameView class. While writing it, I was trying to think of what attributes a GameView should have. The answer I came up with was: which game is being played. But the identity of the game is in fact another class! So my GameView class has a single attribute Game, which is capitalized because it is an attribute which represents a class. If a GameView gv is initialized with the SubtractSquareState class, for instance, calling gv.Game() will initialize a SubtractSquareState, as if SubtractSquareState() was called. Of course, there are plenty of other ways to accomplish this, but I was curious whether this would work. It did, so I left it in.
As for week 4, we have started learning about recursion. This is more tricky than the material of the first three weeks, but I think I understand it. Next week is the first test, which I am optimistic about.
There is something interesting I did with my implementation of the assignment. This has to do with the __init__ method of the GameView class. While writing it, I was trying to think of what attributes a GameView should have. The answer I came up with was: which game is being played. But the identity of the game is in fact another class! So my GameView class has a single attribute Game, which is capitalized because it is an attribute which represents a class. If a GameView gv is initialized with the SubtractSquareState class, for instance, calling gv.Game() will initialize a SubtractSquareState, as if SubtractSquareState() was called. Of course, there are plenty of other ways to accomplish this, but I was curious whether this would work. It did, so I left it in.
As for week 4, we have started learning about recursion. This is more tricky than the material of the first three weeks, but I think I understand it. Next week is the first test, which I am optimistic about.
Thursday, 22 January 2015
Why geeks need to know how to write
Let me start by saying that I don't really write much. So I think I will rephrase this more personally: why should I write more?
Writing is an extension of speaking, essentially. But it's in a way that's permanent. Writing is therefore a sort of extended interaction: with speaking, what's said is said; but when something is written down, it can be read again, and by many other people, potentially long after the writer is dead. The important thing is that writing is interacting with people in a much more flexible way than speaking.
When you get down to it, an vital part of any vocation is interacting with people. So the better I get at that, the better I will be at whatever I end up doing. It probably won't be computer science - I'm don't have any current plans to take any other computer courses - but whatever it is, it will involve other people, because everything does. And communicating effectively with those people will mean writing. That's why geeks (myself included) should know how to write.
Writing is an extension of speaking, essentially. But it's in a way that's permanent. Writing is therefore a sort of extended interaction: with speaking, what's said is said; but when something is written down, it can be read again, and by many other people, potentially long after the writer is dead. The important thing is that writing is interacting with people in a much more flexible way than speaking.
When you get down to it, an vital part of any vocation is interacting with people. So the better I get at that, the better I will be at whatever I end up doing. It probably won't be computer science - I'm don't have any current plans to take any other computer courses - but whatever it is, it will involve other people, because everything does. And communicating effectively with those people will mean writing. That's why geeks (myself included) should know how to write.
Introducing me
So this is my SLOG for the course CSC148 at the University of Toronto. I am Geoffrey, and I am in 4th year. I used to be a Physics Specialist, but switched to a Math program last term. Last term I took CSC108 online, and did pretty well, so I feel that I should be successful in this course.
My first mandatory post about why geeks should write should be up soon.
My first mandatory post about why geeks should write should be up soon.
Subscribe to:
Posts (Atom)