1.10 Calling class methods · 1.11 The Math classMethods that belong to the class

A class method (written with the keyword static in its header) belongs to the class itself, not to any one object. You call it with the class name, a dot, and the method name: Math.sqrt(25), Shop.price(3). Inside the class that defines it, you can call it with just its name. Methods without static are instance methods, which are called on an object. You'll meet them in 1.14.
?Wait, what did static mean in a method header?
The Math class
The Java Quick Reference gives you these Math methods, and they're all class methods.
Math.abs(x) returns the absolute value of x. It's overloaded: give it an int and it returns an int, give it a double and it returns a double. Math.pow(base, exponent) returns base raised to the exponent, as a double. Math.sqrt(x) returns the square root of x, as a double. Math.random() returns a double that is greater than or equal to 0.0 and less than 1.0.
Notice that pow and sqrt always return a double, even when the answer is whole. Math.sqrt(81) is 9.0, so int r = Math.sqrt(81); doesn't compile. You'd need int r = (int) Math.sqrt(81);.
1System.out.println(Math.abs(-7));12System.out.println(Math.abs(-2.5));13System.out.println(Math.pow(2, 10));24System.out.println(Math.sqrt(81));25System.out.println((int) Math.sqrt(50));36double h = Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2));47System.out.println(h);
- line 1-2 abs gives back the same type it gets
- line 3-4 pow and sqrt always return a double
- line 5 sqrt(50) is 7.07..., and the cast chops it
- line 6 A call can be an argument to another call
?Wait, what's a square root, and what's an exponent?
Random whole numbers
Math.random() gives a decimal from 0.0 up to, but never including, 1.0. To get a random whole number in a range, you scale it, cast it, and shift it. Multiply by how many different values you want, cast to int to chop off the decimals, then add the smallest value you want.
So for a whole number from min to max, inclusive, there are max - min + 1 possible values, and the expression is (int) (Math.random() * (max - min + 1)) + min. For a die roll, 1 to 6, that's (int) (Math.random() * 6) + 1.
I thought about it. Show me
- (a) Count the values: 12 - 5 + 1 = 8. Scale by 8, cast, then shift by 5: (int) (Math.random() * 8) + 5.
Why?
Math.random() * 8 is from 0.0 up to just under 8.0. Truncating gives 0 to 7. Adding 5 gives 5 to 12. - (b) The cast hits Math.random() alone, which is always below 1.0, so it truncates to 0. Then 0 * 8 + 5 is 5. It always produces 5.
Why?
A cast binds to the value right after it. Here that's the method call, not the product. - (c) There's no cast, so it's a double from 5.0 up to just under 13.0, like 9.27. It isn't a whole number, and it can't be stored in an int without a cast.
Answer: (a) (int) (Math.random() * 8) + 5. (b) Always 5. (c) It is a double from 5.0 up to (not including) 13.0, not a whole number.
Stuck? Get a hint
- How many different whole numbers are there from 5 to 12? Count them.
- Multiply by that count, then cast the whole product.
- Then add the smallest value you want.
int n = (int) (Math.random() * 4) + 3;
- A3, 4, 5 and 6
- B3, 4, 5, 6 and 7
- C0, 1, 2 and 3
- D4, 5, 6 and 7
Show the answer
A. Math.random() * 4 is from 0.0 up to just under 4.0. The cast gives 0, 1, 2 or 3, and adding 3 gives 3 through 6. Choice B is the trap: it assumes Math.random() can reach 1.0, but it never does, so the cast never produces 4.
- Aint a = Math.sqrt(49);
- Bint b = Math.pow(2, 3);
- Cdouble c = Math.abs(-5);
- Dint d = Math.random() * 10;
Show the answer
C. Math.abs(-5) returns the int 5, and an int can be stored in a double (it becomes 5.0). sqrt, pow and random all return doubles, so A, B and D try to store a double in an int without a cast. Choice A is the most common trap, because 7 looks like a perfectly good int.

Random-range questions are free points if you count first. Count the values (max - min + 1), multiply by that count, cast the whole product, then add the smallest value. And check where the cast's parentheses are before anything else.
- Class (static) methods are called on the class name: Math.sqrt(x).
- Math.abs returns the same type it's given. Math.pow and Math.sqrt return a double.
- Math.random() returns a double with 0.0 <= value < 1.0.
- Random int from min to max inclusive: (int) (Math.random() * (max - min + 1)) + min.