Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a combastState class which will be used to help track the outcomes. Using the State Pattern, each state should have a fight ( )
Create a combastState class which will be used to help track the outcomes. Using the State Pattern, each state should have a fight method that takes in Warriors. The first is the attacker and the second is the defender. The method should return the winning Warrior.
States:
Power If the attackers calculated power is greater than the defenders calculated power, the attacker wins. Defender wins on ties.
Attack If the attackers calculated attack is greater than the defenders calculated attack, the attacker wins. Defender wins on ties.
Defense If the attackers calculated defense is greater than the defenders calculated defense, the attacker wins. Defender wins on ties.
Traditional If the attackers calculated attack is greater than the defenders calculated defense, the attacker wins. Defender wins on ties.
Inverse If the attackers calculated defense is greater than the defenders calculated attack, the attacker wins. Attacker wins on ties.
Combat is the user of the state pattern. Combat should have a duel method that takes in Warriors. The first is the attacker and the second is the defender. Go through each of the states. The Warrior that wins the most of the fights is the winner. The method should return the winning Warrior.
Here is the provided code to create the warriors that will be dueling:
public class Warrior
private final int level;
private final int attack;
private final int defense;
protected WarriorBuilder builder
this.level builder.level;
this.attack builder.attack;
this.defense builder.defense;
public int getLevelreturn level;
public int getAttackreturn attack;
public int getDefense defense;
Template method: defines the overall algorithm
public double calculatePower
return calculateAttack calculateDefense calculateBoost;
Abstract methods to be overridden by subclasses
protected int calculateAttack
return attack;
protected int calculateDefense
return defense;
protected double calculateBoost
return ;
Builder class to create Warrior instances
public static class Builder
private final int level;
private int attack ;
private int defense ;
public Builderint level
if level
throw new IllegalArgumentExceptionLevel cannot be negative";
this.level level;
public Builder attackint attack
if attack
throw new IllegalArgumentExceptionAttack cannot be negative";
this.attack attack;
return this;
public Builder defenseint defense
if defense
throw new IllegalArgumentExceptionDefense cannot be negative";
this.defense defense;
return this;
public Warrior build
return new Warriorthis;
public class AggressiveWarrior extends Warrior
private AggressiveWarriorBuilder builder
superbuilder;
@Override
protected int calculateAttack
return getAttack getLevel;
@Override
protected int calculateDefense
return getDefense getLevel;
@Override
protected double calculateBoost
return double getAttack;
public static class Builder extends Warrior.Builder
public Builderint level
superlevel;
this.attackdefense;
@Override
public AggressiveWarrior build
return new AggressiveWarriorthis;
public class DefensiveWarrior extends Warrior
Constructor is private to enforce Builder pattern
private DefensiveWarriorBuilder builder
superbuilder;
@Override
protected int calculateAttack
return getAttack getLevel;
@Override
protected int calculateDefense
return getDefense getLevel;
@Override
protected double calculateBoost
return double getDefense;
public static class Builder extends Warrior.Builder
public Builderint level
superlevel;
this.attackdefense;
@Override
public DefensiveWarrior build
return new DefensiveWarriorthis;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started