Why programs are divided into modules

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Why programs are divided into modules

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes a well-designed program module?
A It does one job and is self-contained
B It does many unrelated jobs in one block
2. A student's program has a module that calculates GST. Why is this useful when testing?
A The GST module can be tested on its own
B The whole program must be finished before any testing
3. A shop app needs to validate a phone number on three different screens. What is the best approach?
A Copy and paste the same code onto each screen
B Write one validation module and reuse it on each screen
4. The tax rate changes and you edit the calculateTax module. In a well-modularised solution, what happens to the other modules?
A Every module must be rewritten
B They keep working without changes
5. Which module name best shows a single job?
A calculateTotalPrice
B doEverything

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1calculateGST(price) AA module with one job: it only works out the GST for a price and does nothing else
2assert calculateGST(100) == 10 Btotal ← calculateGST(cartPrice) + calculateGST(deliveryFee)
3Reusing a module: the same module is called from different places instead of rewriting the code COne module does many jobs. It should be split into smaller modules (validate, calculate, display) so each can be tested and changed alone.
4One module, one job: each module does a single task and a main program joins them together DThe same code is copied in many places instead of being placed in one reusable module that is called when needed.
5define doEverything() read input check input calculate total print receipt EvalidateInput() processOrder() showReceipt()
6price ← price * 1.1 ... same line copied into 6 places FA test that checks one module on its own, without running the rest of the program

Definitions

module

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ calculateTotal(price, qty) return price * qty
2.
define addGST(price) ______ price * 1.1
3.
total1 ← calculateTotal(5, 2) total2 ← ______________(8, 3)
4.
if addGST(100) _ 110 then print "Test passed" end if
5.
marks ← getMarks() avg ← calculateAverage(_____) print avg

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A define getTotal(price, qty)
B return price + qty
C total <- getTotal(5, 3)
D print total
Question 2
A define isAdult(age)
B return age > 18
C print isAdult(18) // true
D print isAdult(17) // false
Question 3
A define square(n)
B return n * n
C area <- square(side, side)
D print area
Question 4
A define average(a, b)
B total <- a + b
C print total / 2
D print average(4, 8)
Question 5
A define greet(name)
B print "Hello " + userName
C greet("Ava")
D greet("Liam")

Answer Key

Why programs are divided into modules
Section A: Quick Questions
Q1
A — It does one job and is self-contained
Q2
A — The GST module can be tested on its own
Q3
B — Write one validation module and reuse it on each screen
Q4
B — They keep working without changes
Q5
A — calculateTotalPrice
Section B: Match the Pairs
Q1
calculateGST(price) → A module with one job: it only works out the GST for a price and does nothing else
Q2
assert calculateGST(100) == 10 → A test that checks one module on its own, without running the rest of the program
Q3
Reusing a module: the same module is called from different places instead of rewriting the code → total ← calculateGST(cartPrice) + calculateGST(deliveryFee)
Q4
One module, one job: each module does a single task and a main program joins them together → validateInput() processOrder() showReceipt()
Q5
define doEverything() read input check input calculate total print receipt → One module does many jobs. It should be split into smaller modules (validate, calculate, display) so each can be tested and changed alone.
Q6
price ← price * 1.1 ... same line copied into 6 places → The same code is copied in many places instead of being placed in one reusable module that is called when needed.
Section C: Definitions
Q1
module — A self-contained part of a program that does one job, so it can be built, tested on its own, reused in other places and changed without breaking the rest of the solution.
Section D: Fill in the Blanks
Q1
define calculateTotal(price, qty) return price * qty
Q2
define addGST(price) return price * 1.1
Q3
total1 ← calculateTotal(5, 2) total2 ← calculateTotal(8, 3)
Q4
if addGST(100) = 110 then print "Test passed" end if
Q5
marks ← getMarks() avg ← calculateAverage(marks) print avg
Section E: Spot the Error
Q1
B — The module getTotal has one job: multiply the price by the quantity. Adding them gives the wrong result. It should be 'return price * qty'.
Q2
B — Testing the module on its own with 18 should give true, but 'age > 18' gives false. The test exposes the boundary error. It should be 'return age >= 18'.
Q3
C — The square module takes one input, n. Calling it with two arguments does not match its header. Reusing the module means calling it as 'square(side)'.
Q4
C — A module that does a calculation should return its result so other code can reuse it. Printing inside the module gives the caller no value. It should be 'return total / 2'.
Q5
B — The module uses userName, a variable from outside, instead of its parameter name. This makes it depend on the rest of the program and so it cannot be reused or tested alone. It should use 'name'.