Code Companion
Java

Programming technique · B2.1.1

Variables and input

Variables give names to values so that a program can store, change and reuse data. Their data type controls the kind of value represented, while scope controls where each name is available.

IB DP CS standard B2.1.1: Construct and trace programs using a range of 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.

Local and wider scope

Java has no free-standing global variables. In a simple Java program, a class-level field can provide wider scope, while a variable declared inside a method is local to that method or block. Prefer local variables unless the value genuinely needs wider availability.

A name assigned at the top level of a Python file has module-level scope. A name assigned inside a function is normally local to that function. Prefer local variables unless a value genuinely belongs at wider scope.

Read this structure; do not build it yet: the method/function is supplied only to make local scope visible. Functions and modularisation are taught formally later. At this point, trace which names are available in each scope and explain why a value belongs locally or more widely.

Check your understanding

Answer each question before opening the model answer.

  1. Which value should normally have wider scope: a tax rate shared by several calculations, or a temporary tax amount for one item?

    Reveal model answer

    The shared tax rate may justify wider scope; the temporary tax amount belongs locally inside the calculation that uses it.

  2. Can code in main directly read a local variable declared inside another method or function after that call finishes?

    Reveal model answer

    No. A local name is only available within its own scope; the function or method must return a value if the caller needs the result.

  3. Why can wider-scope variables make programs harder to reason about?

    Reveal model answer

    More parts of the program can depend on or change the same state, making it harder to trace where a value came from or why it changed.

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.

Statementcapacitysoldremainingstatus
int capacity = 120;120
int sold = 37;12037
int remaining = capacity - sold;1203783
String status = "Remaining: " + remaining;1203783Remaining: 83
Statementcapacitysoldremainingstatus
capacity = 120120
sold = 3712037
remaining = capacity - sold1203783
status = f"Remaining: {remaining}"1203783Remaining: 83

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

remaining 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.

Scope Snapshot

Challenge ID: PC-T02-C05 · Standards: B2.1.1

Use the supplied Scope example on this page rather than designing new methods. Before running it, make a scope table showing where TAX_RATE, price, tax and itemPrice are available. Change only the numeric input values, run the program, and explain why tax should remain local while TAX_RATE may reasonably have wider scope. Do not add new methods for this challenge; method construction is taught later.