Week 12 · Objectives 第 12 周 · 学习目标 ← Back to Hub ← 返回主页

Pre-defined Methods & Math Class 预定义方法与 Math 类

Learning Objectives 学习目标

  • Invoke built-in library methods from standard java packages.
  • Apply Math static APIs (pow, sqrt, random) inside geometric algorithms.
  • Perform explicit type casting on returned values to match variable bounds.
  • 从标准 Java 包中调用预定义类库方法。
  • 在几何算法中应用 Math 静态 API(pow、sqrt、random)。
  • 对返回值进行显式类型转换,以匹配变量范围。
Bilingual Navigation:双语导航: Read and toggle languages using the globe button at the top. Test your understanding at the end of this page. 阅读并使用顶部的语言按钮切换中英文。在页面底部进行自我检测。

2 · Pre-defined Libraries & java.lang

Java provides a rich set of pre-written class libraries. The package java.lang contains fundamental classes (such as System, String, and Math) and is automatically imported into every Java program without import statements. Java 提供了丰富的预先编写的类库。java.lang 包中包含了核心的类(例如 SystemStringMath),它会自动导入到每个 Java 程序中,而无需显式编写 import 语句。

3 · The Math Class Static Methods

The Math class contains static methods to perform mathematical calculations. Since they are static, you call them directly using class prefix Math.methodName() without creating objects: Math 类包含了进行数学运算的静态方法。因为是静态方法,您可以直接使用类前缀 Math.methodName() 调用它们,而无需创建 Math 对象:

Method方法 Return Type返回类型 Description & Formula说明与公式 Example Statement示例语句
Math.pow(a, b) double Calculates \(a\) raised to the power of \(b\) (\(a^b\)).计算 \(a\) 的 \(b\) 次幂(\(a^b\))。 Math.pow(2, 3)8.0
Math.sqrt(x) double Calculates positive square root of \(x\) (\(\sqrt{x}\)).计算 \(x\) 的正平方根(\(\sqrt{x}\))。 Math.sqrt(25.0)5.0
Math.random() double Returns positive double value in range \([0.0, 1.0)\).返回 \([0.0, 1.0)\) 范围内的双精度随机数。 Math.random()

4 · Explicit Type Casting

Since Math methods return double-precision values, assigning them to integer variables requires explicit type casting. This is written by prefixing the value with the target type in parentheses: 由于 Math 类的方法通常返回双精度浮点数 (double),若将其赋给整数变量,则需要进行显式类型转换 (type casting)。这可以通过在数值前添加括号及目标类型来编写:

double rand = Math.random() * 6; // range [0.0, 6.0)
int roll = (int)rand + 1;          // casts double to int, range [1, 6]

Applied Case Study: Geometry Utilities Engine应用案例研究:几何工具引擎

Company: Kompas Geometry.
We write code using Math class APIs to compute the hypotenuse length of a right-angled triangle using Pythagoras formula (\(c = \sqrt{a^2 + b^2}\)) and simulate rolling dice.
公司: Kompas Geometry。
我们编写使用 Math 类 API 的代码,应用勾股定理(\(c = \sqrt{a^2 + b^2}\))计算直角三角形斜边长度,并模拟掷骰子。

Geometry Tool Source Code几何工具程序源代码

public class GeometryTool {{
    public static void main(String[] args) {{
        double sideA = 3.0;
        double sideB = 4.0;

        // Pythagoras: hypotenuse c = sqrt(a^2 + b^2)
        double sideC = Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));

        // Random roll simulator: [1, 6]
        int diceRoll = (int)(Math.random() * 6) + 1;

        System.out.printf("Triangle Side A: %.1f%n", sideA);
        System.out.printf("Triangle Side B: %.1f%n", sideB);
        System.out.printf("Pythagoras Hypotenuse C: %.1f%n", sideC);
        System.out.println("Random Simulated Dice Roll: " + diceRoll);
    }}
}}
💡 Hint / 提示

Math methods are accessed without imports. Notice that we perform type casting (int) on the random calculation to truncate the fractional part before adding 1.Math 方法无需导入即可访问。请注意,我们在随机数计算中执行了类型转换 (int),以便在加 1 之前截断小数部分。

✅ Show Expected Console Output / 显示预期控制台输出
Triangle Side A: 3.0
Triangle Side B: 4.0
Pythagoras Hypotenuse C: 5.0
Random Simulated Dice Roll: 4

Check Your Understanding自我检测

Q1. Which package containing basic classes like System and Math is auto-imported in Java?Java 中自动导入哪个包含 System 和 Math 等基础类的包?

Q2. Evaluate the output of statement: Math.pow(3, 2).评估语句输出结果:Math.pow(3, 2)

Q3. What is the range of values returned by Math.random() method?Math.random() 方法返回的数值范围是多少?