Programming Exercise 03 – Math, Random, Enums Authd S133) 2133 Prob 11. Create a local BendingType variable called heroType and assign a random BendingType to it. Water. Earth. Fire. Air. While the Avatar cycle has seemingly ended, the 4 nations are preparing for war and it is up to you, fellow 1331 student, to help determine who will win! As each nation gathers their armies, the fire nation a. You can do this using this line: BendingType heroType = BendingType.values () [rand.nextInt (5)]; decides to attack the 3 other nations, forcing the remaining 3 to ally with each other. As all the nations summon 12. Create a double variable called health and assign a random value in the range [50, 150). heroes to help them defeat the enemy, and you will have to write a program to determine which army has the a. Make sure all your floating-point values are rounded to 2 decimal places using Math.round (). strongest support from their heroes. 13. Create a double variable called power and assign a random value in the range (40o, 120] to it. Your assignment will consist of 2 main parts: a. Make sure all your floating-point values are rounded to 2 decimal places using Math.round (). 14. If the heroType is FIRE, then health and power will be added to averageFireHealth and averageFirePower, respectively. Increment numFire appropriately. 15. If heroType is NON_BENDER, multiply power by boomerangBoost. 16. If the heroType is NON_BENDER, AIR, WATER, or EARTH, then health and power will be added to Determining how many heroes are summoned, which nation they belong to and what statistics they have. Printing out the hero information and averaging it out for the hero's kingdom to see who wins. Some notes for all variables in the assignment: averageAllianceHealth and averageAlliancePower, respectively. Increment numAlliance appropriately. 17. Print the status of the hero. You can follow the following format: " hero has been summoned by his army, adding power and health to the army ." 18. Once the loop has completed, divide each of the averages, averageFirePower, averageFireHealth, averageAlliancePower, and averageAllianceHealth by their total number of respective heroes, numFire or numAlliance, to get the correctaverage. When asked to generate a random variable, you must use java.util.Random II. When asked to generate a floating-point value, round the value to 2 decimal places. You can only use Math.round () to round the value, but remember that you have operators like *,-, and / to help you achieve proper rounding. I. Solution Description 1. Create a public enum called BendingType. 19. Print out the following statistics: a. This special "class" should be defined in a separate file. a. "The Fire Nation has an average of power and 2. Populate the enum with the values AIR, WATER, FIRE, EARTH, NON_BENDER. 3. Create a public class called BendingBattlefield 4. Create the main method health." b. "The Alliance has an average of power and health." 5. Inside, create the following variables: 20. Calculate whether the Fire nation or the Alliance won by comparing 2 * averagePower + 3 * averageHealth of each side and checking which one is larger. If they are equal, then the Alliance wins. a. An instance of Random named rand. An int called numHeroes and assign to it a random value in the range [50, 100]. A double called boomerangBoost and assign to it a random value in the range [3, 7). b. a. If the Fire Nation won, print C. "The Fire Nation won!" Note the interval notation: b. else if the Alliance won, print "The Alliance won!" [a, b] means a number x such that a <= x <= b. (a, b] means a number x such that a < [a, b) means a number x such that a <= x < b. x <= b. Example Outputs Please refer to the PE clarification thread on Ed for examples. (a, b) means a number x such that a < x < b. Create four double variables: averageFirePower, averageFireHealth, HINT: To help debug your code, try inserting print statements in places where variables are changed. 6. averageAlliancePower, and averageAllianceHealth. 7. Print numHeroes with the following format: "Selecting heroes." 8. Create an int named numFire that will keep a running total of the number of heroes belonging to the Feature Restrictions There are a few features and methods in Java that overly simplify the concepts we are trying to teach or break our auto grader. For that reason, do not use any of the following in your final submission: FIRE nation. var (the reserved keyword) 9. Create an int named numAlliance that will keep a running total of the number of heroes belonging to System.exit the AIR, WATER, EARTH, or NON BENDER nations. 10. Create a loop that terminates after numHeroes iterations. Perform steps 11 to 17 inside the loop. Import Restrictions 白 Sample Output: Selecting 93 heroes. AIR hero has been summoned by his army, adding 49.76 power and 61.37 health to the army. WATER hero has been summoned by his army, adding 47.22 power and 73.33 health to the army. FIRE hero has been summoned by his army, adding 103.90 power and 91.67 health to the army. FIRE hero has been summoned by his army, adding 57.56 power and 64.65 health to the army. AIR hero has been summoned by his army, adding 71.16 power and 58.62 health to the army. NON_BENDER hero has been summoned by his army, adding 618.86 power and 108.20 health to the army. AIR hero has been summoned by his army, adding 115.35 power and 132.83 health to the army. EARTH hero has been summoned by his army, adding 51.41 power and 105.28 health to the army. EARTH hero has been summoned by his army, adding 90.06 power and 105.74 health to the army. ..(93 hero print statements in total) AIR hero has been summoned by his army, adding 81.25 power and 144.14 health to the army. WATER hero has been summoned by his army, adding 96.97 power and 117.30 health to the army. EARTH hero has been summoned by his army, adding 102.70 power and 92.30 health to the army. EARTH hero has been summoned by his army, adding 118.07 power and 79.76 health to the army. FIRE hero has been summoned by his army, adding 101.52 power and 58.58 health to the army. EARTH hero has been summoned by his army, adding 118.07 power and 54.09 health to the army. The Fire Nation has a average of 81.33 power and 94.65 health. The Alliance has a average of 152.33 power and 101.98 health. The Alliance won!
Please write Java Code and make sure the output is the same as the sample output. Please pay attention to the expected output.
Feature Restrictions:
• var (the reserved keyword)
• System.exit
Import Restrictions
You can import only the following classes:
• java.util.Random
Thanks!
![Programming Exercise 03 – Math, Random, Enums
Authd
S133)
2133
Prob
11. Create a local BendingType variable called heroType and assign a random BendingType to it.
Water. Earth. Fire. Air. While the Avatar cycle has seemingly ended, the 4 nations are preparing for war and it is up
to you, fellow 1331 student, to help determine who will win! As each nation gathers their armies, the fire nation
a. You can do this using this line:
BendingType heroType = BendingType.values () [rand.nextInt (5)];
decides to attack the 3 other nations, forcing the remaining 3 to ally with each other. As all the nations summon
12. Create a double variable called health and assign a random value in the range [50, 150).
heroes to help them defeat the enemy, and you will have to write a program to determine which army has the
a. Make sure all your floating-point values are rounded to 2 decimal places using Math.round ().
strongest support from their heroes.
13. Create a double variable called power and assign a random value in the range (40o, 120] to it.
Your assignment will consist of 2 main parts:
a. Make sure all your floating-point values are rounded to 2 decimal places using Math.round ().
14. If the heroType is FIRE, then health and power will be added to averageFireHealth and
averageFirePower, respectively. Increment numFire appropriately.
15. If heroType is NON_BENDER, multiply power by boomerangBoost.
16. If the heroType is NON_BENDER, AIR, WATER, or EARTH, then health and power will be added to
Determining how many heroes are summoned, which nation they belong to and what statistics they have.
Printing out the hero information and averaging it out for the hero's kingdom to see who wins.
Some notes for all variables in the assignment:
averageAllianceHealth and averageAlliancePower, respectively. Increment
numAlliance appropriately.
17. Print the status of the hero. You can follow the following format:
"<heroType> hero has been summoned by his army, adding <power> power
and <health> health to the army ."
18. Once the loop has completed, divide each of the averages, averageFirePower,
averageFireHealth, averageAlliancePower, and averageAllianceHealth by their total
number of respective heroes, numFire or numAlliance, to get the correctaverage.
When asked to generate a random variable, you must use java.util.Random
II. When asked to generate a floating-point value, round the value to 2 decimal places. You can only use
Math.round () to round the value, but remember that you have operators like *,-, and / to help you
achieve proper rounding.
I.
Solution Description
1. Create a public enum called BendingType.
19. Print out the following statistics:
a. This special "class" should be defined in a separate file.
a.
"The Fire Nation has an average of <averageFirePower> power and
2. Populate the enum with the values AIR, WATER, FIRE, EARTH, NON_BENDER.
3. Create a public class called BendingBattlefield
4. Create the main method
<averageFireHealth> health."
b. "The Alliance has an average of <averageAlliancePower> power and
<averageAllianceHealth> health."
5. Inside, create the following variables:
20. Calculate whether the Fire nation or the Alliance won by comparing 2 * averagePower + 3 *
averageHealth of each side and checking which one is larger. If they are equal, then the Alliance wins.
a.
An instance of Random named rand.
An int called numHeroes and assign to it a random value in the range [50, 100].
A double called boomerangBoost and assign to it a random value in the range [3, 7).
b.
a.
If the Fire Nation won, print
C.
"The Fire Nation won!"
Note the interval notation:
b. else if the Alliance won, print
"The Alliance won!"
[a, b] means a number x such that a <= x <= b.
(a, b] means a number x such that a <
[a, b) means a number x such that a <= x < b.
x <= b.
Example Outputs
Please refer to the PE clarification thread on Ed for examples.
(a, b) means a number x such that a <
x < b.
Create four double variables: averageFirePower, averageFireHealth,
HINT: To help debug your code, try inserting print statements in places where variables are changed.
6.
averageAlliancePower, and averageAllianceHealth.
7. Print numHeroes with the following format:
"Selecting <numHeroes> heroes."
8. Create an int named numFire that will keep a running total of the number of heroes belonging to the
Feature Restrictions
There are a few features and methods in Java that overly simplify the concepts we are trying to teach or break our
auto grader. For that reason, do not use any of the following in your final submission:
FIRE nation.
var (the reserved keyword)
9. Create an int named numAlliance that will keep a running total of the number of heroes belonging to
System.exit
the AIR, WATER, EARTH, or NON BENDER nations.
10. Create a loop that terminates after numHeroes iterations. Perform steps 11 to 17 inside the loop.
Import Restrictions
白](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8a1ff5b0-6d62-4156-84f4-ecc9607dcad9%2F8ce31cd9-e98f-4443-b448-9a06acff3d54%2Fmur0x5e_processed.png&w=3840&q=75)


import java.util.Random;
public class BendingBattlefield {
public static void main(String[] args) {
Random rand = new Random();
int numHeroes = rand.nextInt(50, 101); // 50 included, 101 is not included
double boomerangBoost = rand.nextDouble(3, 7); // 3 included, 7 not included
double averageFirePower = 0, averageFireHealth = 0, averageAlliancePower = 0, averageAllianceHealth = 0;
System.out.println(String.format("Selecting %d heroes.", numHeroes));
int numFire = 0, numAlliance = 0;
for (int i = 1; i <= numHeroes; i++) {
BendingType heroType = BendingType.values()[rand.nextInt(5)];
double health = round(rand.nextDouble(50, 150));
double power = round(rand.nextDouble(40, 120));
if (power == 40)
power += 0.01; // as we are rounding to 2 decimal places 40.01 is the smallest number in range
switch (heroType) {
case FIRE:
averageFireHealth += health;
averageFirePower += power;
numFire++;
break;
case NON_BENDER:
power = power * boomerangBoost;
case AIR:
case WATER:
case EARTH:
averageAllianceHealth += health;
averageAlliancePower += power;
numAlliance++;
break;
}
System.out.println(String.format(
"%s hero has been summoned by his army, adding %.2f power and %.2f health to the army.",
heroType.name(), power, health));
}
averageFireHealth = numFire == 0 ? averageFireHealth : averageFireHealth / numFire;
averageFirePower = numFire == 0 ? averageFirePower : averageFirePower / numFire;
averageAllianceHealth = numAlliance == 0 ? averageAllianceHealth : averageAllianceHealth / numAlliance;
averageAlliancePower = numAlliance == 0 ? averageAlliancePower : averageAlliancePower / numAlliance;
System.out.println(String.format("The Fire Nation has an average of %.2f power and %.2f health.",
averageFirePower, averageFireHealth));
System.out.println(String.format("The Alliance has an average of %.2f power and %.2f health.",
averageAlliancePower, averageAllianceHealth));
if (2 * averageAlliancePower + 3 * averageAllianceHealth >= 2 * averageFirePower + 3 * averageFireHealth)
System.out.println("The Alliance won!");
else
System.out.println("The Fire Nation won!");
}
private static double round(double val) {
val = val * 100;
val = Math.round(val);
return val / 100;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps









