0

I created a test project, but I encountered something I can't figure out.

I am trying to call Monster in FightManager. I want Monster's variables (name, health, damage and defense) to be equal to whatever monster is randomized (WolfMonster or GoblinMonster)

Previously I had only one monster, and I managed to do it but now when there are 2 monsters how can I pass the variables a different value if a different monster is selected?

public class Units {
    int health;
    int damage;
    int defense;
    String name;

    public boolean isAlive(){
        if(health >= 1){
            return true;
        }else{
            return false;
        }
    }
}

public class Monster extends Units{
    public Monster(String name,int health,int damage,int defense){
        this.name = name;
        this.health = health;
        this.damage = damage;
        this.defense = defense;
    }
}

public class GoblinMonster extends Monster {
    public GoblinMonster(String name, int health, int damage, int defense) {
        super("Goblin",50,5,6);
        this.name = name;
        this.health = health;
        this.damage = damage;
        this.defense = defense;
    }
}

public class WolfMonster extends Monster {
    public WolfMonster(String name, int health, int damage, int defense) {
        super("Wolf",50,5,6);
        this.name = name;
        this.health = health;
        this.damage = damage;
        this.defense = defense;
    }
}

public class FightManager {

    GameManager manage = new GameManager();
    Player player = new Player("Player",100,10,5);
    GoblinMonster gobli = new GoblinMonster("Goblin", 40, 7, 4);
    WolfMonster wolf = new WolfMonster("Wolf",50,9,6);

    boolean myTurn = true;
    ....

I want to know how to assign a value of monster depending on which monster is generated.

4
  • 2
    It is not clear what you are asking for. Could you specify please what you mean by value? Commented May 8, 2017 at 16:08
  • 4
    I don't see any need for two subclasses here. They have exactly the same fields and the same behavior (no overridden method). Also, your constructors make no sense. You're initializing every field twice: once in the base constructor, and once again in the subclass constructor. And you pass a hardcoded name from the subclass constructor, but overwrite it immediately after with the passed name. Commented May 8, 2017 at 16:08
  • Not sure what the problem is. It seems that you are confused about polymorphic behaviors. Commented May 8, 2017 at 16:09
  • Your current code does dot require inheritance at all. All your "special" units differ only in Properties. But inheritances should be used to provide different behavior which means: methods with same name but different content. Commented May 8, 2017 at 16:20

3 Answers 3

1

I don't see any need of multiple subclasses and parent Units class here. You can simply create different monster object with names WolfMonster, GoblinMonster.

public class Monster {
    int health;
    int damage;
    int defense;
    String name;

    Monster(String name, int health, int damage, int defense)
    {
        this.name = name;
        this.health = health;
        this.damage = damage;
        this.defense = defense;
    }
    public boolean isAlive()
    {
        if(health >= 1){
            return true;
        }else{
            return false;
        }
    }    
}

public class FightManager {

    GameManager manage = new GameManager();
    Player player = new Player("Player",100,10,5);

    //changes

    Monster gobli = new Monster("Goblin", 40, 7, 4);
    Monster wolf = new Monster("Wolf",50,9,6);

    boolean myTurn = true;

    // To-Do
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah i thought of that,but in future i plan adding more and more monsters and i think it will be overkill using this method
You can always use more Monster by calling new Monster("SomeName", 40, 7, 4);
0

Maybe what you want to do is set "name" as a constant in each constructor.

So, for example WolfMonster would be:

public class WolfMonster extends Monster {
    public static String TYPE = "Wolf";
    public WolfMonster(int health, int damage, int defense) {
        super(WolfMonster.TYPE,health,damage,defense);
    }
}

Note that you don't need to reasign the member fields as the will be assigned whe super() is called.

Comments

0

to do so you have to use polymorphism , by declaring the Unit class as an interface .The method isAlive() as abstract as well as the attributs .In the other hand the class Monster should implement the Unit interface,and the rest of your monster classes will extend the classe Monster. at last you will overide the method isAlive() at each subclass ,then Voila !

1 Comment

You saved me bro! Thanks really an answer i was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.