Programming Companion
Java

Programming technique

Arithmetic, random numbers and conversion

Arithmetic expressions transform data. Random generation creates varied outcomes, while type conversion changes how a value is stored or interpreted.

Enabling programming skills: arithmetic, conversion and random generation support B2.1.1 variables and B2.3.1 sequence. They are not a separate named syllabus standard.

Arithmetic operators

OperatorPurposeExample
+Additionprice + tax
-Subtractionbalance - withdrawal
*Multiplicationhours * rate
/Divisiontotal / count
%Remaindernumber % 2
OperatorPurposeExample
+Additionprice + tax
-Subtractionbalance - withdrawal
*Multiplicationhours * rate
/Decimal divisiontotal / count
//Floor divisiontotal // count
%Remaindernumber % 2

Generate a random integer

Math.random() produces a decimal from 0.0 up to, but not including, 1.0. Scale it, convert it to an integer and then shift the range.

Import random and use random.randint(low, high) when both endpoints should be possible.

How the range changes

Math.random()

Produces a decimal from 0.0 up to, but not including, 1.0.

* sides

Stretches the range from 0.0 up to, but not including, the number of sides.

(int) ... + 1

Removes the decimal part, then shifts the final values to 1 through sides.

Define the inclusive range directly

import random

Makes Python's random-number functions available.

randint(1, sides)

Can return both 1 and sides, plus every integer between them.

Test the endpoints

Run the program repeatedly and check that every result stays inside the intended range.

Convert between types

Casting a decimal to an integer removes the fractional part; it does not round. Use Math.round() when rounding is the intended operation.

int() removes the fractional part of a positive decimal; it does not round. Use round() when rounding is the intended operation.

Use brackets

Make the intended order of operations clear rather than relying on memory.

Watch integer division

When both operands are integers, Java produces an integer result.

Choose the division operator

/ produces a decimal result; // deliberately performs floor division.

Define the random range

Write down the smallest and largest possible value before testing the code.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

RPG Dice

Challenge ID: PC-T04-C01 · Standards: B2.1.1, B2.3.1

Ask the user how many sides a die has. Generate and output a random whole number from 1 up to that number of sides. Test common RPG dice such as 4, 6, 8, 12 and 20 sides.

A collection of colourful role-playing dice with different numbers of sides.

Exactly Divisible

Challenge ID: PC-T04-C02 · Standards: B2.1.1, B2.3.1, B2.3.2

Ask the user for two integers. Output whether the first number is exactly divisible by the second. When it is not, output the remainder. Prevent division by zero with a clear error message.

Month and Leap Year

Challenge ID: PC-T04-C03 · Standards: B2.1.1, B2.3.1, B2.3.2

Ask for a month number from 1 to 12 and a year. Output the month name and number of days. February has 29 days in a leap year. Use the full leap-year rule: divisible by 400, or divisible by 4 but not by 100.

A calendar showing February changing between 28 and 29 days for leap years.

Balanced Dice Game

Challenge ID: PC-T04-C04 · Standards: B2.1.1, B2.3.1, B2.3.2

Design a short dice game that uses at least two random values, arithmetic scoring and at least one special outcome. Explain the probability of each special outcome and run enough test games to decide whether the scoring appears balanced.