Ch 2 · 20 Further Programming · AS & A Level Computer Science 9618
Topic 2 of 2 in Computational Thinking and Further Programming — AS & A Level Computer Science 9618 — 79 lessons.
Introduction
A programming paradigm is a style or approach to programming, with its own way of structuring code and its own features. This lesson covers four paradigms (low-level, imperative, object-oriented and declarative), then file processing with records and exception handling: the skills tested in the practical Paper 4. (Syllabus topic 20: sections 20.1–20.2.)
Key ideas — 20.1 (a) Low-level programming
Low-level programming uses instructions that correspond closely to the processor's machine code (assembly language). The programmer works directly with registers and memory addresses. It gives full control and efficient code, but is hard to write and is specific to one processor.
🧑🏫 Teacher's explanation
Imagine you are a librarian organizing a massive collection of books. High-level programming is like telling a robot: "Go get the Harry Potter book." You don't care where it is; you just want the book. Low-level programming is like giving the robot exact coordinates: "Go to Aisle 4, Shelf 2, Position 12, and pick up the object there." You have total control, but if the library rearranges the shelves, your instructions break.
📖 Definitions
🔍 Worked example — Easy
A processor uses direct addressing. If the instruction is LDD 500, and the memory contents at address 500 is 42, what value is loaded into the accumulator?
🔍 Worked example — Standard
Calculate the effective address using indexed addressing. The base address is 1000, and the Index Register (IX) contains 25.
🔍 Worked example — Exam-style
An instruction LDI 200 is executed. The memory contents are: Address 200 contains 350. Address 350 contains 12. What value is loaded into the accumulator?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
Address 500 arrow 800 Address 800 arrow 12
Key ideas — 20.1 (b) Imperative (procedural) programming
In imperative programming, the program is a sequence of statements (commands) that change the program's state (the values of variables), using the constructs sequence, selection and iteration. Procedural programming structures the program into procedures and functions (see AS topic 11). Examples: Python, Pascal, C, Visual Basic used procedurally. It is the style of all the pseudocode at AS.
🧑🏫 Teacher's explanation
Imagine you are following a recipe to bake a cake. The recipe is a list of step-by-step instructions: "Crack two eggs," "Mix the flour," "Bake for 30 minutes." You are telling the computer exactly *how* to do something, step by step. This is the essence of imperative programming.
📖 Definitions
🔍 Worked example — Easy
Identify the paradigm: A program consists of a sequence of commands that modify variable values using loops and if-statements. Is this Imperative or Declarative?
🔍 Worked example — Standard
Trace the state change: DECLARE x : INTEGER DECLARE y : INTEGER ASSIGN x <- 10 ASSIGN y <- 5 IF x > y THEN x <- x - y ENDIF OUTPUT x
🔍 Worked example — Exam-style
Write a procedural algorithm in pseudocode to calculate the sum of integers from 1 to n using a function.
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
a) Identify the programming paradigm used here. [1] b) Explain how the 'state' of the program changes during the execution of the loop. [2] [3 marks]
Key ideas — 20.1 (c) Object-oriented programming: terms
Designing classes: identify the objects in the problem (nouns), their data (attributes) and what they do (methods); look for "is a" relationships (inheritance) and "has a" relationships (containment).
🧑🏫 Teacher's explanation
Imagine you are an architect. Before you build a house, you draw a blueprint. This blueprint isn't a house itself; it's just a set of instructions that says every house built from it must have 3 bedrooms, 2 bathrooms, and a front door. In programming, this blueprint is the class. When you actually build a house using that blueprint, that physical house is the object (or instance).
📖 Definitions
🔍 Worked example — Easy
Identify the relationship: A 'Car' class has an attribute 'Engine' which is itself an object of an 'Engine' class. Which OOP concept is this?
🔍 Worked example — Standard
A class Animal has a method makeSound(). A subclass Dog inherits from Animal and defines its own makeSound() to output 'Bark'. A subclass Cat also inherits from Animal and defines its own makeSound() to output 'Meow'. What is the term for the ability of these different objects to respond to the same method call in different ways?
🔍 Worked example — Exam-style
A class BankAccount has a private attribute balance. To ensure the balance never becomes negative, the programmer provides a method setBalance(newBalance). If newBalance < 0, the method rejects the change. What is the purpose of this design pattern?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
(a) The Animal class has a method makeSound(). The Lion class overrides this method to output 'Roar', and the Penguin class overrides it to output 'Honk'. State the OOP term that describes this ability. [1]
Key ideas — 20.1 (c) Object-oriented programming: classes in code
Both pets respond to describe(), but each in its own way: that is polymorphism.
🧑🏫 Teacher's explanation
Imagine you are designing a blueprint for a house. The blueprint itself isn't a house; it is just a set of instructions describing what a house should have (windows, doors, roof) and what it can do (open door, turn on lights). In Object-Oriented Programming (OOP), this blueprint is called a 'Class'. When you actually build a house using that blueprint, the physical house is an 'Instance' or an 'Object'.
📖 Definitions
🔍 Worked example — Easy
A class Car has private attributes Make and Model. Write a pseudocode constructor (NEW) and a getter for Make.
🔍 Worked example — Standard
A class Shape has a method Area(). A subclass Square inherits from Shape and adds a private attribute Side. Write the pseudocode for the Square class including the constructor and the overridden Area method.
🔍 Worked example — Exam-style
Trace the following code and output the final result:
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
(a) State the purpose of using PRIVATE for the attributes in the Media class. [1] (b) Write pseudocode for the Book class, including the constructor and a method GetDetails() that returns a string containing the Title, ID, and Author. [4] [5 marks]
Key ideas — 20.1 (d) Declarative programming
In declarative programming, the programmer states what the problem is (as facts and rules), not how to solve it. The language (e.g. Prolog) searches the facts and rules to satisfy a goal (query), using backtracking to find every solution.
🧑🏫 Teacher's explanation
Imagine you are at a restaurant. In imperative programming, you would give the chef a detailed recipe: "Take 500g of flour, add two eggs, mix for 5 minutes, bake at 180 degrees." You are telling the computer exactly how to do the work. In declarative programming, you simply say: "I want a chocolate cake." You describe what you want, and you trust the kitchen (the language engine) to know the recipe and the steps to make it happen.
📖 Definitions
🔍 Worked example — Easy
Given the following facts: bird(tweety).\ bird(pingu).\ penguin(pingu).\
🔍 Worked example — Standard
Consider the following knowledge base: likes(alice, pizza).\ likes(bob, pizza).\ likes(alice, sushi).\ likes(charlie, sushi).\
🔍 Worked example — Exam-style
A knowledge base contains: parent(a, b).\ parent(b, c).\ parent(a, d).\ parent(d, e).\
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
student(amy).\ student(bob).\ student(cal).\ course(amy, cs101).\ course(bob, cs101).\ course(cal, math202).\
Key ideas — 20.2 (a) File processing with records
Files can be opened for READ, WRITE (creates a new file, replacing any existing one) or APPEND (adds to the end), and must be closed after use. Files of records can also be opened for RANDOM access.
🧑🏫 Teacher's explanation
Imagine you have a physical notebook. If you want to add a new name to a list of names sorted alphabetically (a sequential file), you can't just shove it in the middle of the page without rewriting everything that comes after it. You'd have to copy the whole notebook into a new one, inserting the name in the right spot as you go. This is 'sequential' processing.
📖 Definitions
🔍 Worked example — Easy
A serial file contains the following IDs in order: 10, 20, 30. We want to append the ID 25. Describe the steps to update the file.
🔍 Worked example — Standard
A sequential file contains student IDs: 101, 105, 110. We want to insert ID 103 into the correct position. Trace the logic of the insertion.
🔍 Worked example — Exam-style
A random file uses a hashing function H(x) = (x mod 10) to determine the address. If we want to store a record with ID 42, what is its address?
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
Key ideas — 20.2 (b) Exceptions and exception handling
An exception is an unexpected event that occurs while a program is running (a run-time error) and disrupts its normal flow: e.g. a file that does not exist, a division by zero, invalid input that cannot be converted, an array index out of range, or a lost network connection.
🧑🏫 Teacher's explanation
Imagine you are following a recipe to bake a cake. Most of the time, everything goes smoothly. However, halfway through, you realize you have run out of eggs. If you simply stop and freeze in the kitchen, you haven't finished the task, and you've left a mess. Instead, a 'handled' situation would be: "If I run out of eggs, I will use applesauce as a substitute instead." This is exactly what exception handling does for software.
📖 Definitions
🔍 Worked example — Easy
Identify the appropriate exception type for the following scenario: A program attempts to access an element at index 10 in an array that only has 5 elements (indices 0-4).
🔍 Worked example — Standard
Trace the execution of the following pseudocode:
🔍 Worked example — Exam-style
A program uses the following logic to read a file:
⚠️ Common errors
🧠 Quick check 1
🧠 Quick check 2
🎯 Exam question
State one reason why exception handling is more appropriate in this scenario than using simple validation (e.g., an IF statement checking if the divisor is zero) before the division occurs. [1] [1 marks]
Worked example
Design classes for a library: the library holds many books; a book has a title, an author and an on-loan status; a reference book is a book that can never be loaned.
🔍 Worked example — Easy
Identify the OOP concept: A class Student has a private attribute Grade. The only way to change Grade is through a method SetGrade(). Which principle is being demonstrated?
🔍 Worked example — Standard
A class Vehicle has a method Move(). A subclass Car inherits from Vehicle but provides its own version of Move() to include engine sounds. Name the OOP concept used when Car changes the behavior of the inherited method.
🔍 Worked example — Exam-style
Complete the Cambridge Pseudocode for the Lend method in the Book class.
⚠️ Common errors
Common mistakes
Practice
Answers: (1) A style or approach to programming with its own features; declarative: states facts and rules (what), not the steps (how), and the system searches to satisfy goals. (2) (a) 60; (b) 85; (c) 12; (d) 7. (3) Inheritance: a subclass gets the attributes and methods of its superclass; polymorphism: a method with the same name behaves differently in different classes (the subclass overrides it). (4) Encapsulation: the data cannot be changed directly from outside the class, only through methods, which can validate changes and keep the object in a valid state. (5) grandfather(X, Z) IF parent(X, Y) AND parent(Y, Z) AND male(X). Who = zain; Who = maya. (6) The records must stay in key order; a new record cannot be inserted in the middle of the existing file, so the records are copied to a new file with the new record written in its correct position. (7) An unexpected event during execution that disrupts the program (a run-time error); e.g. opening a file that might not exist; converting user input to a number.