Math Class Homework
- Homework Checklist
- Homework Hack: Game Stats Calculator
- What to Submit:
- Due by Oct 17th
- Submit Your Work
Homework Checklist
- 2 Popcorn Hacks
- MCQ
- Homework Hack
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
Submit Your Work
Complete the form below to submit your MC answers and blog link: