Pallet Town: OO with Pokemon and Java

Oct 27, 2013 | Pallet Town, Programming

Welcome to Pallet Town! Pallet town is going to be an ongoing bi-weekly look at various development technologies and techniques from the perspective of someone new to that technology. For those who might know or (God forbid) are too young to get the reference, Pallet Town is the first town in the original Pokemon games for the Nintendo Gameboy.

Object oriented programming is one of the most common and widely used programming paradigms on the market today. In fact, if (like me), you code for a living, then it is likely that you have to deal with OO concepts on a daily basis; in fact, they have probably become something of a second nature to you. Still, maybe you are just dipping your feet into the wide world of Pokemon… err I meant Java… err I mean programming. Well, this post and its follow up posts are for you.

Before we begin, take a look at this fast code snippet:


public Class Pokemon {     

    public int id;    
    public String name;
    public int hp;
    public int ap;
}

If that seems foreign to you, don’t worry, we will be going over it right now!

OK so what we have there is a simple but boring class that represents Pokemon. Bascailly, we are defining the basic attributes (or properties) of a Pokemon. Taking it from the top, all Pokemon need to have an id so we can store them in our Pokedex (which is basically a fancy database), they all have names, they all all have hp or hit points, and they all have attack points. Of coure, we are likely over simplifying what Pokemon are but we will look at that issue in future posts.

At this point we have a Pokemon class, but no actually Pokemon objects. We need to create an instance of our Pokemon class. To do this we are going to use Java’s ‘new’ operator like so:


Pokemon pikachu = new Pokemon();

Our resulting “pikachu” is a little lame… he has 0 hp, 0 ap, and his name and id are null. Let’s try that again but take it a step futher:


Pokemon pikachu = new Pokemon();

pikachu.name = "Pikachu";
pikachu.ap = 10;
pikachu.hp = 100;

That’s it you have a very basic Pikachu! Sure it can’t do much right now and is a little boring and has some data encapsulation problems, but we will address some of those issues in the next post.

Hope this post helps you on your journey to become a Pokemon… err coding master! Please feel free to send any feedback to me on Twitter or Google+. Also, be sure to check out my company Fingertip Tech, INC on Twitter.

 

More from Mike:

About Me

Hi! I’m Mike! I’m a software engineer who codes at The Mad Botter INC. You might know me from Coder Radio or The Mike Dominick Show.  Drop me a line if you’re interested in having some custom mobile or web development done.

Follow Me

© 2024 Copyright Michael Dominick | All rights reserved