AP Computer Science A · Unit 1 lecture · Part 7 of 11 · 9 min

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

Mr. Thompkins
Some methods don't need an object at all. You just ask the class. The Math class is the one you'll use most, and it shows up on almost every exam, usually in a question about random numbers.

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?
static marks a class method, one that belongs to the class rather than to a particular object. You call it on the class name, like Math.abs(-3), and you don't need to create an object with new first.

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);
  1. line 1-2 abs gives back the same type it gets
  2. line 3-4 pow and sqrt always return a double
  3. line 5 sqrt(50) is 7.07..., and the cast chops it
  4. line 6 A call can be an argument to another call
output › 7 2.5 1024.0 9.0 7 5.0
The Math methods, run under Java 17. Line 6 is the distance formula: the hypotenuse of a 3-4-5 triangle.
?Wait, what's a square root, and what's an exponent?
The square root of x is the number that, multiplied by itself, gives x: the square root of 81 is 9, because 9 × 9 = 81. An exponent says how many times to multiply a base by itself: 2 to the 10th is 2 × 2 × ... × 2, ten times, which is 1024.

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.

Math.random(): from 0.0, never 1.0times 8: from 0.0, never 8.0cast to int: 0, 1, 2, ..., 7plus 5: 5, 6, 7, ..., 12015812
Building a random int from 5 to 12, inclusive: 8 possible values. The open circles mean "gets close, never reaches."
Pause and think
What values can (int) (Math.random() * 10) produce? And what about (int) Math.random() * 10?
I thought about it. Show me
The first gives the whole numbers 0 through 9. The second always gives 0: the cast applies only to Math.random(), which is less than 1.0, so it truncates to 0 every time, and 0 * 10 is 0. Where the parentheses go is the whole question.
Build a random range
A game needs a random whole number from 5 to 12, inclusive. (a) Write the expression. (b) A student writes (int) Math.random() * 8 + 5. What values does it produce? (c) Another writes Math.random() * 8 + 5. What's wrong with it?
  1. (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.
  2. (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.
  3. (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
  1. How many different whole numbers are there from 5 to 12? Count them.
  2. Multiply by that count, then cast the whole product.
  3. Then add the smallest value you want.
Check yourself
Which values can n hold?
int n = (int) (Math.random() * 4) + 3;
  1. A3, 4, 5 and 6
  2. B3, 4, 5, 6 and 7
  3. C0, 1, 2 and 3
  4. 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.

Check yourself
Which statement compiles?
  1. Aint a = Math.sqrt(49);
  2. Bint b = Math.pow(2, 3);
  3. Cdouble c = Math.abs(-5);
  4. 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.

Exam tip from Mr. T

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.