Stonny
Stonny

Reputation: 1

Java Calling Constructor

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.

Upvotes: 0

Views: 93

Answers (3)

yssndrm
yssndrm

Reputation: 1

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 !

Upvotes: 0

Abhi Andhariya
Abhi Andhariya

Reputation: 560

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
}

Upvotes: 1

LuisGP
LuisGP

Reputation: 431

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.

Upvotes: 0

Related Questions