Skip to content
Advertisement

Inheritance, setters and getters (Game Class Output)

How am I supposed to decrease its armor first and next the health? I want not to print the negative of armor, and I want to print if the armor gets 0, the health should be the next to decrease.

For Instance in Testing Class, I use wizard and cast a spell to fireball 3 times to knight. Since knight should decrease the armor first, and health after the armor gets to 0.

Output:

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now 6 armor left, and 80 health left.

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 80 health left.

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 56 health left.

As you can see in the Second Print, the armor gets -18, I want that to print 0 and 62 health left. since wizard deal 24 damage

enter image description here

This is the Parent class:

    public class Human {
    
   String name;
   int strength;
   int stealth;
   int intelligence;
   int health;

   public Human() {
       name = "Human";
       strength=3;
       stealth=3;
       intelligence=3;
       health=100;
   }
   
  
   public int getStrength() 
   {
       return strength;
   }

   public int getStealth() 
   {
       return stealth;
   }

   public int getIntelligence() 
   {
       return intelligence;
   }

   public int getHealth() 
   {
       return health;
   }

  
   public String getName()
   {
       return name;
   }


   public void setName(String name) 
   {
    this.name = name;
   }


   public void setStrength(int strength) 
   {
    this.strength = strength;
   }


   public void setStealth(int stealth) 
   {
    this.stealth = stealth;
   }


   public void setIntelligence(int intelligence)
   {
    this.intelligence = intelligence;
   }


   public void setHealth(int health) 
   {
    this.health = health;
   }

   public void attack(Human human)
   {
       int damagetaken = this.getStrength();
        human.health = human.health - this.getStrength();
        System.out.println(this.getName()+ " attack " + human.getName() + " and will deal " +damagetaken +" damage.");
        System.out.println(human.getName()+ " has now "+human.getHealth()+" health left");
        System.out.println();
   }


}

Child Class Knight:

public class Knight extends Human
{
   int armor=30;
   
   public Knight() {
       this.name="Knight";
       this.health=80;  
   }
   public void Reinforce(){
       this.armor=this.armor+5;
       System.out.println(this.getName()+ " use a 'Reinforce' and will increase its armor to 5 ");
       System.out.println(this.getName()+ " has now "+ this.armor +" armor left.");
       System.out.println();

   }
   public void HolyStrike(Human human)
   {   
       int damagetaken = (int)(human.health-10-(.20*this.armor));
       human.health= human.getHealth() - damagetaken;
       System.out.println(this.getName()+ " use a 'Holy Strike' and will deal "+damagetaken+ " damage to "+ human.getName());
       System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
       System.out.println();
   }
}

Child Class Wizard:

public class Wizard extends Human{

   public Wizard() 
   {
       this.name="Wizard";
       this.health=50;
       this.intelligence=8;  
   }
   public void heal(Human human)
   {
       int healtake = this.getIntelligence();
       human.health=human.health+this.intelligence;
       System.out.println(this.getName()+" use a 'Heal' to " + human.getName() + " he/she will receive " + healtake + " additional health to his/her HP");
       System.out.println(human.getName()+" has now "+ human.getHealth()+" health left.");
       System.out.println();
   }
   public void fireball(Human human)
   {
       int damagetaken = this.intelligence*3;
       human.health= human.health-(this.intelligence*3);
       System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
       System.out.println(human.getName()+" has now " +human.getHealth()+" health left.");
       System.out.println();
   }
   
   public void fireball(Knight human)
   {
       
       if (human.armor > 1)
       {
           int damagetaken = this.intelligence*3;
           human.armor= human.armor-(this.intelligence*3);
           System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
           System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
           System.out.println();
       }
       
       else
       {
           int damagetaken = this.intelligence*3;
           human.health= human.health-(this.intelligence*3);
           System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ human.getName());
           System.out.println(human.getName()+" has now " +human.armor+" armor left, and "+human.getHealth()+" health left.");
           System.out.println();
       }
   }
   
}

Testing Class:

public class HumanTest {
   public static void main(String Args[]){
       
       Human h1 = new Human();
       Wizard w1=  new Wizard();
       Ninja n1 = new Ninja();
       Samurai s1 = new Samurai();
       Knight k1 = new Knight();
       
   
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
   }
}

Can you help me to solve this?

you can ask me about the source code.

Advertisement

Answer

You are checking if armor is greater than 1 (human.armor > 1). You must reduce as much armor as possible and then the rest from health.

public void fireball(Knight knight) { //I've renamed the variable as knight
    int damageToTake = this.intelligence * 3;
    if (knight.armor >= damageToTake) {
        knight.armor -= damageToTake;
    } else {
        int remaining = damageToTake - knight.armor;
        knight.armor = 0;
        // reduce remaining from health
        knight.health -= remaining;
    }
    System.out.println(this.getName()+ " use a 'Fireball' and will deal "+damagetaken+ " damage to "+ knight.getName());
    System.out.println(knight.getName()+" has now " +knight.armor+" armor left, and "+knight.getHealth()+" health left.");
    System.out.println();

}

I guess you’ll also have to handle when there isn’t enough health as well. I’ll leave that to you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement