Skip to content

The Math Class

Introduced
Handout
Video lesson for this topic Open the video page
15:05

Using Objects and Methods

Here are two Java programs. The first is missing a semicolon. The second divides seven by two and stores the answer in a whole number. The compiler stops the…

English narration · English and 中文 subtitles you can switch

In this lesson

Video lesson

Trace a random range at both ends

  • Tickets need integer labels from 4 through 9 inclusive. Use (int)(Math.random() * 6) + 4.
  • Scaling gives values from 0 up to but not including 6; truncating gives 0 through 5; shifting gives 4 through 9.
Trace a random range at both ends: (int)(Math.random() * 6) + 4 scales to values from 0 up to but not including 6, truncates to 0 through 5, and shifts to 4 through 9; casting first, (int)Math.random() * 6 + 4, always gives 4

Handout

Log in or create account

IGCSE, A-Level & AP