Your first real steps into using pre-built functionality (Math functions) and Object-Oriented Programming (instantiating objects from existing classes).
Common mathematical functions and their typical use in Java (via the Math class):
| Purpose | Java example |
|---|---|
| Minimum/maximum of two values | Math.min(a, b), Math.max(a, b) |
| Power/exponent | Math.pow(base, exponent) |
| Absolute value | Math.abs(x) |
| Square root | Math.sqrt(x) |
| Rounding to whole number | Math.round(x) |
| Random number | Math.random() โ returns a double between 0.0 and 1.0 |
Integer arithmetic โ calculating a dividend and remainder together, e.g. splitting seconds into minutes and remaining seconds:
Example
int totalSeconds = 125; int minutes = totalSeconds / 60; // dividend: 2 int remainingSeconds = totalSeconds % 60; // remainder: 5
Working with objects from existing classes:
๐ก Exam Tip
A method call that returns a value can be used directly in an expression, e.g. `int result = Math.max(x, y) + 5;` โ a void method cannot, since it has no value to plug into the expression.