Saturday, February 28, 2015

The summary of Object-Oriented Programming #Week6 (to be graded)

In the csc148 class, we learned a new method of programming, which is Object-Oriented Programming (OOP). At the beginning of the class, I was struggling in the Object-Oriented Programming because it is a new concept and I had totally no idea what it is. Fortunately, a couple of weeks later, I made sense about the Object-Oriented Programming.

BASIC CONCEPTS ABOUT OOP

There are some concepts about 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 superclasses, and explains how to derive one class from another using the simple syntax provided by the python programming language.
What Is an Interface?
An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.


A SIMPLE EXAMPLE ABOUT OOP:
For example, we are trying to make a small RPG game. So how do we start it?
Well, there are some monsters and heroes in this small game. They can attack each other with different skills (try to compare this game with Pokémon).


First of all, we need to create a class which called "Heroes" and each hero has the features of blood, age, and name. In addition, do not forget create a class of monsters.

Here's the code:




Well, now we have finished creating classes, it’s time to create some objects.

hero1 = Heroes(100, 20, 'Lee', 'Fire')

The hero called Lee, he has 100 blood, and he is 20 years old, and he can use fire


monster1 = Monsters(50, ‘Snoopy’, ‘Bite’)

The monster called Snoopy, he has 50 blood, and he can bite someone.

Image if you want to create thousands of monsters and heroes in the game and if you do not know how to use object-oriented-programming, that will be very tough for creating the game. However, if you know how to use OOP well, you can make a flexible combination for each object and that will save your time.


CONCLUSION:


Object-oriented-programming is significant and useful for programmers especially for creating the Role Play Games and there are several reasons, which are easy to maintain, easy to extend, efficient. Firstly, the idea of using OOP has a higher readability because of the existence of the inheritance. Even though the requirements need to be changed, the maintaining can be only working with local models. Therefore, it is convenient and low-cost to maintain a program if we use the concept of OOP. Secondly, because of the features of inheritance, encapsulation, and polymorphism, we can design naturally out a well-structured computer system with high cohesion and low coupling. Finally, when we write code, we can create abstractions of objects from the real world according from our demands. Using this concept for addressing programming problems is close to our daily life and the natural thinking, therefore, there is no doubt that it can enhance the quality of code and the efficiency.





Wednesday, February 25, 2015

THE AMAZING RECURSION #Week 5

RECURSION IN DAILY LIFE

It was my first time learning how to use recursion in programming in week #4.  But it was not the first time that I come into contact with recursion. I remembered when I was a child, sometimes I thought that the god (god A) knows my thoughts, and an another god (god B) knows that the god (god A) knows my thoughts, etc.
We can see there are many recursion examples in our daily life. For example:


Therefore, recursion is not only interesting and also is not mysterious.

WHY PROGRAMMERS NEED TO KNOW ABOUT RECURSION

Well, it's said that an ordinary programmer like using iteration, however, a talented programmer like using recursion. Even it is just a joke, but recursion is significant for a programmer. Knowing how to use recursion can let your code run more efficiently, and since most of the programs are a teamwork, therefore, reducing the amount of codes is also important for a good programmer.
Here is the comparison between iteration and recursion:
==========================================
Iteration:
def factorial(number):
product = 1
for i in range(number):
product = product * ( i + 1 )
return product

Recursion:
def factorial(number):
if number <= 1:
return 1
else:
return number * factorial(number - 1)

===========================================
Basically, the code of recursion looks simpler than the code of iteration. Image if you are working with a group and your teammates have to read your code. It's important that keep your code as simple as possible.

WHAT IS RECURSION?

Here is the definition of recursion from WIKIPEDIA
In mathematics and computer science, a class of objects or methods exhibit recursive behavior when they can be defined by two properties:
  1. A simple base case (or cases)
  1. A set of rules that reduce all other cases toward the base case
For example, the following is a recursive definition of a person's ancestors:
  • The ancestors of one's ancestors are also one's ancestors (recursion step).
The Fibonacci sequence is a classic example of recursion:
\text{Fib}(0)=0\text{ as base case 1,}
\text{Fib}(1)=1\text{ as base case 2,}
\text{For all integers }n>1,~\text{ Fib}(n):=\text{Fib}(n-1) + \text{Fib}(n-2).
Many mathematical axioms are based upon recursive rules. For example, the formal definition of the natural numbers by the Peano axioms can be described as: 0 is a natural number, and each natural number has a successor, which is also a natural number. By this base case and recursive rule, one can generate the set of all natural numbers.
Recursively defined mathematical objects include functions, sets, and especially fractals.
There are various more tongue-in-cheek "definitions" of recursion; see recursive humor. 

THE DIFFERENCE BETWEEN RECURSION AND ITERATION

An iterative algorithm is based on a repetition of a set of instructions (block) using a loop construct of the programming language. However, a recursive algorithm repeats the code by calling itself. The main advantage of recursive algorithms is their compactness and understandability. Their disadvantage is a need of additional system resources to maintain the call stack.

CONCLUSION:

Personally, although using recursion can reduce the amount of codes, however, something it might cause a serious bug if we use recursion casually because it would cause recursion depth goes infinity and it will take up the whole memory.