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
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.
I'm trying to find a way to loop the two fighter untill one wins this is 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){ if (newHealth>0){ System.out.println("Health is at 0"); System.exit(0); } } public void setStrength(double newStrength){ strength=newStrength; } public String getName(){ return getName(); } public String getHealth(){ return getHealth(); } public String getStrength(){ return getStrength(); } public void attack(Fighter foe, double setHealth){ double newhealth; newhealth=Math.random()*strength; foe.setHealth(newhealth);
}
public boolean isDead(){ int fighterhealth = 0; if (fighterhealth>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 " + nme + "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 = 100; double Strength=50; 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); if(fighter1.isDead()){ System.out.println(fighter2.getName() + " Wins "); } if(fighter2.isDead()){ System.out.println(fighter1.getName() + " Wins "); } } public static void FighterStats(Fighter fighters, Fighter fighting) { int fightStats; while (fighters.health()<100){ fightStats=fighters.setHealth(); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
