Home > Mobile >  How do I access and update a variable form another java class?
How do I access and update a variable form another java class?

Time:10-09

I am trying to access the variable 'hp' which I initialized in the 'Monster' class but nothing is working. I tried using setters but it didn't work.

Monster class:

public class Monster{
    
    private String name, type;
    private int hp, speed, attack, defense;
    private Move move1, move2, move3, move4;

    public Monster(String name, String type, int hp, int speed, int attack, int defense, Move move1, Move move2, Move move3, Move move4) {

        this.name = name;
        this.type = type;
        this.hp = hp;
        this.speed = speed;
        this.attack = attack;
        this.defense = defense;
        this.move1 = move1;
        this.move2 = move2;
        this.move3 = move3;
        this.move4 = move4;
    }

The following code is part of another class where I am trying to access hp and update its value:

    damage = getMonster().getAttack()   movePower - pl.getMonster().getDefense();
    int currentHP = getMonster().getHP();
    int updatedHP = currentHP - damage;
    hp.setHP(updatedHP);

This is how I'm trying to access and modify it. Please help.

CodePudding user response:

I figured it out. All I had to do was set the monster variable to public in the Player class and then I used the dot operator to access and update the hp variable. Thanks for all the help!

CodePudding user response:

You will not be able to update that instance variable hp outside of the Monster class. Your idea of using .setHP() isn't working, because you are not using that method on a Monster object. If your Monster in this example is called p1, you should do something like this:

int updatedHP = p1.getHP() - damage;
p1.setHP(updatedHP);

assuming you want to keep the updatedHP variable, and have a working getHP() method within the Monster class, that is.

CodePudding user response:

Your code is unclear as there a bunch of variables without the Type included (hp and p1) but I believe what you are trying to do is this -

    damage = getMonster().getAttack()   movePower - pl.getMonster().getDefense();
    int currentHP = getMonster().getHP();
    int updatedHP = currentHP - damage;
    getMonster().setHP(updatedHP);

CodePudding user response:

you'll need to define setter in Monster class and use it after instantiating Monster object.(Removing move for simplicity)

public class Monster {

    private String name, type;
    private int hp, speed, attack, defense;

    public Monster(String name, String type, int hp, int speed, int attack, int defense) {
                                                                                         
        this.name = name;
        this.type = type;
        this.hp = hp;
        this.speed = speed;
        this.attack = attack;
        this.defense = defense;
    }

    public int getHp() {
        return this.hp;
    }

    public void setHp(int num) {
        this.hp = num;
    }
}
import Monster.java;

public class Hello {
    public static void main(String[] args) {
        Monster m = new Monster("name1", "type1", 20, 1, 2, 3);
        m.setHp(200);
        System.out.println(m.getHp());
    }
}

CodePudding user response:

Let's assume we have a Monster and we calculate some damage:

Monster monster = getMonster();
int damage = calculateDamage();

We can calculate the HP then assign it back to the Monster:

int remainingHp = monster.getHp() - damage;
monster.setHp(remainingHp);

This can be simplified a bit to:

monster.setHp(monster.getHp() - damage);

Note that there is no bounds checking. What happens if damage is greater than the HP on the monster? Is it meaningful to go negative or should it drop to 0? Consider adding a method to Monster:

public class Monster {
  ... stuff ...
  /**
   * @return true if the Monster is still alive, false if it has died
   */
  public boolean acceptDamage(int damage) {
    hp = hp - damage;
    if (hp > 0) {
      return true;
    }
    // doh - we died!  set HP to 0 and return false
    hp = 0; 
    return false;
  }



  
  •  Tags:  
  • java
  • Related