7. Given the Player class below: public class Player { public int strength; publ
ID: 3650692 • Letter: 7
Question
7. Given the Player class below:public class Player {
public int strength;
public int defense;
public boolean magic;
}
implement a method public boolean winsAgainst(Player other) which returns true if the player can beat or tie other in a fight, false otherwise. The way you determine who wins in a fight is by following these rules:
a. If a player has magic and the other player does not then the player with magic wins if his strength + defense is greater than the other player's defense.
b. If both have magic then the one with the highest defense wins.
c. If neither has magic then the one with the highest defense + magic wins
Explanation / Answer
Please rate...
Program Player.java
===================================================
class Player
{
public int strength;
public int defense;
public boolean magic;
public boolean winsAgainst(Player other)
{
if(other.magic && !this.magic)
{
if((other.strength+other.defense)>this.defense)return false;
else return true;
}
if(other.magic && this.magic)
{
if(other.defense>this.defense)return false;
else return true;
}
if(!other.magic && !this.magic)
{
if((other.strength+other.defense)>(this.defense+this.strength))return false;
else return true;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.