Friday, July 01, 2011

X-Men First Class as OO example

How do you describe X-Men First Class as object oriented? This is just a question, but no specific answer.

Mutants (X-Men) has superpower, at least one superpower, but some may have more than one superpower. I just pick a few character from X-Men First Class.




Erik (Magneto) play by Michael Fassbender

1. Erik aka Magneto, magnetic force fields to control metal.


Charles Xavier play by James McAvoy

2. Charles Xavier aka Professor X, telepathy power read and control others people (or mutants) mind. 

Emma Frost (by January Jones)

3. Emma Frost, telepathy and diamond state power. 

Raven (Mystique) play by Jennifer Lawrence

4. Raven aka Mystique, shapeshifter power allow her to morphing into other mutants or human form.

Emma Frost does not has nickname but she has more than one superpower. Emma has telepathy power and can turn to diamond state. She can not use both of the mutant power at the same time. For example, Emma can not use telepathy in diamond state, but she stop Charles Xavier from reading her mind in diamond state too.

The superpower of the above mutants are telepathy, diamond state, shapeshifter and magnetic force fields.

Tips:
1. All mutants has (at least 1) superpower.

2. Majority of the mutants has nickname, nickname may related to the superpower

3. There is no specific answers on how you classified or implement the object

My solution:
I am using Java-like syntax, you can use any syntax which is suitable or familiar for you.

// abstract class, because it has an abstract method, userPower() 
abstract class Mutant { 
    String name; 
    String nickname;

    Mutant() { }; 

    Mutant(String name) { 
        this.name= name; 
        } 

    Mutant(String name, String nickname) { 
        this.name= name; 
        this.nickname= nickname; 
        } 

    String getName() {
        return name;
        }

    String getNickname() {
        return nickname; 
        }

    abstract void usePower();
    } 

class Magneto extends Mutant { 
    private static final String name= "Erik Lehnsherr"; // inheritance override 
    private static final String nickname= "Magneto"; 

    Magneto() {
        super(name, nickname);
        }

    void usePower() { 
        System.out.println("move the coin"); 
        } 
    } 

class Telepathy extends Mutant { 
    // no specific name here
    Telepathy(String name) { 
        super(name);
        }

    Telepathy(String name, String nickname) { 
        super(name, nickname);
        }

    void usePower() { 
        System.out.println("read the mind"); 
        } 
    } 

class Shapeshifter extends Mutant { 
    String name= "Raven"; // inheritance override 
    String nickname= "Mystique"; 

    Shapeshifter(String name) { 
        super(name); 
        } 

    Shapeshifter(String name, String nickname) { 
        super(name, nickname); 
        } 

    void usePower() { 
        System.out.println("transform to another human or mutant"); 
        } 
    } 

class FirstClass { 
    public static void main (String [] args) { 
        Magneto erik= new Magneto(); 
        Telepathy charles= new Telepathy("Charles Xavier", "Professor X"); 
        Telepathy emma= new Telepathy("Emma Frost"); 
        Shapeshifter raven= new Shapeshifter("Raven", "Mystique"); 

        
        System.out.println(erik.getName() + " aka " + erik.getNickname()); 
        erik.usePower(); 
        System.out.println(charles.getName()); 
        charles.usePower(); 
        System.out.println(emma.getName()); 
        emma.usePower(); 
        System.out.println(raven.getName() + " aka " + raven.getNickname()); 
        raven.usePower(); 
        }
    } 

$ java FirstClass 
Erik Lehnsherr aka Magneto
move the coin
Charles Xavier
read the mind
Emma Frost
read the mind
Raven aka Mystique
transform to another form


I didn't implement second superpower for Emma Frost, left it for you to do it.

No comments:

Post a Comment