Skip to content

QuestionAnswered step-by-stepImage transcription textState P

Do you have a similar question? Our professional writers have done a similar paper in past. Give Us your instructions and wait for a professional assignment!        

QuestionAnswered step-by-stepImage transcription textState Pattern Each state should have a fight() method that takes in 2 Warriors. The first is the attacker and thesecond is the defender. The method should return the winning Warrior. States: Power?If the attacker’scalculated power is greater than the defender’s calculated power, the attacker wins. Defender win… Show more?ext explanation for why you decided to implement the state pattern the way you did.public interface Warrior { int level = 0; int attack = 0; int defense = 0; public int getLevel(); public int getAttack(); public int getDefense();}public class StrongWarriorDecorator extends Behavior implements Warrior { // warriorType holds what // type of warrior is // passed in the constructor // 1 – AggressiveWarrior // 2 – DefensiveWarrior private final int level; private int attack; private int defense; private int warriorType; // constructor where // AggressiveWarrior is // the parameter StrongWarriorDecorator(AggressiveWarrior warrior) { // multiply attack field // by 2 // set warriorType to 1 this.level = warrior.getLevel(); this.attack = 2 * warrior.getAttack(); this.defense = warrior.getDefense(); this.warriorType = 1; } // constructor where // DefensiveWarrior is // the parameter StrongWarriorDecorator(DefensiveWarrior warrior) { // multiply attack field // by 2 // set warriorType to 2 this.level = warrior.getLevel(); this.attack = 2 * warrior.getAttack(); this.defense = warrior.getDefense(); this.warriorType = 2; } @Override public int getLevel() { return this.level; } @Override public int getAttack() { return this.attack; } @Override public int getDefense() { return this.defense; } @Override public int calculateAttack() { // AggressiveWarrior has twice // the attack if (this.warriorType == 1) { return this.attack + 2 * this.level; } else if (this.warriorType == 2) { return this.attack + this.level; } return 0; } @Override public int calculateDefense() { // DefensiveWarrior has twice // the defense if (this.warriorType == 1) { return this.defense + this.level; } else if (this.warriorType == 2) { return this.defense + 2 * this.level; } return 0; } @Override public double calculateBoost() { // AggressiveWarrior has boost // half the attack // DefensiveWarrior has boost // half the defense if (this.warriorType == 1) { return this.attack / 2.0; } else if (this.warriorType == 2) { return this.defense / 2.0; } return 0.0; }}// DefensiveWarrior.java// extend DefensiveWarrior// ?o class Behaviorpublic class DefensiveWarrior extends Behavior implements Warrior { private final int level; private int attack = 2; private int defense = 3; DefensiveWarrior(Builder builder) { this.level = builder.getLevel(); this.attack = builder.getAttack(); this.defense = builder.getDefense(); } @Override public int getLevel() { return this.level; } @Override public int getAttack() { return this.attack; } @Override public int getDefense() { return this.defense; } public static class Builder { private int level; private int attack = 2; private int defense = 3; Builder(int level) { this.level = level; } public Builder level(int level) { this.level = level; return this; } public Builder attack(int attack) { this.attack = attack; return this; } public Builder defense(int defense) { this.defense = defense; return this; } public int getLevel() { return this.level; } public int getAttack() { return this.attack; } public int getDefense() { return this.defense; } public DefensiveWarrior build() { String errorMessage = “”; if (level < 0) { errorMessage += “Level must be greater than 0. “; } if (attack < 0) { errorMessage += “Attack must be greater than 0. “; } if (defense < 0) { errorMessage += “Defense must be greater than 0. “; } if (errorMessage.equals(“”)) { return new DefensiveWarrior(this); } throw new IllegalStateException(errorMessage); } } // overrides abstract // method calculateAttack // to compute and return // calculateAttack @Override public int calculateAttack() { // return sum of // attack and level return this.attack + this.level; } // overrides abstract // method calculateDefense // to compute and return // calculateDefense @Override public int calculateDefense() { // return the sum of // twice the level // and defense return this.defense + 2 * this.level; } // overrides abstract // method calculateBoost // to compute and return // calculateBoost @Override public double calculateBoost() { // return half of defense return this.defense / 2.0; }}////////// Behavior.java// the abstract class// ?hat holds// ?alculateAttack, calculateDefense// ?alculateBoost and calculatePowerpublic abstract class Behavior { // calculateAttack, // calculateDefense and // calculateBoost has own // specific implementations // for AggressiveWarrior and // DefensiveWarrior // so mark as abstract public abstract int calculateAttack(); public abstract int calculateDefense(); public abstract double calculateBoost(); // calculatePower is the same // for both // AggressiveWarrior and // DefensiveWarrior // so implement public double calculatePower() { // return sum of // calculateAttack, // calculateDefense and // calculateBoost return this.calculateAttack() + this.calculateDefense() + this.calculateBoost(); }}public class ArmoredWarriorDecorator extends Behavior implements Warrior { // warriorType holds what // type of warrior is // passed in the constructor // 1 – AggressiveWarrior // 2 – DefensiveWarrior private final int level; private int attack; private int defense; private int warriorType; // constructor where // AggressiveWarrior is // the parameter ArmoredWarriorDecorator(AggressiveWarrior warrior) { // multiply defense field // by 2 // set warriorType to 1 this.level = warrior.getLevel(); this.attack = warrior.getAttack(); this.defense = 2 * warrior.getDefense(); this.warriorType = 1; } // constructor where // DefensiveWarrior is // the parameter ArmoredWarriorDecorator(DefensiveWarrior warrior) { // multiply defense field // by 2 // set warriorType to 2 this.level = warrior.getLevel(); this.attack = warrior.getAttack(); this.defense = 2 * warrior.getDefense(); this.warriorType = 2; } @Override public int getLevel() { return this.level; } @Override public int getAttack() { return this.attack; } @Override public int getDefense() { return this.defense; } @Override public int calculateAttack() { // AggressiveWarrior has twice // the attack if (this.warriorType == 1) { return this.attack + 2 * this.level; } else if (this.warriorType == 2) { return this.attack + this.level; } return 0; } @Override public int calculateDefense() { // DefensiveWarrior has twice // the defense if (this.warriorType == 1) { return this.defense + this.level; } else if (this.warriorType == 2) { return this.defense + 2 * this.level; } return 0; } @Override public double calculateBoost() { // AggressiveWarrior has boost // half the attack // DefensiveWarrior has boost // half the defense if (this.warriorType == 1) { return this.attack / 2.0; } else if (this.warriorType == 2) { return this.defense / 2.0; } return 0.0; }}////////// AggressiveWarrior.java// extend AggressiveWarrior// ?o class Behaviorpublic class AggressiveWarrior extends Behavior implements Warrior { private final int level; private int attack = 3; private int defense = 2; AggressiveWarrior(Builder builder) { this.level = builder.getLevel(); this.attack = builder.getAttack(); this.defense = builder.getDefense(); } @Override public int getLevel() { return this.level; } @Override public int getAttack() { return this.attack; } @Override public int getDefense() { return this.defense; } public static class Builder { private int level; private int attack = 3; private int defense = 2; Builder(int level) { this.level = level; } public Builder level(int level) { this.level = level; return this; } public Builder attack(int attack) { this.attack = attack; return this; } public Builder defense(int defense) { this.defense = defense; return this; } public int getLevel() { return this.level; } public int getAttack() { return this.attack; } public int getDefense() { return this.defense; } public AggressiveWarrior build() { String errorMessage = “”; if (level < 0) { errorMessage += “Level must be greater than 0. “; } if (attack < 0) { errorMessage += “Attack must be greater than 0. “; } if (defense < 0) { errorMessage += “Defense must be greater than 0. “; } if (errorMessage.equals(“”)) { return new AggressiveWarrior(this); } throw new IllegalStateException(errorMessage); } } // overrides abstract // method calculateAttack // to compute and return // calculateAttack @Override public int calculateAttack() { // return the sum of // twice the level // and attack return this.attack + 2 * this.level; } // overrides abstract // method calculateDefense // to compute and return // calculateDefense @Override public int calculateDefense() { // return sum of // defense and level return this.defense + this.level; } // overrides abstract // method calculateBoost // to compute and return // calculateBoost @Override public double calculateBoost() { // return half of attack return this.attack / 2.0; }}Computer ScienceEngineering & TechnologyJava ProgrammingCOP 2850CShare Question

Get a plagiarism-free order today   we guarantee confidentiality and a professional paper and we will meet the deadline.    

Leave a Reply

Order a plagiarism free paper today. Get 20% off your first order!

X