Popcorn Hacks
Popcorn Hack 1
// Popcorn Hack #1: Power Level Calculator
// This program calculates a character's final power level based on a hard-coded level.
// Base power of the character
double basePower = 100;
// Hard-coded level
int level = 10;
// Calculate final power using formula: basePower * (1.2)^level
double finalPower = basePower * Math.pow(1.2, level);
// Print the results
System.out.println("Level: " + level);
System.out.println("Base Power: " + basePower);
System.out.printf("Final Power: %.2f%n", finalPower);
Level: 10
Base Power: 100.0
Final Power: 619.17
java.io.PrintStream@44dc0768
Testing with different values
// Testing the Power Level Calculator with different level values
double basePower = 100;
// Test different levels
int[] testLevels = {5, 10, 15, 20};
for (int level : testLevels) {
// Calculate final power for each level
double finalPower = basePower * Math.pow(1.2, level);
// Print the result
System.out.printf("Level: %d | Final Power: %.2f%n", level, finalPower);
}
Level: 5 | Final Power: 248.83
Level: 10 | Final Power: 619.17
Level: 15 | Final Power: 1540.70
Level: 20 | Final Power: 3833.76
Popcorn Hack 2
// Popcorn Hack #2: Loot Drop System
// This program simulates a random loot drop with different rarities and gold values.
// Print starting message
System.out.println("Loot Drop!");
// Generate a random rarity roll between 1 and 100 (inclusive)
int rarityRoll = (int)(Math.random() * 100) + 1;
System.out.println("Rarity Roll: " + rarityRoll);
// Variables for rarity type and gold value
String rarity;
int goldValue;
// Determine rarity and gold value based on rarityRoll
if (rarityRoll >= 1 && rarityRoll <= 60) {
rarity = "COMMON";
goldValue = (int)(Math.random() * (30 - 10 + 1)) + 10; // 10–30 gold
} else if (rarityRoll <= 85) {
rarity = "RARE";
goldValue = (int)(Math.random() * (70 - 31 + 1)) + 31; // 31–70 gold
} else {
rarity = "LEGENDARY";
goldValue = (int)(Math.random() * (100 - 71 + 1)) + 71; // 71–100 gold
}
// Print the result
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
Loot Drop!
Rarity Roll: 22
You got: COMMON item
Gold Value: 17
Testing with different values
// Testing the Loot Drop System multiple times
// This will simulate 5 random loot drops to show different possible outcomes
for (int i = 1; i <= 5; i++) {
System.out.println("Loot Drop #" + i + "!");
// Generate rarity roll (1–100)
int rarityRoll = (int)(Math.random() * 100) + 1;
String rarity;
int goldValue;
// Determine rarity and gold value
if (rarityRoll <= 60) {
rarity = "COMMON";
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;
}
// Print results for this drop
System.out.println("Rarity Roll: " + rarityRoll);
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
System.out.println("---------------------------");
}
Loot Drop #1!
Rarity Roll: 15
You got: COMMON item
Gold Value: 14
---------------------------
Loot Drop #2!
Rarity Roll: 50
You got: COMMON item
Gold Value: 18
---------------------------
Loot Drop #3!
Rarity Roll: 90
You got: LEGENDARY item
Gold Value: 97
---------------------------
Loot Drop #4!
Rarity Roll: 34
You got: COMMON item
Gold Value: 10
---------------------------
Loot Drop #5!
Rarity Roll: 13
You got: COMMON item
Gold Value: 11
---------------------------
Homework Hacks
// This program defines several methods to handle player statistics in a simple game.
public class GameStatsCalculator {
// Part A: Health Difference
// Calculates the absolute difference between two players' health values.
public static int healthDifference(int player1Health, int player2Health) {
return Math.abs(player1Health - player2Health);
}
// Part B: Attack Damage
// Calculates total attack damage using a power multiplier.
public static double calculateDamage(double baseDamage, double powerLevel) {
return baseDamage * Math.pow(1.5, powerLevel);
}
// Part C: Distance Detector
// Calculates distance between a player and an enemy using the distance formula.
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));
}
// Part D: Random Loot Generator
// Generates a random integer value between minValue and maxValue (inclusive).
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): " + healthDifference(75, 120));
System.out.println("healthDifference(100, 80): " + healthDifference(100, 80));
System.out.println("healthDifference(50, 50): " + healthDifference(50, 50));
System.out.println("\n=== Part B: Attack Damage ===");
System.out.println("calculateDamage(10.0, 2): " + calculateDamage(10.0, 2));
System.out.println("calculateDamage(15.0, 3): " + calculateDamage(15.0, 3));
System.out.println("\n=== Part C: Distance Detector ===");
System.out.println("findDistance(0, 0, 3, 4): " + findDistance(0, 0, 3, 4));
System.out.println("findDistance(1, 1, 4, 5): " + findDistance(1, 1, 4, 5));
System.out.println("\n=== Part D: Random Loot Generator ===");
System.out.println("generateLoot(10, 50): " + generateLoot(10, 50));
System.out.println("generateLoot(100, 100): " + generateLoot(100, 100));
System.out.println("generateLoot(1, 6): " + generateLoot(1, 6)); // like a dice roll
}
}
GameStatsCalculator.main(null);
=== Part A: Health Difference ===
healthDifference(75, 120): 45
healthDifference(100, 80): 20
healthDifference(50, 50): 0
=== Part B: Attack Damage ===
calculateDamage(10.0, 2): 22.5
calculateDamage(15.0, 3): 50.625
=== Part C: Distance Detector ===
findDistance(0, 0, 3, 4): 5.0
findDistance(1, 1, 4, 5): 5.0
=== Part D: Random Loot Generator ===
generateLoot(10, 50): 33
generateLoot(100, 100): 100
generateLoot(1, 6): 1