4.4.1.1–4.4.1.2 - Solving problems and tracing algorithms (Java)

4.4.1.1–4.4.1.2 - Solving problems and tracing algorithms (Java)

Turn a precise problem into a terminating algorithm, trace its state changes and translate it into Java. Then use reasoning, tests and user feedback to check that the solution meets the original need. This Java route is incomplete: runnable support pending; its source questions are written practice.

Problems and algorithms

A computational solution begins with a clear statement of the inputs, required outputs and rules connecting them.

Algorithm

A sequence of steps that can be followed to complete a task and that always terminates.

An instruction such as “find a good timetable” is too vague: define what counts as good and how to recognise completion. Suppose Ana, Ben and Cora must occupy three different presentation slots. Ana must be before Cora and Ben cannot be first. Enumerate the six orders and reject those that break a rule: ABC and ACB remain. Checking each candidate against both rules verifies these solutions; finding one permitted order does not prove that it is the only one.

For a programming problem, suppose an array contains recorded ages and the required output is the number between 16 and 18 inclusive. The input may be empty; the answer is then zero. “Inclusive” means both boundary ages count. One solution starts a counter at zero, examines every age once, increments the counter only when both conditions hold and returns it. The input length is finite and the loop advances to its end, so the algorithm terminates. A loop whose condition never becomes false would not meet this definition of an algorithm.

Pseudocode and traces

Pseudocode expresses the algorithm without depending on one programming language's syntax, while a trace records what each executed step actually changes.

In the convention below, ← means assignment, arrays start at index zero, LENGTH gives the number of elements, AND requires both conditions and the upper bound of FOR is inclusive. An empty index range performs no iterations. These conventions are supplied for this example; they are not a claim that AQA 7517 requires one exact token vocabulary.

Pseudocode
count ← 0
FOR i ← 0 TO LENGTH(ages) - 1
    IF ages[i] >= 16 AND ages[i] <= 18 THEN
        count ← count + 1
    ENDIF
ENDFOR
RETURN count

Sequence orders the steps. Assignment replaces the old value of count with the computed new value. Selection chooses whether to increment it. Iteration repeats the body for successive elements. Assignment is not an equality assertion: count ← count + 1 is a state change.

For ages [15, 16, 19, 18], record the state after each complete loop body:

Index iages[i]Conditioncount
initial——0
015false0
116true1
219false1
318true2

The returned value is 2. Do not increment a counter just because the trace has advanced one row. If an assignment is inside a conditional branch, it executes only when that branch is taken. A flowchart could express the same logic using a decision diamond with true/false arrows and a loop back to the next-element test; its paths must preserve the same order and stop condition.

Translating into Java

Translation preserves the algorithm's meaning while using Java's types, array bounds and control structures.

Java
public class Main {
    static int countEligible(int[] ages) {
        int count = 0;
        for (int i = 0; i < ages.length; i++) {
            if (ages[i] >= 16 && ages[i] <= 18) {
                count = count + 1;
            }
        }
        return count;
    }
}

The pseudocode's final index is LENGTH(ages) - 1; Java's condition i < ages.length includes that index without accessing ages[ages.length]. Java uses = for assignment and && for logical AND. The return is outside the loop so every element is considered. Returning from inside the first iteration would ignore later ages.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account