Wednesday, February 11, 2015

Week 6: Object Oriented Programming


There are many reasons that Object Oriented Programming important to programmers. It encourages abstract thinking and allows the programmer to isolate only the minimum information needed to solve a problem. Be as lazy as possible! Programming has many applications to real life situations because objects in the real world can be modelled as “objects” in code. When the problem size increases, it is sometimes useful to define objects with the information necessary allows us to manipulate them to get the information needed instead of inputting the same data constantly.

Anything can be made into an object by creating a class in Python. You can make a Class Ball, Employee, Company, Words, or even Sounds. Notice that not all of these are what we would define as an object in real life. The beauty of programming is the ability to be abstract, and create something out of nothing for lack of a better term.

An example of an object in Python can be a class called “Flower” which you can initialize with the type of flower and details about each flower like their name, average height, color, texture, etc. This will not create an actual flower, but the data that is entered can keep track of the information about Flower’s that you are interested in. It is essential to realize what data is important to keep track of and what isn’t. After initializing an instance of a Flower and calling a method, the first parameter (usually called self) lets the Flower instance gather all of the information it knows about itself so the method can use anything it needs. Once you create an instance of a specific flower, any data entered in parameters is stored in that instance for when you need it again.

Sunday, February 8, 2015

Week 5: Inheritance and Recursion in Python


Inheritance
                Inheritance is a powerful tool that allows a programmer to organize their code. When defining a new class, every method from parent classes are inherited. By default, every class in Python is a subclass of type object and inherits its methods. The Python constructor __init__ initializes the new object and is called every time an instance of a class is created. If a method exists in both the parent class and the child class, the most specific method is the one that will be called. For example, say that parent class called Ball has a child class called BaseBall that also both have methods called bounce. Which bounce will be called if you call method bounce on a BaseBall? Logically, the method bounce in class BaseBall would be called. If you create a Ball and call bounce on it, since Ball is not a type of BaseBall, its most specific method is in class Ball and that is the one that will be called.

Recursion
                We have just started to learn about recursion. Recursion can simplify certain problems that would be more difficult to do without it. The basic idea of recursion is that it breaks down a hard problem into smaller, simpler steps that are easier to solve. The simplest step is called the base case.
Tracing recursion can be quite difficult if inductive reasoning is not used. When a problem requires the function to call itself many times, it can become difficult and time consuming follow all of the steps the computer is taking. Using inductive reasoning, we start with the simplest case (base case) and trace it fully to prove that it will always be true. Every time we increase the difficulty a little, we can skip the steps that we previously proved so we can arrive at the answer faster. This is a very powerful concept because it allows a programmer to think about their code at a higher level, and not have to look at every detail.

Sunday, February 1, 2015

Week 4: Pros and Cons of the First Few Weeks


My opinion on the first few weeks of the course. The most difficult things and the easiest things.

Classes

                The most difficult part of designing a class structure was figuring out which variables should be initialized in the parent class. I need to spend more time learning the concepts behind classes because they are a powerful tool in python. I understand that classes are great because they allow you to reuse code, but I have trouble finding what parts of a problem are reusable. In the first assignment, keeping the parent class general enough so that it can work for multiple games, but at the same time getting all of the information needed to make it work for a specific game was quite a challenge. I will have to go back and change how the assignment was written the first time because I don't think the classes were designed as efficiently as they could have been.


Labs

                I found that I was very lost during labs 1 and 2. I sometimes felt like I was in the lab for a different class. It felt like there was a huge gap between what we were being taught in class, and what we were expected to know for the labs. So much so that I actually asked my teacher assistant what class the lab was for during the first lab. When my teacher assistant was explaining how to approach problems, they were writing code and using ideas we didn’t learn yet. At first I thought it was just me, but after speaking with multiple people in my lab section, many people felt like the labs were confusing. The third lab was definitely an improvement from the first two. I found the recursion lab to be very similar to what we learned in class, and I could actually follow along with what the teacher assistant was talking about.


Python Style

                I never had difficulties following the style guidelines in CSC108. I think this is because I want all of my code to have high readability and I like having my code organized. I abuse commenting as much as I can because it helps explain to group members what I did and why I did it.


Design Recipe

Examples -> Type Contract - > Header -> Description -> Body

The design recipe has been ingrained in my head, and I’m sure many students who took CSC108 would say the same thing because we had a lot of practice writing functions using it. It is very methodical and makes writing code A LOT easier. Examples are the first thing written because they tell us what the program will take in and spit back out. This is great because we can see where the program is heading, and part of the problem is figuring out what to make the function return. The type contract easily follows examples because all of the information needed is contained in the example. I usually write the header and description together because they both use the same information. The header lets me put words behind what each parameter in my example means, and what the function will be called. I like naming my functions using a word that describes what they do. I name each parameter based on its type contract and how it is used. The description explains to a user what each parameter is and what the function is going to do with them. Finally, the most important thing to implement is the body. Since everything else is done, it is easier to see what is available initially, and how to build an algorithm to get the desired answer.