Sunday, March 6, 2011

REACH Session #2

During our second session with Mr.Marques we reviewed Arrays, ArrayLists, Inheritance, Polymorphism, and Recursion.
Everything else is something that you should know from class except for recursion and some polymorphism.

For most examples, the teacher uses Mario Kart to explain object-oriented programming.
You can think of the world as the racetrack and the players as the objects.




When you select your player in the game that player is created and you then play the game.
This is analogous to constructing an object through a client method. Once you created it, you can use it (you can play with it).







All of the players have different cars. Mario has a red car, Peach has a chariot hybrid, Donkey Kong has a car made out of trees etc. They are all still vehicles (an abstract class, there aren't really any vehicles! they must be a car or truck etc)and share the same methods. When you push the accelerate button on your console as Peach, you move forward. The same happens when you're Bowser but you move a lot slower. The vehicle interface may have a go() method that is overridden by classes that implement it since they all act a bit different when they move.
This is called polymorphism. Classes have the same method but use it differently.


This can look something like this:

public interface Vehicle {
void go();
}

public class MariosCar implements Vehicle {
public void go() {
/* goes at 50 mph*/
}
}

public class BowsersCar implements Vehicle {
public void go() {
/* goes at 10 mph*/
}
}

We also learned recursion which is, according to Mr.Marques, the hardest topic in CS.
He gave us an example of writing factorials like this:

public int factorial(int n) {
if (n==1) { return 1; } //this is the base case
else {
return n* factorial(n-1);
}
}

It's a complicated topic but easier if you trace it as you go.

He also gave us practice problems to do at home and sent extra copies for those who missed the session.
The next REACH session is on 4/30, by then you should have reviewed everything and come to him with questions.
Remember to use the barron's review book! he strongly recommends it!

No comments:

Post a Comment