Question: I'm having trouble with my fighter class, what method can be used to make sure a dead fighter doesn't attack a living fighter and vice

I'm having trouble with my fighter class, what method can be used to make sure a dead fighter doesn't attack a living fighter and vice versa

this is what i have so far

import java.util.Scanner;

public class Fighter { private String name; private double health; private double strength;

public void setName(String Name){ this.name=Name; } public void setHealth(double newHealth){ health=newHealth; } public void setStrength(double newStrength){ strength=newStrength; } public String getName(){ return name; } public double getHealth(){ return health; } public double getStrength(){ return strength; } public void attack(Fighter foe){ double newhealth; newhealth=Math.random()*strength; foe.setHealth(health-newhealth); printChange(newhealth,foe);

}

public boolean isDead(){ if (health>0){ return true; } else { return false; } } public void printChange(double c,Fighter nme){ System.out.println(nme.getName()); System.out.println("The change amount of health:"+ c); System.out.println("The resulting health for " + name + " is now " + health);

} public static void main(String[] args) { Scanner sc = new Scanner(System.in); Fighter fighter1 = new Fighter(); Fighter fighter2 = new Fighter(); String name1; String name2; double health; double Strength; System.out.println("Enter the First Fighter's name"); name1=sc.next(); System.out.println("Enter the Second Fighter's name"); name2=sc.next(); fighter1.setName(name1); fighter2.setName(name2); fighter1.setHealth(10); fighter2.setHealth(10); fighter1.setStrength(5); fighter2.setStrength(5); while (!fighter1.isDead()&&!fighter2.isDead()){ fighter1.attack(fighter2); if (fighter1.isDead()&&!fighter2.isDead()){ fighter2.attack(fighter1); } } if(fighter1.getHealth()>0){ System.out.println(" Player 2 Wins "); } else { System.out.println("Player 1 wins"); }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!