Skip to content

c. Practical Examples

Modular Programming, OOP Principles and Data Structures

These examples use a light I do - We do - You do model. Students should attempt the reasoning first, then compare with the hidden model response.


Example 1 - I do: Choosing a data structure

Scenario

A program needs to store:

  • the names of eight clubs
  • one student's first name, year level and house

Try first

Choose the most suitable data structure for each situation.

Teacher walkthrough

A strong response could use:

  • Array or list for the club names, because this is an ordered collection of similar items
  • Record for the student details, because the fields are related but may use different data types

Focus: The best structure depends on whether the data is a collection of similar values or a grouped set of related fields.


Example 2 - We do: Turning repeated code into a function

Starting idea

A programmer calculates total price in several places using the same formula.

Try first

Decide:

  1. should this repeated logic stay copied in multiple places, or be moved into one named block?
  2. would it suit a function or a procedure?
One possible model response

The repeated logic should be moved into one named block to improve reuse and reduce mistakes.

It would usually suit a function if the code needs to return a calculated total.

Example function idea
double CalculateTotal(double price, int quantity)
{
    return price * quantity;
}

Focus: Modular programming helps reduce duplication and makes code easier to maintain.


Example 3 - We do: Thinking about abstraction and encapsulation

Scenario

A BankAccount class stores a balance. Another programmer wants to let every part of the program change the balance directly at any time.

Try first

Decide:

  1. which OOP principle is most relevant here
  2. why direct unrestricted access could be a problem
One possible model response
  • Encapsulation is the key principle here.
  • Direct unrestricted access could allow unsafe or invalid changes to the balance.
  • A better approach is to let the class control balance changes through methods such as deposit() or withdraw().
Where abstraction fits

The user of the class does not need to know every internal detail of how balance checking works. They only need a simple interface for safe actions.

Focus: Encapsulation protects data; abstraction hides unnecessary complexity.


Example 4 - You do: Class or object?

Scenario

A programmer writes:

  • class Student
  • Student student1 = new Student();

Your task

State:

  1. which one is the class
  2. which one is the object
  3. what the relationship is between them

Retrieval Prompt

Try to explain this without using the word "thing". Use terms like template, instance, data and method.


Example 5 - You do: Retrieval grid

Complete the table from memory.

Term What does it mean? Example use
Array
Record
Function
Procedure
Method
Abstraction
Encapsulation

Study Tip

Retrieval is stronger when you explain how the idea would help in a real program, not just repeat the definition.