Math Class
Popcorn Hack #1
import java.util.Scanner;
public class PowerLevelCalculator {
public static void main(String[] args) {
// Base power
int basePower = 100;
// Ask user for their level
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your level: ");
int level = scanner.nextInt();
// Calculate final power
double finalPower = basePower * Math.pow(1.2, level);
// Print the result
System.out.println("Level: " + level);
System.out.println("Base Power: " + basePower);
System.out.printf("Final Power: %.2f%n", finalPower);
scanner.close();
}
}
PowerLevelCalculator.main(null);
Enter your level: Level: 1000
Base Power: 100
Final Power: 1517910089172245700000000000000000000000000000000000000000000000000000000000000000.00
Popcorn Hack #2
import java.util.Random;
public class LootDropSimulator {
public static void main(String[] args) {
System.out.println("Loot Drop!");
// Generate a random rarity roll (1-100)
Random random = new Random();
int rarityRoll = random.nextInt(100) + 1; // 1 to 100
System.out.println("Rarity Roll: " + rarityRoll);
String rarity;
int goldValue;
// Determine rarity and gold value
if (rarityRoll <= 60) {
rarity = "COMMON";
goldValue = random.nextInt(21) + 10; // 10 to 30
} else if (rarityRoll <= 85) {
rarity = "RARE";
goldValue = random.nextInt(40) + 31; // 31 to 70
} else {
rarity = "LEGENDARY";
goldValue = random.nextInt(30) + 71; // 71 to 100
}
// Print the results
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
}
}
LootDropSimulator.main(null);
Loot Drop!
Rarity Roll: 72
You got: RARE item
Gold Value: 45
MCQ
// MCQ 1
int n = (int) Math.pow(2, 3.0 / 2);
System.out.println(n);
// Output: 2 There answer is A
2
// MCQ 2
int r = (int) (Math.random() * 6) + 5;
System.out.println(r);
// Output: 5 to 10 There answer is A
9
// MCQ 3
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);
// Output: -10 There answer is A
-10
Which expression correctly computes the Euclidean distance between points (x1, y1) and (x2, y2)?
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))
Answer is A.
int a = (int) Math.sqrt(26);
int b = (int) Math.sqrt(26);
System.out.println(a * b);
// Output: 25 There answer is B
25
Homework Hack
public class PlayerStatistics {
// 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));
}
// Part D: Random Loot Generator
public static int generateLoot(int minValue, int maxValue) {
return (int) (Math.random() * (maxValue - minValue + 1)) + minValue;
}
public static void main(String[] args) {
// Test Part A
System.out.println(healthDifference(75, 120)); // Output: 45
System.out.println(healthDifference(100, 80)); // Output: 20
System.out.println(healthDifference(50, 50)); // Output: 0
// Test Part B
System.out.println(calculateDamage(10.0, 2)); // Output: 22.5
System.out.println(calculateDamage(15.0, 3)); // Output: 50.625
// Test Part C
System.out.println(findDistance(0, 0, 3, 4)); // Output: 5.0
System.out.println(findDistance(1, 1, 4, 5)); // Output: 5.0
// Test Part D
System.out.println(generateLoot(10, 50)); // Output: Random number between 10 and 50
System.out.println(generateLoot(100, 100)); // Output: 100
System.out.println(generateLoot(1, 6)); // Output: Random number between 1 and 6
}
}
PlayerStatistics.main(null);
45
20
0
22.5
50.625
5.0
5.0
19
100
1