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

Create a class for a Fighter in a combat game. The Fighter class has the followi

ID: 3606125 • Letter: C

Question

Create a class for a Fighter in a combat game. The Fighter class has the following instance variables:

-name

-health points -- a double representing health. 0 or below is dead.

-strength points -- a double representing the strength of the Fighter

The Fighter class also has the following methods

-public void setName(String n)
A mutator method for the name. Note: Do not input the name in this method. That belongs in the main method.

-public void setHealth(double h)
A mutator method for the health (Do not input the health here.)

-public void setStrength(double s)
A mutator method for the strength (Do not input the strength here.)

-public String getName()
An accessor method for the name

-public double getHealth()
An accessor method for the health

-public double getStrength()
An accessor method for the strength

-public void attack(Fighter foe)
Calculates a change in the health of the foe parameter by multiplying the strength of this Fighter by Math.random(), then changing the health of the foe ;by the change.

-public boolean isDead()
Returns true if the Fighter's health is less than or equal to 0, else false

-public void printChange(double c,Fighter nme)
Prints a message displaying the name of the nme Fighter, the amount of change in health c, and the resulting level of health for nme. Called from attack.

Also write a driver program, instantiating two Fighters, assigning them names, strengths and health values, for example, 10 for health and 5 for strength for both, then loop until one of them is dead, taking it in turns for one to attack the other. Do not allow a dead Fighter to attack a living one, or vice versa. This main method can be in the same file or in a different file.

Explanation / Answer

import java.util.*;

import java.lang.*;

class Fighter

{

private String name;

private double health;

private double strength;

public void setName(String n)

{

name = n;

}

public void setHealth(double h)

{

health = h;

}

public void setStrength(double s)

{

strength = s;

}

public String getName()

{

return name;

}

public double getHealth()

{

return health;

}

public double getStrength()

{

return strength;

}

public void attack(Fighter foe)

{

foe.health = foe.health - this.strength * Math.random();

printChange(foe.health,foe);

}

public boolean isDead()

{

if(health <= 0)

return true;

else

return false;

}

public void printChange(double c,Fighter nme)

{

System.out.println(" "+nme.name + " change in health : "+ c+ " health : "+ nme.health );

}

}

class Test

{

public static void main (String[] args)

{

Fighter f1 = new Fighter();

f1.setName("Goldberg");

f1.setHealth(10);

f1.setStrength(5);

Fighter f2 = new Fighter();

f2.setName("Roman Reigns");

f2.setHealth(10);

f2.setStrength(5);

do

{

f2.attack(f1);

if(f1.isDead())
break;   

}while(!f1.isDead());

}

}

Output:

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