1.11 Java Math Class - Power Levels and Random Generation
Categories: homeworkJava homework assignment covering Math.pow(), Math.random(), and conditional logic with gaming examples
Popcorn hack 1
class PowerLevelCalculator {
public static void main(String[] args) {
int basePower = 100;
int level = 10;
double finalPower = basePower * Math.pow(1.2, level);
System.out.println("--- Power Level Calculator ---");
System.out.println("Level: " + level);
System.out.println("Base Power: " + basePower);
System.out.println("Final Power: " + finalPower);
}
}
=== Power Level Calculator === Level: 10 Base Power: 100 Final Power: 619.1736422041505
Popcorn hack 2
class LootDropSimulator {
public static void main(String[] args) {
// Generate random rarity (1-100)
int rarityRoll = (int)(Math.random() * 100) + 1;
String rarity;
int goldValue;
if (rarityRoll >= 1 && rarityRoll <= 60) {
// Common item (10-30 gold)
rarity = "COMMON";
goldValue = (int)(Math.random() * (30 - 10 + 1)) + 10;
}
else if (rarityRoll >= 61 && rarityRoll <= 85) {
rarity = "RARE";
goldValue = (int)(Math.random() * (70 - 31 + 1)) + 31;
}
else {
rarity = "LEGENDARY";
goldValue = (int)(Math.random() * (100 - 71 + 1)) + 71;
}
System.out.println("=== Loot Drop! ===");
System.out.println("Rarity Roll: " + rarityRoll);
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
}
}
Example Output: === Loot Drop! === Rarity Roll: 73 You got: RARE item Gold Value: 54
Hw hacks a-d
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 parts
public static void main(String[] args) {
// Part A Tests
System.out.println("Part A: Health Difference");
System.out.println(healthDifference(75, 120));
System.out.println(healthDifference(100, 80));
System.out.println(healthDifference(50, 50));
// Part B Tests
System.out.println("\nPart B: Attack Damage");
System.out.println(calculateDamage(10.0, 2));
System.out.println(calculateDamage(15.0, 3));
// Part C Tests
System.out.println("\nPart C: Distance");
System.out.println(findDistance(0, 0, 3, 4));
System.out.println(findDistance(1, 1, 4, 5));
// Part D Tests
System.out.println("\nPart D: Random Loot (sample outputs)");
System.out.println(generateLoot(10, 50));
System.out.println(generateLoot(100, 100));
System.out.println(generateLoot(1, 6));
}
}
Part A: Health Difference 45 20 0
Part B: Attack Damage 22.5 50.625
Part C: Distance 5.0 5.0
Part D: Random Loot (sample outputs) [random number between 10-50] 100 [random number between 1-6]