pcgamer2315132
pcgamer2315132

Reputation: 7

Creating a Pokémon class in Java

I have a Java project that requires me to have two classes called: Pokemon.java and Move.java so I can add new Pokemon and their moves. I’ve already written all of the methods that were required for the project but I’m having issues storing and modifying the moves, specifically the forgetMove method in the Pokemon class.

Here’s the code for Pokemon.java:

    public class Pokemon
{
    // Private constants
    private static final int MAX_HEALTH = 100;
    private static final int MAX_MOVES = 4;
    private String name;
    private int health;
    private Move move;

    // Write your Pokemon class here
    public Pokemon(String theName, int theHealth)
    {
        name = theName;
        if(theHealth <= MAX_HEALTH)
        {
            health = theHealth;
        }
    }

    public String getName()
    {
        return name;
    }

    public int getHealth()
    {
        return health;
    }

    public boolean hasFainted()
    {
        if(health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean canLearnMoreMoves()
    {
        if(Move.getNumOfMoves() < 4)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean learnMove(Move move)
    {
        if(canLearnMoreMoves())
        {
            this.move = move;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void forgetMove(Move other)
    {
        if(Move.equals(other))
        {
            move -= other;
        }
    }

    public String toString()
    {
        return name + " (Health: " + health + " / " + MAX_HEALTH + ")";
    }
}

and here is the code for Move.java:

public class Move
{
    // Copy over your Move class into here
    private static final int MAX_DAMAGE = 25;
    private static String name;
    private static int damage;
    public static int numMoves;

    public Move(String theName, int theDamage)
    {
        name = theName;
        if(theDamage <= MAX_DAMAGE)
        {
            damage = theDamage;
        }
        numMoves++;
    }

    public static String getName()
    {
        return name;
    }

    public static int getDamage()
    {
        return damage;
    }

    public static int getNumOfMoves()
    {
        return numMoves;
    }

    public String toString()
    {
        return name + " (" + damage + " damage)";
    }    
    // Add an equals method so we can compare Moves against each other

    public static boolean equals(Move other)
    {
        if(name.equals(other.getName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Here is the code for PokemonTester.java:

public class PokemonTester extends ConsoleProgram
{
    public void run()
    {
        Pokemon p1 = new Pokemon("Charrizard", 100);
        Move m1 = new Move("Flamethrower", 90);
        System.out.println(p1);
        System.out.println(m1);
    }
}

Upvotes: 1

Views: 2877

Answers (1)

CampbellMG
CampbellMG

Reputation: 2220

This seems like it might be homework so I won't give you a full implementation.

If you are simply filling out the methods required for the Pokemon and Move class, I would start by reconsidering the way you are storing moves.

The getNumOfMoves provides a hint that your Pokemon class should store more than one move, a common way to do this is with arrays or lists.

If you have stored your moves in a list, the forgetMove function may look like this:

public void forgetMove(Move other){
  moves.remove(other);
}

Upvotes: 2

Related Questions