1.11 Math_classhw
---
layout: post
toc: true
title: Math Class Homework
menu: nav/CSA_Nunits/csaunit1.html
permalink: /csa/unit_01/1_11_hw
author: Gurshawn, Anvay, Akshay
---
Homework Checklist
- 2 Popcorn Hacks
- MCQ
- Homework Hack
Popcorn Hack 1
public class PowerLevelCalculator {
public static void main(String[] args) {
int basePower = 100;
int level = 10; // hard-coded; change or use the input version below
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%n", finalPower); // prints 619.17 for level 10
}
}
Popcorn Hack 2
public class LootDropSimulator {
public static void main(String[] args) {
System.out.println("Loot Drop!");
// Rarity roll: 1–100 (inclusive)
int rarityRoll = (int) (Math.random() * 100) + 1;
System.out.println("Rarity Roll: " + rarityRoll);
String rarity;
int goldValue;
if (rarityRoll <= 60) {
rarity = "COMMON";
goldValue = randomInRange(10, 30);
} else if (rarityRoll <= 85) {
rarity = "RARE";
goldValue = randomInRange(31, 70);
} else {
rarity = "LEGENDARY";
goldValue = randomInRange(71, 100);
}
System.out.println("You got: " + rarity + " item");
System.out.println("Gold Value: " + goldValue);
}
// Inclusive random integer helper: [min, max]
private static int randomInRange(int min, int max) {
return (int) (Math.random() * (max - min + 1)) + min;
}
}
int n = (int) Math.pow(2, 3.0 / 2);
System.out.println(n);
What is printed?
A. 2 B. 3 C. 4 D. It does not compile
int r = (int) (Math.random() * 6) + 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;
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)? 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);
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
public class GameStats {
// 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) {
// baseDamage * (1.5 ^ 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 dx = enemyX - playerX;
double dy = enemyY - playerY;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
}
// Part D: Random Loot Generator
public static int generateLoot(int minValue, int maxValue) {
// Optional safety: swap if inputs are reversed
if (minValue > maxValue) {
int t = minValue;
minValue = maxValue;
maxValue = t;
}
// Inclusive range
return (int)(Math.random() * (maxValue - minValue + 1)) + minValue;
}
// Quick demo using the examples
public static void main(String[] args) {
// Part A examples
System.out.println(healthDifference(75, 120)); // 45
System.out.println(healthDifference(100, 80)); // 20
System.out.println(healthDifference(50, 50)); // 0
// Part B examples
System.out.println(calculateDamage(10.0, 2)); // 22.5
System.out.println(calculateDamage(15.0, 3)); // 50.625
// Part C examples
System.out.println(findDistance(0, 0, 3, 4)); // 5.0
System.out.println(findDistance(1, 1, 4, 5)); // 5.0
// Part D examples (random outputs)
System.out.println(generateLoot(10, 50)); // 10..50
System.out.println(generateLoot(100, 100)); // 100
System.out.println(generateLoot(1, 6)); // 1..6
}
}
Submit Your Work
Complete the form below to submit your MC answers and blog link: