Programming Companion
Java

Programming technique

Variables and input

Variables give names to values so that a program can store, change and reuse data. Input allows a user to supply those values while the program is running.

IB DP CS standard B2.1.1: Construct and trace programs using global and local variables of Boolean, char, decimal, integer and string data types.

Choose an appropriate data type

Java typeStoresExample
intWhole numbers42
doubleDecimal numbers3.75
StringText"Lina"
charOne character'A'
booleantrue or falsetrue
Python typeStoresExample
intWhole numbers42
floatDecimal numbers3.75
strText"Lina"
strOne character when its length is 1"A"
boolTrue or FalseTrue

Read input with Scanner

Create one Scanner and reuse it. Use nextLine() for a complete line of text, nextInt() for an integer and nextDouble() for a decimal.

Read and convert input

input() always returns a string. Wrap it with int() for a whole number or float() for a decimal when the program needs numeric data.

Common Scanner issue: after nextInt() or nextDouble(), the newline may still be waiting. Call nextLine() once before reading the next full line of text.
Common input issue: input() returns text even when the user types digits. Convert before doing arithmetic, and be ready to handle text that cannot be converted.

Trace variable values

When tracing, record each variable after every statement that changes it. Do not record what you expect the variable to become later.

Statementlengthwidthheightvolume
double length = 40;40
double width = 25;4025
double height = 30;402530
double volume = length * width * height;40253030000
Statementlengthwidthheightvolume
length = 40.040.0
width = 25.040.025.0
height = 30.040.025.030.0
volume = length * width * height40.025.030.030000.0

Declare before use

A variable must have a type and name before the program can use it.

Assign before use

A Python variable is created when a value is assigned, but it still must exist before it is read.

Use meaningful names

tankVolume or tank_volume communicates more than x.

Integer division

5 / 9 is integer division and produces 0. Use 5.0 / 9.0 when a decimal answer is required.

Division operators

5 / 9 produces a decimal result. Use // only when floor division is intentionally required.

Challenges Choose one

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

Simple Adder

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

Ask the user for two whole numbers. Output both numbers and their total in a complete sentence.

Two number inputs combining to produce a total in a simple addition program.

Temperature Converter

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

Ask the user for a temperature in degrees Fahrenheit and display the equivalent temperature in degrees Celsius. Use Celsius = (Fahrenheit - 32) * 5.0 / 9.0. Test a value below freezing, freezing point and a warm value.

A thermometer showing conversion between Fahrenheit and Celsius.

Fish Tank Volume

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

Ask the user for the length, depth and height of a fish tank in centimetres. Calculate the volume in litres by multiplying the three dimensions and dividing by 1000. Also display the approximate volume in UK gallons using 1 UK gallon = 4.54609 litres.

A rectangular fish tank with length, width and height marked.

Circle Properties

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

Ask the user for the diameter of a circle and an arc angle. Output the radius, area, circumference and arc length. Use Math.PI and show clearly which input and calculated value each line represents.

A labelled circle showing circumference, radius, diameter and an arc.