CSA Unit 1.11

Popcorn Hack #1

public class PowerLevelCalculator {
    public static void main(String[] args) {
        int level = 10; // you can change this or take input from user
        double basePower = 100;
        double finalPower = basePower * Math.pow(1.2, level);

        System.out.println("Level: " + level);
        System.out.println("Base Power: " + basePower);
        System.out.println("Final Power: " + finalPower);
    }
}
Level: 10
Base Power: 100
Final Power: 619.17

Popcorn Hack #2

public class LootDropSimulator {
    public static void main(String[] args) {
        // Start the loot drop simulation
        System.out.println("Loot Drop!");

        // Generate a random number from 1–100 to determine item rarity
        int rarityRoll = (int)(Math.random() * 100) + 1;
        System.out.println("Rarity Roll: " + rarityRoll);

        // Declare variables for rarity name and gold value
        String rarity;
        int goldValue;

        // Check the rarity range based on the random roll
        if (rarityRoll <= 60) {
            // 1–60 → Common item
            rarity = "COMMON";
            // Generate gold between 10 and 30 (inclusive)
            goldValue = (int)(Math.random() * (30 - 10 + 1)) + 10;
        } else if (rarityRoll <= 85) {
            rarity = "RARE";
            goldValue = (int)(Math.random() * (70 - 31 + 1)) + 31;
        } else {
            rarity = "LEGENDARY";
            goldValue = (int)(Math.random() * (100 - 71 + 1)) + 71;
        }

        // Display the results of the loot drop
        System.out.println("You got: " + rarity + " item");
        System.out.println("Gold Value: " + goldValue);
    }
}

Loot Drop!
Rarity Roll: 73
You got: RARE item
Gold Value: 54

Homework Hack

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) {
        return Math.sqrt(Math.pow(enemyX - playerX, 2) + Math.pow(enemyY - playerY, 2));
    }

    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) {
        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)); 

        System.out.println("\nPart B: Attack Damage");
        System.out.println(calculateDamage(10.0, 2)); 
        System.out.println(calculateDamage(15.0, 3));

        System.out.println("\nPart C: Distance Detector");
        System.out.println(findDistance(0, 0, 3, 4));
        System.out.println(findDistance(1, 1, 4, 5)); 

        System.out.println("\nPart D: Random Loot Generator");
        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 Detector
5.0
5.0

Part D: Random Loot Generator
37
100