OOP Method Patterns

IB Syllabus: B3.1, B3.2 – Common method patterns for constructing and tracing OOP programs in exam contexts.

Table of Contents

  1. Why This Page Exists
  2. The Student Class (Used Throughout)
  3. Pattern 1: Filter and Display
  4. Pattern 2: Count Matching Items
  5. Pattern 3: Accumulate and Return
  6. Pattern 4: Find by ID / Find by Name
  7. Pattern 5: Find Maximum / Minimum
  8. Pattern 6: Nested Traversal (Two Collections)
  9. Pattern 7: Remove from Collection
  10. Pattern Summary Table
  11. Quick Check
  12. Trace Exercise
  13. Spot the Error
  14. Predict the Output
  15. Practice Exercises
    1. Core
    2. Extension
    3. Challenge
  16. Connections

Why This Page Exists

IB Paper 2 OOP questions follow predictable patterns. The class definitions change, but the types of methods asked for repeat every session. If you can recognise the pattern, you can write the method quickly and confidently.

This page catalogues the most common patterns, each with a template and a worked example using a consistent Student class.


The Student Class (Used Throughout)

All examples on this page use this class:

public class Student {
    private String name;
    private int grade;
    private String house;

    public Student(String name, int grade, String house) {
        this.name = name;
        this.grade = grade;
        this.house = house;
    }

    public String getName() { return name; }
    public int getGrade() { return grade; }
    public String getHouse() { return house; }

    public void setGrade(int grade) { this.grade = grade; }
}

And this collection class that manages an array of students:

public class School {
    private Student[] students;
    private int count;

    public School(int maxSize) {
        students = new Student[maxSize];
        count = 0;
    }

    public void addStudent(Student s) {
        if (count < students.length) {
            students[count] = s;
            count++;
        }
    }

    public int getCount() { return count; }
    // ... methods below go here
}

Pattern 1: Filter and Display

What it does: Loop through the array, test a condition, print matching items.

Template:

public void displayByCondition(ConditionType value) {
    for (int i = 0; i < count; i++) {
        if (students[i].getField().equals(value)) {  // or == for int
            System.out.println(students[i].getName());
        }
    }
}

Example – print all students in a given house:

public void displayByHouse(String house) {
    for (int i = 0; i < count; i++) {
        if (students[i].getHouse().equals(house)) {
            System.out.println(students[i].getName() + " - Grade " +
                students[i].getGrade());
        }
    }
}

Always use .equals() for String comparisons, never ==. Use == only for int, double, boolean, char.


Pattern 2: Count Matching Items

What it does: Loop through, test a condition, increment a counter, return it.

Template:

public int countByCondition(ConditionType value) {
    int total = 0;
    for (int i = 0; i < count; i++) {
        if (students[i].getField().equals(value)) {
            total++;
        }
    }
    return total;
}

Example – count students with grade above a threshold:

public int countAboveGrade(int threshold) {
    int total = 0;
    for (int i = 0; i < count; i++) {
        if (students[i].getGrade() > threshold) {
            total++;
        }
    }
    return total;
}

Pattern 3: Accumulate and Return

What it does: Loop through, accumulate a numeric value (sum, average, etc.), return it.

Template:

public double calculateAggregate() {
    double sum = 0;
    for (int i = 0; i < count; i++) {
        sum = sum + students[i].getNumericField();
    }
    return sum / count;  // for average; just sum for total
}

Example – calculate average grade:

public double getAverageGrade() {
    if (count == 0) return 0;
    double sum = 0;
    for (int i = 0; i < count; i++) {
        sum = sum + students[i].getGrade();
    }
    return sum / count;
}

Pattern 4: Find by ID / Find by Name

What it does: Loop through, compare an identifier, return the matching object (or null if not found).

Template:

public Student findByName(String name) {
    for (int i = 0; i < count; i++) {
        if (students[i].getName().equals(name)) {
            return students[i];
        }
    }
    return null;  // not found
}

The return type is the object type (e.g., Student), not String or int. Returning the whole object allows the caller to access any attribute. Always return null if no match is found – this is the standard IB convention.

Using the result safely:

Student s = school.findByName("Alice");
if (s != null) {
    System.out.println(s.getName() + ": " + s.getGrade());
} else {
    System.out.println("Student not found");
}

Pattern 5: Find Maximum / Minimum

What it does: Track the “best so far” object while looping, return it at the end.

Template:

public Student findHighestGrade() {
    if (count == 0) return null;
    Student best = students[0];
    for (int i = 1; i < count; i++) {
        if (students[i].getGrade() > best.getGrade()) {
            best = students[i];
        }
    }
    return best;
}

Key points:

  • Start with the first element as the initial “best”
  • Loop from index 1 (not 0) since index 0 is already the initial best
  • Compare using getters, not direct field access (fields are private)
  • Return the object, not just the value

Pattern 6: Nested Traversal (Two Collections)

What it does: For each item in one collection, check or process items in another.

Example – find students enrolled in two courses:

public class Course {
    private String name;
    private Student[] enrolled;
    private int count;

    // constructor, addStudent, etc.

    public boolean hasStudent(String studentName) {
        for (int i = 0; i < count; i++) {
            if (enrolled[i].getName().equals(studentName)) {
                return true;
            }
        }
        return false;
    }
}
// Find students in BOTH courses
public static void findCommon(Course c1, Course c2, Student[] allStudents, int total) {
    for (int i = 0; i < total; i++) {
        String name = allStudents[i].getName();
        if (c1.hasStudent(name) && c2.hasStudent(name)) {
            System.out.println(name + " is in both courses");
        }
    }
}

Pattern 7: Remove from Collection

What it does: Find an item, shift remaining elements left, decrement count.

public boolean removeStudent(String name) {
    for (int i = 0; i < count; i++) {
        if (students[i].getName().equals(name)) {
            // Shift everything after this position left by one
            for (int j = i; j < count - 1; j++) {
                students[j] = students[j + 1];
            }
            students[count - 1] = null;
            count--;
            return true;
        }
    }
    return false;  // not found
}

When removing from an array, you must shift elements left to fill the gap. Set the last position to null and decrement count. Return boolean to indicate success or failure.


Pattern Summary Table

Pattern Returns Key Feature Null check needed?
Filter and display void Print inside loop No
Count matching int Counter variable No
Accumulate double or int Running sum Check count > 0
Find by ID Object or null Return inside loop Yes (caller checks)
Find max/min Object or null Track “best so far” Check count > 0
Nested traversal varies Loop inside loop Depends on context
Remove boolean Shift elements left No

Quick Check

Q1. A findByName() method searches an array for a matching Student. What should it return if no match is found?

Q2. In a "find maximum" method, what should the loop start index be?

Q3. After removing element at index 2 from an array of 5 items, what must happen to elements at indices 3 and 4?

Q4. A "filter and display" method has return type:

Q5. Why must you use .equals() instead of == when comparing student names?


Trace Exercise

Trace the findHighestGrade() method on this array.

Trace: Find Maximum Pattern

// students array:
// [0] Alice, grade 85, "Red"
// [1] Bob, grade 92, "Blue"
// [2] Charlie, grade 78, "Red"
// [3] Diana, grade 95, "Blue"

Student best = students[0]; // Alice (85)
// Loop from i=1 to i=3
istudents[i]Gradebest.getGrade()Update best?
1 Bob 92 85
2 Charlie 78
3 Diana 95

Final best: with grade


Spot the Error

This countAboveGrade method has a bug that causes it to always return 0.

Bug Hunt: A method that should count students with grades above a threshold, but the count resets every iteration.

1public int countAboveGrade(int threshold) { 2 for (int i = 0; i < count; i++) { 3 int total = 0; 4 if (students[i].getGrade() > threshold) { 5 total++; 6 } 7 } 8 return total; 9}

Pick the correct fix for line 3:


Predict the Output

Given three students: Alice (grade 85), Bob (grade 92), Charlie (grade 78). Using the corrected countAboveGrade method, what does this print?

System.out.println(school.countAboveGrade(80));

Practice Exercises

Core

  1. Display by house – Using the School class, write a displayByHouse(String house) method that prints the name and grade of every student in the given house.

  2. Calculate average – Write a getAverageGrade() method that returns the average grade of all students. Handle the case where count is 0.

Extension

  1. Find and update – Write a method updateGrade(String name, int newGrade) that finds a student by name and updates their grade. Return true if successful, false if the student was not found.

  2. Top N students – Write a method displayTopStudents(int n) that prints the names of the n students with the highest grades. You may use the find-maximum pattern repeatedly, marking each found student to avoid duplicates.

Challenge

  1. House report – Write a method houseReport() that prints each house name, the number of students in that house, and their average grade. You do not know the house names in advance – discover them from the data.

Connections


© EduCS.me — A resource hub for IB Computer Science

This site uses Just the Docs, a documentation theme for Jekyll.