Skip to content
Computer ApplicationsICSE Class 9 · Mathematical Library Methods

Math.abs, max, min, random, the Exact Methods and Java Expressions (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

Math.abs(x) gives the absolute value, Math.max(a, b) and Math.min(a, b) give the larger and smaller of two values, and Math.random() gives a double from 0.0 up to but not including 1.0. The result type follows the argument types for abs, max and min. addExact, subtractExact and multiplyExact do int or long arithmetic but throw an ArithmeticException on overflow instead of silently wrapping around.

Scale the 0 to 1 value by n, cut off decimals with (int), then add the start value to shift the range.

Illustration drawn for QuestionBuddy.

Understand abs, max, min, random, the Exact Methods and Writing Java Expressions

abs, max and min. The result has the type of the arguments (int for ints, double for doubles; if the types differ, the larger type is used).

  • Math.abs(-7.5) is 7.5; Math.abs(-4) is 4.
  • Math.max(3, 8) is 8; Math.max(3, 8.0) is 8.0.
  • Math.min(-2, -9) is -9.

Combination: Math.max(Math.abs(-9), Math.min(4, 6)) is max(9, 4) = 9.

random. Math.random() returns a double greater than or equal to 0.0 and less than 1.0.

  • (int) (Math.random() * 6) + 1 gives a whole number from 1 to 6 (a die).
  • (int) (Math.random() * 10) gives 0 to 9.
  • (int) (Math.random() * 10) + 20 gives 20 to 29.
  • 10 + Math.random() * 5 gives a decimal from 10.0 up to, but not including, 15.0.

General rule: (int) (Math.random() * n) + start gives n values starting at start.

The exact methods. Math.addExact(a, b), Math.subtractExact(a, b) and Math.multiplyExact(a, b) work on int or long values. If the true result does not fit in the type, they throw an ArithmeticException (integer overflow). Ordinary arithmetic wraps around: with int a = 2147483647, a + 1 becomes -2147483648. The exact methods are used when a wrong silent answer would be worse than stopping.

Writing Java expressions. Convert formulas carefully.

  • Distance between (x1, y1) and (x2, y2): Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)).
  • Largest of three: Math.max(a, Math.max(b, c)).
  • Positive difference: Math.abs(a - b).
  • Use 1.0 or 2.0 in fractions to avoid integer division.

Neighbouring topics: pow, sqrt, cbrt, hypot and the rounding methods are on the previous page of this chapter.

abs, max, min, random, the Exact Methods and Writing Java Expressions: key facts

abs, max, min
Math.absx
Math.maxa, b
Math.mina, b
result type follows the arguments
Mixed types give the larger type.
Random range
  • Math.random() is in [0.0, 1.0)
  • (int)(Math.random() times n) + start
  • n values from start
Die: n = 6, start = 1.
Exact methods
  • addExact
  • subtractExact
  • multiplyExact
  • ArithmeticException on overflow
Plain + wraps around silently.

abs, max, min, random, the Exact Methods and Writing Java Expressions in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

A dice game

Suppose a board game program must roll a six-faced die and a second program must pick a lucky number from 20 to 29.

Write the Java expressions.

Try it first, then show the solutionSolution
  1. Die: (int)(Math.random() * 6) + 1.
  2. Lucky number: (int)(Math.random() * 10) + 20.

Answer: (int)(Math.random() * 6) + 1 and (int)(Math.random() * 10) + 20.

Memory hook: Multiply by the count, then add the start.

Tricks to remember abs, max, min, random, the Exact Methods and Writing Java Expressions

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Random is below one

    Math.random() is at least 0.0 and always less than 1.0.

    Use it when: Ranges.

  • Multiply by how many, add where it starts

    (int)(Math.random() * count) + start.

    Use it when: Random ranges.

  • Exact stops, plus wraps

    Exact methods throw an exception; + wraps silently.

    Use it when: Overflow.

abs, max, min, random, the Exact Methods and Writing Java Expressions: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Absolute value · 1 mark · MCQBoard level

What is Math.abs(-12)?
(a) -12 (b) 12 (c) 12.0 (d) 0

Try it first, then show the solutionSolution
  1. abs of an int is an int.

Answer: (b) 12

Example 2: Nested abs, max and min · 3 marks · Short answerHarder board level

Find the output:

System.out.println(Math.abs(-2.25) + " " + Math.max(3, 8) + " " + Math.min(-2, -9) + " " + Math.max(Math.abs(-9), Math.min(4, 6)));
Try it first, then show the solutionSolution
  1. abs(-2.25) = 2.25, max(3, 8) = 8, min(-2, -9) = -9. (1 mark)
  2. Inner: abs(-9) = 9 and min(4, 6) = 4, so max(9, 4) = 9. (1 mark)
  3. Output: 2.25 8 -9 9. (1 mark)

Answer: 2.25 8 -9 9

Example 3: Ranges of random expressions · 3 marks · Short answerHarder board level

State the possible values of: (i) (int) (Math.random() * 10); (ii) (int) (Math.random() * 10) + 5; (iii) 10 + Math.random() * 5.

Try it first, then show the solutionSolution
  1. (i) Math.random() * 10 is from 0.0 up to below 10.0, so the int is 0 to 9. (1 mark)
  2. (ii) Adding 5 gives 5 to 14. (1 mark)
  3. (iii) A decimal from 10.0 up to but not including 15.0. (1 mark)

Answer: (i) 0 to 9 (ii) 5 to 14 (iii) 10.0 up to, but not including, 15.0

Example 4: Case study: overflow · 4 marks · Case-basedHOTS / competency

A program uses int a = 2147483647;
(i) What is a + 1 with ordinary addition? (1)
(ii) What does Math.addExact(a, 1) do? (1)
(iii) What do 65536 * 65536 and Math.multiplyExact(65536, 65536) give? (2)

Try it first, then show the solutionSolution
  1. (i) The value wraps around to -2147483648.
  2. (ii) It throws an ArithmeticException (integer overflow) because the true result does not fit in an int.
  3. (iii) The product is 4294967296, which does not fit in an int. Ordinary multiplication wraps to 0; multiplyExact throws an ArithmeticException.

Answer: (i) -2147483648 (ii) ArithmeticException (iii) 0 and ArithmeticException

Example 5: Expressions for formulas · 4 marks · Short answerHarder board level

For the points (1, 2) and (4, 6), write a Java expression for their distance and find its value. Also write expressions for (i) the larger of a, b, c and (ii) the positive difference of a and b.

Try it first, then show the solutionSolution
  1. Distance: Math.sqrt(Math.pow(4 - 1, 2) + Math.pow(6 - 2, 2)). (1 mark)
  2. Value: pow(3, 2) = 9.0 and pow(4, 2) = 16.0, sum 25.0, sqrt = 5.0. (1 mark)
  3. Largest: Math.max(a, Math.max(b, c)). (1 mark)
  4. Positive difference: Math.abs(a - b). (1 mark)

Answer: Distance = 5.0; largest of three: Math.max(a, Math.max(b, c)); difference: Math.abs(a - b)

Where students lose marks in abs, max, min, random, the Exact Methods and Writing Java Expressions

The slips examiners see most often, and what to write instead.

  • Mistake: Random range off by one

    The count and start are confused.

    Wrong working

    (int)(Math.random() * 6) gives 1 to 6

    Right working

    (int)(Math.random() * 6) gives 0 to 5; add 1 for 1 to 6

    Do this: Multiply by count, then add start.

    Usually costs: 1 to 2 marks

  • Mistake: Omitting the cast on random

    A decimal is stored in an int.

    Wrong working

    int x = Math.random() * 6;

    Right working

    int x = (int) (Math.random() * 6);

    Do this: Cast before storing.

    Usually costs: 1 mark

  • Mistake: Wrong type for max

    Types are ignored.

    Wrong working

    Math.max(3, 8.0) = 8

    Right working

    Math.max(3, 8.0) = 8.0

    Do this: Mixed types give a double.

    Usually costs: 1 mark

Exam tips

  • Write the range of every random expression with its end points.
  • Trace nested calls from the innermost.
  • State what an exact method does on overflow.
  • Convert formulas with powers and roots using Math methods and brackets.

Practice questions on abs, max, min, random, the Exact Methods and Writing Java Expressions

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    The value of Math.min(5, -3) is:
    (a) 5 (b) -3 (c) 3 (d) 2

    Hint

    Smaller of the two.

    Answer

    (b) -3

  2. Q2 · 1 mark · Very short answerBoard level

    Which method returns a decimal number that is at least 0.0 and less than 1.0?

    Hint

    Think of the Math class.

    Answer

    Math.random()

  3. Q3 · 2 marks · Short answerHarder board level

    Evaluate Math.abs(-6) + Math.pow(2, 3).

    Hint

    An int plus a double is a double.

    Answer

    14.0 (6 + 8.0)

  4. Q4 · 3 marks · Short answerHarder board level

    Write a statement that stores a random whole number from 20 to 29 in an int variable r, and another that simulates the roll of a die.

    Hint

    (int)(Math.random() * count) + start.

    Answer

    int r = (int) (Math.random() * 10) + 20;
    int die = (int) (Math.random() * 6) + 1;

  5. Q5 · 3 marks · Short answerHOTS / competency

    A student writes int n = Math.addExact(2000000000, 2000000000); What happens? What would the plain sum 2000000000 + 2000000000 give?

    Hint

    The largest int is 2147483647.

    Answer

    The true sum 4000000000 is larger than the largest int (2147483647), so addExact throws an ArithmeticException (integer overflow). The plain sum wraps around and gives -294967296.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Math.abs(-9)?Show answer

    9

  2. 2.Can Math.random() return 1.0?Show answer

    No; it is less than 1.0.

  3. 3.Math.max(3, 8.0)?Show answer

    8.0

  4. 4.Which exception do the exact methods throw?Show answer

    ArithmeticException

  5. 5.Range of (int)(Math.random() * 6) + 1?Show answer

    1 to 6

  6. 6.Write the positive difference of a and b.Show answer

    Math.abs(a - b)

abs, max, min, random, the Exact Methods and Writing Java Expressions: frequently asked questions

Is Math.random() truly random?

It gives pseudo-random numbers, which are enough for school programs.

Do addExact and + give the same answer?

Yes when the result fits; only on overflow does addExact throw an exception while + wraps around.

Do these methods need an import?

No. The Math class is in java.lang.