24su ver.
Note: this is for the Summer 2024 iteration of CSE 121. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse121/.
Math Class¶
(a set of useful methods for performing mathematical operations)
Method | Returns |
---|---|
Math.abs(value) | absolute value (make nonnegative) |
Math.max(value1, value2) | larger of two values |
Math.min(value1, value2) | smaller of two values |
Math.pow(base, exp) | base to the exp power |
Math.sqrt(num) | square root of num |
Example
// Evaluates to 8.0
double num = Math.pow(2, 3);
// Evaluates to 9.0
double big = Math.max(num, Math.sqrt(81));
System.out.println(big + " is big!");
Output
9.0 is big!
Returns¶
Declaration
public static type name(...) {
...
return expression;
}
Example
public static void main(String[] args) {
double seattleC = fToC(60.0);
double newYorkC = fToC(50.0);
System.out.println(seattleC);
System.out.println(newYorkC);
}
// Converts Fahrenheit to Celsius.
public static double fToC(double degreesF) {
double degreesC = 5.0 / 9.0 * (degreesF - 32);
return degreesC;
}
Output
15.555555555555557
10.0