Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java create a class GameCharacter to store information about a character i

ID: 641614 • Letter: U

Question

Using Java create a class GameCharacter to store information about a character in a fighting game. A GameCharacter should have fields for a name and three statistics (health, attack damage, and defense strength). It should have an appropriate constructor and a method called .getStats() that returns a string describing the character. It should also ahve methods .takeDamage() and .attack() that work as follows.

Given a damage amount (such as the attack damage of an attacker) .takeDamage() should first lessen the damage amount by the character's defense stat and tehn deduct the remaining damage points from its health. So for example, if goblin started with health 10 and defense strength 4 and you called goblin.takeDamage(6), then goblin would take 6-4=2 points of damage and end up with health 8.

The .attack() method should take another GameCharacter and use that character's .takeDamage() method with an attack damage equal to the attacking character's attack damage stat. So for example, if storm (attack damage: 20) was going to attack mystique (health:40, defense strength:5), you would call storm.attack(mystique), and mystique would end up with a health stat of 25.

Next, create a subclass Healer of GameCharacter. Healers have an additional stat for their healing amount. You'll have to write a new contructor as well that handles the new stat. Override the .getStats() method so taht it mentions the class (healer) and the healing stat. Healers also ahve a method called .heal() that takes another GameCharacter and heals them by the amount of Healer's healing amount.

Here's a tester class:

public class GameTester {

public static void main(String[] args) {

GameCharacter eowyn = new GameCharacter("Eowyn",40,7,2);

Healer wong = new Healer("Wong Fei-hung",25,5,4,5);

System.out.print(eowyn.getStats());

System.out.print(wong.getStats());

System.out.println("Eowyn attacks Wong Fei-hung!");

eowyn.attack(wong);

System.out.print(eowyn.getStats());

System.out.print(wong.getStats());

System.out.println("A dragon attacks Eowyn!");

eowyn.takeDamage(20);

System.out.print(eowyn.getStats());

Sytem.out.print(wong.getStats());

Explanation / Answer

//GameCharacter.java
public class GameCharacter
{
  
   //variables
   private String name;
   protected int health;  
   protected int attack_damage;
   protected int defense_strength;
  
   //Constructor
   public GameCharacter(String name,int health,int attack_damage,int defense_strength)
   {
       this.name=name;
       this.health=health;
       this.attack_damage=attack_damage;
       this.defense_strength=defense_strength;
   }
  
   //Returns the health of the GameCharacter
   public int getHealth()
   {
       return health;
   }
  
   //Sets the health of the GameCharacter
   public void setHealth(int h)
   {
       health=health+h;
   }
      
   //Returns the statistics of the GameCharacter object
   public String getStats()
   {
       return " Name : "+name+
               " Health : "+health+
               " Attack Damage : "+attack_damage+
               " Defence Strength : "+defense_strength;
   }
  
   //The method takeDamage that accepts the damage
   //and remove defense stregth from damage and
   //remove the remaining from health
   public int takeDamage(int take_damage)
   {
       int remaing_stength=take_damage-defense_strength;
       health=health-remaing_stength;
       return health;
   }  
  
   //attack method that accepts the GameCharacter
   //and remove attacke attak_damage from the defencese
   //and add to the health
   public void attack(GameCharacter gameCharacter)
   {
       health=health+defense_strength-gameCharacter.attack_damage;
   }
      
  
}

--------------------------------------------------------------------


//Healer extends GameCharacter
public class Healer extends GameCharacter
{
//additional healer value
   private int healing;
   //constructor that calls the GameChanger constructor
   public Healer(String name, int health, int attack_damage,
           int defense_strength,int healing)
   {
       super(name, health, attack_damage, defense_strength);
       this.healing=healing;
   }
  
   //Override getStats method
   @Override
   public String getStats()
   {
       return super.getStats()+" Healing : "+healing;
   }
  
   //The heal method that accepts the GameCharacter
   //and add the gameCharacter health and healing
   public void heal(GameCharacter gameCharacter)
   {
       health=gameCharacter.getHealth()+healing;
   }
  
}//end of the class Healer


------------------------------------------------------------------------------------------------------


//GameTester.java
public class GameTester
{
   public static void main(String[] args)
   {
      
      
       GameCharacter eowyn = new GameCharacter("Eowyn",40,7,2);
       Healer wong = new Healer("Wong Fei-hung",25,5,4,5);
      
       System.out.print(eowyn.getStats());
       System.out.print(wong.getStats());
      
       System.out.println(" Eowyn attacks Wong Fei-hung!");
       eowyn.attack(wong);
      
       System.out.print(eowyn.getStats());
       System.out.print(wong.getStats());
       System.out.println(" A dragon attacks Eowyn!");
      
       eowyn.takeDamage(20);
       System.out.print(eowyn.getStats());
       System.out.print(wong.getStats());
   }
}

------------------------------------------------------------------------------------------------------

Sample output:


Name : Eowyn
Health : 40
Attack Damage : 7
Defence Strength : 2
Name : Wong Fei-hung
Health : 25
Attack Damage : 5
Defence Strength : 4
Healing : 5
Eowyn attacks Wong Fei-hung!

Name : Eowyn
Health : 37
Attack Damage : 7
Defence Strength : 2
Name : Wong Fei-hung
Health : 25
Attack Damage : 5
Defence Strength : 4
Healing : 5
A dragon attacks Eowyn!

Name : Eowyn
Health : 19
Attack Damage : 7
Defence Strength : 2
Name : Wong Fei-hung
Health : 25
Attack Damage : 5
Defence Strength : 4
Healing : 5

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote