Home > Net >  How to double a value of private int type field?
How to double a value of private int type field?

Time:11-10

This is the code of method takeDamage in the class Enemy which is a superclass of class Vampire.

public void takeDamage(int damage) {
    int remainingHitPoints = hitPoints - damage;

    if (remainingHitPoints > 0) {
        setHitPoints(remainingHitPoints);
        System.out.println(name   " took "   damage   " damage and have "   remainingHitPoints   " HitPoint left"   "\n------------------------------------" );
    } else {
        this.lives = this.lives - 1;
        System.out.println(name   " took "   damage   " damage"   "\n------------------------------------"   "\n"   name  " lost a life"   "\n------------------------------------");

        if (lives > 0) {
            System.out.println(name   " revived"   "\n------------------------------------");
            setHitPoints(remainingHitPoints   damage);
        } else {
            System.out.println(name   " is dead now RIP"   "\n------------------------------------");
            setHitPoints(0);
        }
    }
}

In the class Vampire I override the method takeDamge:

public void takeDamage(int damage) {
    int inflictedDamage = damage/2;
    int doubleHitPoints = getHitPoints()*2;
    int remainingHitPoints = getHitPoints() - inflictedDamage;
    int remainingLives = getLives() - 1;

    if (remainingHitPoints > 0) {
        setHitPoints(remainingHitPoints);
        System.out.println(getName()   " took "   inflictedDamage   " damage(Vampire takes inflicted damage) and has "   remainingHitPoints   " HitPoint left"   "\n------------------------------------" );
    } else {
        setLives(remainingLives);
        System.out.println(getName()   " took "   inflictedDamage   " damage(Vampire takes inflicted damage)"   "\n------------------------------------"   "\n"   getName()  " lost a life"   "\n------------------------------------");

        if (remainingLives > 0) {
            System.out.println(getName()   " revived and even got stronger!"   "\n------------------------------------");
            setHitPoints(doubleHitPoints);
        } else {
            System.out.println(getName()   " is dead now RIP"   "\n------------------------------------");
            setHitPoints(0);
        }
    }
}

What I intend to do is double the object's hitPoints which is originally set by the constructor.

When a Vampire object takes enough damage to die (in other words, when it loses its life), this method successfully doubles the object's hitPoints.

The problem happens (if I diagnosed right) when the Vampire object takes not enough damage to die.

setHitPoints(remainingHitpoints); gets executed and when the Vampire object takes enough damage to die, it doubles the object's remainingHitPoints not an original hitPoints.

I would like to fix this issue.

The hitPoints field has been declared as private inside the Enemy class so if I understood well the only way to modify the value of this field is by using getHitPoint method (getter) inside the Enemy class.

CodePudding user response:

You need some place where the original hit points are stored.

If your Enemy class doesn't provide this information and you only need it in the Vampire class you could change the Vampire class along these lines:

public class Vampire extends Enemy {

    private int originalHitPoints;

    public Vampire() {
        // ...
        originalHitPoints = getHitPoints();
    }

    public void takeDamage(int damage) {
        // ...

        if (remainingLives > 0) {
            System.out.println(getName()   " revived and even got stronger!"   "\n------------------------------------");
            setHitPoints(originalHitPoints * 2);
        }

        // ...
    }
}

If you want to double the hit points every time the Vampire dies you could change the code of takeDamage() like this:

    public void takeDamage(int damage) {
        // ...

        if (remainingLives > 0) {
            System.out.println(getName()   " revived and even got stronger!"   "\n------------------------------------");
            originalHitPointes *= 2; // double the hit points
            setHitPoints(originalHitPoints);
        }

        // ...
    }
  • Related