Home > Software design >  Is it possible with OOP in Java to automatically call a method when an object's variable turns
Is it possible with OOP in Java to automatically call a method when an object's variable turns

Time:11-20

I'm sorry in advanced if this was a silly question, just understand the fact that I'm a beginner, so please don't be too harsh with me if I'm asking something like this.

I am practicing to work my way with understanding how object's attributes works with each other (OOP) I tried to mimick a game scenario as my practice idea, I initialised HPs and attack values in each object creation, I have created an attack method which simply just decrements the HP of the attacked entity, what I want to accomplish is that every time the HP of the attacked entity turns zero or less, it will automatically reassign its value to 0, and prints a message "An enemy was terminated"

This was the class `

public class Character {
    
    int HP;
    int ATK;
    
}

class Player extends Character {
    
    // default values
    public Player() {
        HP = 100;
        ATK = 40;
    }
    
    // attack method
    public void attack(Enemy a) {
        a.HP -= this.ATK;
    }
    
    
}

class Enemy extends Character {
    
    public Enemy() {
        HP = 50;
        ATK = 10;
    }
    
    public void attack(Player a) {
        a.HP -= this.ATK;
    }
}

`

Using it in main method:

`

public class Main {
    
    public static void main(String[] args) {
        
        // instantiation
        Player p = new Player();
        Enemy e1 = new Enemy();
        
                
        System.out.println("Player's HP: "   p.HP); // 100
        System.out.println("Enemy's HP: "   e1.HP   "\n"); // 50
    
        
        System.out.println("*Enemy attacking Player*");
        e1.attack(p);
        System.out.println("Player's HP: "   p.HP); // 90
        System.out.println("Enemy's HP: "   e1.HP   "\n"); // 50

        System.out.println("*Player attacking Enemy*");
        p.attack(e1);
        System.out.println("Player's HP: "   p.HP); // 90
        System.out.println("Enemy's HP: "   e1.HP   "\n"); // 10

        p.attack(e1); 
        /* when I did it again, it will turn enemy's HP to -30 */
    }
}

`

And as I've said in the previous sentence, what I want to accomplish is instead of letting that value to continue decrementing to the negative numbers, I want its value to set as zero and print a message about the termination of the enemy.

I have no idea if this was a silly way of practicing (I apologize in advanced)

I have tried doing this but it feels wrong. (and it didn't worked) Edit: The value wasn't affected by the if statement, the same thing still occurs within the HP of the enemy even if its value is less than or equal to 0. `

class Player extends Character {
    
    // default values
    public Player() {
        HP = 100;
        ATK = 40;
    }
    
    // attack method
        public void attack(Enemy a) {
                if (!(this.HP <= 0)) {
                a.HP -= this.ATK;
        } else {
            a.HP = 0;
            System.out.println("An enemy was terminated.");
        }
    }
    
}

`

I also tried doing that with constructor but I felt that was wrong as well because constructors works for initializing objects and not for waiting for a specific condition.

I'm very sorry if this question is silly, I swear I have no idea if you would laugh at this thinking "why would you ever going to program a thing like that?", I'm just a beginner, so please bear with me.

CodePudding user response:

class Player extends Character {
    
    // default values
    public Player() {
        HP = 100;
        ATK = 40;
    }
    
    // attack method
    public void attack(Enemy a) {
        a.HP -= this.ATK;
        if(a.HP<=0){
            
            System.out.println("Enemy is Terminated");
            a.HP=0;
        }
    }
    
    
}

CodePudding user response:

class Player extends Character {
    
    // default values
    public Player() {
        HP = 100;
        ATK = 40;
    }
    
    // attack method
        public void attack(Enemy a) {
                if (!(this.HP <= 0)) {
                a.HP -= this.ATK;
        } else {
            a.HP = 0;
            System.out.println("An enemy was terminated.");
        }
    }
    
}`a.HP=12 , b.HP=12 , p.HP=32 System.out.println("");`

CodePudding user response:

class Player extends Application{
    
    public Player() {
        HP = 100;
        ATK = 40;
    }

    public void attack(Enemy a) {
        if (!(this.HP <= 0)) {
            a.HP -= this.ATK;
        } else {
            a.HP = 0;
            System.out.println("An enemy was terminated.");
        }
    }
}
  • Related