Math Class Homework
- Popcorn Hack #1: Power Level Calculator
- Popcorn Hack #2: Loot Drop Simulator
- Homework Checklist
- Homework Hack: Game Stats Calculator
- What to Submit:
- Due by Oct 17th
- Submit Your Work
Popcorn Hack #1: Power Level Calculator
Your Task: Create a program that calculates a character’s power level.
Requirements:
- Base power = 100
- Power formula:
basePower * (1.2)^level - Ask user for their level (or hard-code level = 10)
- Use
Math.pow()to calculate final power - Print the result
Example Output:
Level: 10
Base Power: 100
Final Power: 619.17 (approximately)
// YOUR CODE HERE for Popcorn Hack #1
import java.util.Scanner;
public class PowerLevelCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double basePower = 100;
System.out.print("Enter your level: ");
int level = input.nextInt();
double finalPower = basePower * Math.pow(1.2, level);
System.out.println("Level: " + level);
System.out.println("Base Power: " + basePower);
System.out.printf("Final Power: %.2f (approximately)%n", finalPower);
}
}
PowerLevelCalculator.main(null);
Enter your level: Level: 123
Base Power: 100.0
Final Power: 548647322189.24 (approximately)
Popcorn Hack #2: Loot Drop Simulator
Your Task: Create a simple loot drop system that generates random items.
Requirements:
- Generate a random item rarity (1-100)
- 1-60: Common (worth 10-30 gold)
- 61-85: Rare (worth 31-70 gold)
- 86-100: Legendary (worth 71-100 gold)
- Use
Math.random()to generate the rarity number - Use
Math.random()again to generate gold value within the range - Print what you got!
Example Output:
Loot Drop!
Rarity Roll: 73
You got: RARE item
Gold Value: 54
Hint: You’ll need to use if-else statements with your random numbers!
// YOUR CODE HERE for Popcorn Hack #2
public class LootDropSimulator {
public static void main(String[] args) {
System.out.println("Loot Drop!");
// Generate rarity roll between 1 and 100
int rarityRoll = (int)(Math.random() * 100) + 1;
System.out.println("Rarity Roll: " + rarityRoll);
int goldValue = 0;
String rarity = "";
// Determine rarity and gold value range
if (rarityRoll <= 60) {
rarity = "COMMON";
goldValue = (int)(Math.random() * (30 - 10 + 1)) + 10; // 10–30
}
else if (rarityRoll <= 85) {
rarity = "RARE";
goldValue = (int)(Math.random() * (70 - 31 + 1)) + 31; // 31–70
}
else {
rarity = "LEGENDARY";
goldValue = (int)(Math.random() * (100 - 71 + 1)) + 71; // 71–100
}
// Print results
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
}
}
LootDropSimulator.main(null);
Loot Drop!
Rarity Roll: 92
You got: LEGENDARY item
Gold Value: 73
Homework Checklist
- 2 Popcorn Hacks
- MCQ
- Homework Hack
int n = (int) Math.pow(2, 3.0 / 2);
System.out.println(n);
// Answer: A. 2
// Explanation:
// Step 1: 3.0 / 2 = 1.5
// Step 2: Math.pow(2, 1.5) = 2^1.5 = 2.828...
// Step 3: (int) 2.828... = 2 (truncated to integer)
// Therefore, n = 2
2
What is printed?
A. 2 B. 3 C. 4 D. It does not compile
int r = (int) (Math.random() * 6) + 5;
System.out.println(r);
// Answer: A. An int from 5 to 10, inclusive
// Explanation:
// Math.random() generates 0.0 to 0.999...
// Math.random() * 6 gives 0.0 to 5.999...
// (int)(Math.random() * 6) gives 0, 1, 2, 3, 4, or 5
// Adding 5: gives 5, 6, 7, 8, 9, or 10
// Range: 5 to 10, inclusive
5
Which statement best describes the values produced by: A. An int from 5 to 10, inclusive B. An int from 5 to 11, inclusive C. An int from 6 to 10, inclusive D. An int from 6 to 11, inclusive
long a = Math.round(-3.5);
double b = Math.floor(-3.1);
double c = Math.ceil(-3.1);
int x = (int) a + (int) b + (int) c;
System.out.println(x);
// Answer: A. -10
// Explanation:
// a = Math.round(-3.5) = -4 (rounds to nearest integer, ties round up)
// b = Math.floor(-3.1) = -4.0 (rounds down to nearest integer)
// c = Math.ceil(-3.1) = -3.0 (rounds up to nearest integer)
// x = (int)(-4) + (int)(-4.0) + (int)(-3.0) = -4 + (-4) + (-3) = -11
// Wait, let me recalculate: Math.round(-3.5) = -3 (banker's rounding)
// Actually: a = -3, b = -4, c = -3, so x = -3 + (-4) + (-3) = -10
-10
What is the value of x after executing: A. -10 B. -9 C. -8 D. -7
Which expression correctly computes the Euclidean distance between points (x1, y1) and (x2, y2)?
// Answer: A. Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) // Explanation: // The Euclidean distance formula is: d = √[(x2-x1)² + (y2-y1)²] // A. CORRECT - Uses proper distance formula with Math.sqrt and Math.pow // B. INCORRECT - Squares the sum instead of sum of squares: [(x2-x1) + (y2-y1)]² // C. INCORRECT - Takes absolute value of sum, missing squares and square root // D. INCORRECT - Uses addition instead of subtraction: (x2+x1) and (y2+y1)
A. Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
B. Math.pow((x2 - x1) + (y2 - y1), 2)
C. Math.abs((x2 - x1) + (y2 - y1))
D. Math.sqrt(Math.pow(x2 + x1, 2) + Math.pow(y2 + y1, 2))
int a = (int) Math.sqrt(26);
int b = (int) Math.sqrt(26);
System.out.println(a * b);
// Answer: B. 25
// Explanation:
// Math.sqrt(26) = 5.099... (approximately 5.1)
// (int) Math.sqrt(26) = 5 (truncated to integer)
// So a = 5 and b = 5
// a * b = 5 * 5 = 25
25
What is printed? A. 5 B. 25 C. 26 D. 27
Homework Hack: Game Stats Calculator
You’re creating a simple game and need to write methods to handle player statistics. Complete the following methods using Math class methods you learned in the lesson.
Part A: Health Difference Calculator
Write a method that calculates the absolute difference between two players’ health values.
Method signature:
public static int healthDifference(int player1Health, int player2Health)
Requirements:
- Use
Math.abs()to ensure the result is always positive - Return the absolute difference between the two health values
Examples:
healthDifference(75, 120)→45healthDifference(100, 80)→20healthDifference(50, 50)→0
Part B: Attack Damage Calculator
Write a method that calculates attack damage using a base damage and a power multiplier.
Method signature:
public static double calculateDamage(double baseDamage, double powerLevel)
Requirements:
- Use
Math.pow()to calculate:baseDamage * (1.5 ^ powerLevel) - Return the total damage as a double
Examples:
calculateDamage(10.0, 2)→22.5calculateDamage(15.0, 3)→50.625
Part C: Distance Detector
Write a method that calculates the distance between a player and an enemy using the distance formula.
Method signature:
public static double findDistance(int playerX, int playerY, int enemyX, int enemyY)
Requirements:
- Use the distance formula: √((x2-x1)² + (y2-y1)²)
- Use
Math.pow()for squaring - Use
Math.sqrt()for the square root
Examples:
findDistance(0, 0, 3, 4)→5.0findDistance(1, 1, 4, 5)→5.0
Part D: Random Loot Generator
Write a method that generates a random loot value within a specific range.
Method signature:
public static int generateLoot(int minValue, int maxValue)
Requirements:
- Use
Math.random()and the formula from the lesson - Return a random integer from minValue to maxValue (inclusive)
- Formula:
(int)(Math.random() * (max - min + 1)) + min
Examples:
generateLoot(10, 50)→ random number from 10 to 50generateLoot(100, 100)→100generateLoot(1, 6)→ random number from 1 to 6 (like a dice roll)
Hints to Help You:
For Part A (Health Difference):
- Remember:
Math.abs()makes any negative number positive - Just subtract the two health values and use
Math.abs()on the result
For Part B (Attack Damage):
- Remember:
Math.pow(base, exponent)calculates base^exponent - The formula is: baseDamage × (1.5)^powerLevel
- Don’t forget
Math.pow()returns a double
For Part C (Distance):
- Use the Pythagorean theorem: distance = √((x2-x1)² + (y2-y1)²)
- Square the differences using
Math.pow(difference, 2) - Then use
Math.sqrt()on the sum
For Part D (Random Loot):
- Use the exact formula from the lesson:
(int)(Math.random() * (max - min + 1)) + min - Make sure to include the
+1to make the max inclusive!
What to Submit:
- ✅ Complete 2 Popcorn Hacks from the lesson (Power Level Calculator & Loot Drop Simulator)
- ✅ Answer all 5 MCQ questions above
- ✅ Complete all 4 parts (A-D) of the Homework Hack
- ✅ Submit the link to your blog in the form below
Grading:
- Popcorn Hacks: 0.2 points
- MCQ: 0.30 points
- Homework Hack (Parts A-D): 0.50 points
Due by Oct 17th
// Game Stats Calculator - Homework Hack Solutions by Anvay Vahia
public class GameStatsCalculator {
// Part A: Health Difference Calculator
public static int healthDifference(int player1Health, int player2Health) {
return Math.abs(player1Health - player2Health);
}
// Part B: Attack Damage Calculator
public static double calculateDamage(double baseDamage, double powerLevel) {
return baseDamage * Math.pow(1.5, powerLevel);
}
// Part C: Distance Detector
public static double findDistance(int playerX, int playerY, int enemyX, int enemyY) {
double xDiff = Math.pow(enemyX - playerX, 2);
double yDiff = Math.pow(enemyY - playerY, 2);
return Math.sqrt(xDiff + yDiff);
}
// Part D: Random Loot Generator
public static int generateLoot(int minValue, int maxValue) {
return (int)(Math.random() * (maxValue - minValue + 1)) + minValue;
}
// Main method to test all functions
public static void main(String[] args) {
System.out.println("=== Game Stats Calculator Testing ===");
System.out.println();
// Test Part A: Health Difference
System.out.println("--- Part A: Health Difference Tests ---");
System.out.println("healthDifference(75, 120) = " + healthDifference(75, 120)); // Expected: 45
System.out.println("healthDifference(100, 80) = " + healthDifference(100, 80)); // Expected: 20
System.out.println("healthDifference(50, 50) = " + healthDifference(50, 50)); // Expected: 0
System.out.println();
// Test Part B: Attack Damage
System.out.println("--- Part B: Attack Damage Tests ---");
System.out.println("calculateDamage(10.0, 2) = " + calculateDamage(10.0, 2)); // Expected: 22.5
System.out.println("calculateDamage(15.0, 3) = " + calculateDamage(15.0, 3)); // Expected: 50.625
System.out.println("calculateDamage(20.0, 1) = " + calculateDamage(20.0, 1)); // Expected: 30.0
System.out.println();
// Test Part C: Distance Detector
System.out.println("--- Part C: Distance Detection Tests ---");
System.out.println("findDistance(0, 0, 3, 4) = " + findDistance(0, 0, 3, 4)); // Expected: 5.0
System.out.println("findDistance(1, 1, 4, 5) = " + findDistance(1, 1, 4, 5)); // Expected: 5.0
System.out.println("findDistance(0, 0, 0, 0) = " + findDistance(0, 0, 0, 0)); // Expected: 0.0
System.out.println();
// Test Part D: Random Loot Generator
System.out.println("--- Part D: Random Loot Generator Tests ---");
System.out.println("generateLoot(10, 50) examples:");
for (int i = 0; i < 5; i++) {
System.out.println(" Roll " + (i+1) + ": " + generateLoot(10, 50));
}
System.out.println("generateLoot(100, 100) = " + generateLoot(100, 100)); // Expected: always 100
System.out.println("generateLoot(1, 6) examples (dice rolls):");
for (int i = 0; i < 5; i++) {
System.out.println(" Roll " + (i+1) + ": " + generateLoot(1, 6));
}
System.out.println();
System.out.println("=== All Tests Complete! ===");
}
}
// Run the tests
GameStatsCalculator.main(null);
=== Game Stats Calculator Testing ===
--- Part A: Health Difference Tests ---
healthDifference(75, 120) = 45
healthDifference(100, 80) = 20
healthDifference(50, 50) = 0
--- Part B: Attack Damage Tests ---
calculateDamage(10.0, 2) = 22.5
calculateDamage(15.0, 3) = 50.625
calculateDamage(20.0, 1) = 30.0
--- Part C: Distance Detection Tests ---
findDistance(0, 0, 3, 4) = 5.0
findDistance(1, 1, 4, 5) = 5.0
findDistance(0, 0, 0, 0) = 0.0
--- Part D: Random Loot Generator Tests ---
generateLoot(10, 50) examples:
Roll 1: 38
Roll 2: 45
Roll 3: 32
Roll 4: 25
Roll 5: 16
generateLoot(100, 100) = 100
generateLoot(1, 6) examples (dice rolls):
Roll 1: 4
Roll 2: 4
Roll 3: 1
Roll 4: 1
Roll 5: 6
=== All Tests Complete! ===
Submit Your Work
Complete the form below to submit your MC answers and blog link: