DoAssignment study guide
B2.1 · Adapt a program template to a specification
Learn to adapt a program template to a specification through clear examples and targeted practice.
Ontario Grade 11 Computer Science
Software Development
ICS3U study topic B2.1
A program template is a starting program: it may contain working structure, sample values, or places marked for completion. A specification describes what the finished program must do. To adapt a template, read the requirements, connect each one to the relevant part of the program, make the necessary changes, and check the result against the specification. The goal is not to add features that were never requested. This lesson uses Python 3 examples; a classroom may use another language, where the syntax may differ. The code is presented for tracing, not as code that has been run.
What you will learn
- Explain what a program template and a specification provide.
- Identify the required inputs, processing, and outputs in a specification.
- Adapt existing code by changing only what is needed to meet stated requirements.
- Trace a program’s variables and compare its expected result with the specification.
1. Read the specification before editing
A specification is a clear description of a program’s required behaviour. It may state what information the program receives, what calculation or action it performs, and what result it displays. A template gives you a base to adapt, rather than requiring you to begin with a blank file.
First, separate the requirements into input, process, and output. Then locate the parts of the template that correspond to those requirements. If a requirement says to ask for a name, for example, a hard-coded sample name will not meet it. If the specification asks for a total, the program must calculate and display that total.
Treat each requirement as something you can check. Be precise about details such as the wording of a prompt, the calculation, and the information displayed. Change the template where it fails to match a requirement; keep parts that already do the right job. This makes it easier to see why each change is needed.
- Specification: the stated requirements for a program.
- Template: existing program code used as a starting point.
- Map each requirement to input, processing, or output before editing.
2. Make a focused adaptation
A template may be almost correct but still miss a requirement. For instance, it might calculate a value correctly while displaying an unclear message, or it might use a sample value where the specification requires user input. Make the smallest changes that bring the program into line with the stated requirements.
After editing, trace the code using a particular input. Follow the statements in order and record how variable values change. Then work out what the program would display. Compare that expected result with every relevant requirement. This is a reasoned check, not a claim that the code was executed.
When the specification gives a specific example input and expected result, trace that case. If it does not, choose a suitable ordinary input and check the result against the described behaviour. Do not silently add extra requirements or features.
- Use the specification to decide what to change.
- Trace the actual values through the adapted code.
- Check expected output against the stated requirements.
3. Check whether the adaptation meets the request
A useful final check is to revisit the specification one requirement at a time. For each one, ask: where does the program satisfy this? Can the chosen input reach the relevant code? Does the output show what was requested? A program can contain valid code and still fail the specification if it omits a required prompt or displays the wrong information.
Keep the check at the level of the task. If the specification requests a simple greeting, an added menu is unnecessary. Extra behaviour can make it harder to tell whether the requested behaviour is correct. If a requirement is unclear, identify the uncertainty rather than guessing and treating the guess as part of the specification.
The examples below show how to connect requirements to a template, adapt it, and trace expected output. They use short Python 3 programs so each change and its effect can be followed directly.
- Check every requirement, not just whether the program looks complete.
- Keep the adaptation focused on what was requested.
- Distinguish stated requirements from assumptions.
4. A practical adaptation routine
A steady routine helps prevent missed details. Read the full specification, mark the inputs and outputs, and identify the required processing. Read the template before changing it: note what already matches and what does not. Edit the mismatched parts, then trace a suitable input from the first relevant statement to the displayed result.
Finally, compare that result with the specification. If a requirement is not represented in the code or cannot be checked from the output, revisit the adaptation. A trace table can make the reasoning visible by showing an input alongside the variable values that follow from it.
- Read, map, inspect, adapt, trace, and compare.
- A trace table records expected state; it does not report an actual run.
Trace for the kilometre conversion
| Point in the program | kilometres | metres |
|---|---|---|
| After reading input | 3.5 | not assigned |
| After calculation | 3.5 | 3500.0 |
| Displayed result | 3.5 | Distance in metres: 3500.0 |
Worked example
Replace a sample value with required input
Specification: Ask for a learner’s name and display a greeting using that name. The template contains a sample name instead of asking the user. Adapt it, then trace the input Mira.
The program receives a name, then displays a greeting containing it.
Python 3
name = input("Name: ")
print("Hello, " + name + "!")- Identify the mismatchThe template’s fixed sample value does not meet the requirement to ask for a name. Keep the greeting, but replace the fixed value with input.
- Trace the inputFor this trace, the user enters Mira. The input statement stores that text in name; the next statement combines it with the greeting.
- Derive the outputThe displayed line follows from the traced value. It is the expected output for this input, not a report of running the program.
Answer: Expected display: Hello, Mira!
Check: The program asks for a name and uses the entered value in its greeting.
Worked example
Adapt the calculation and label
Specification: Ask for a distance in kilometres, convert it to metres, and display the result with a clear label. The template multiplies by 100 but labels the result as metres. Adapt it and trace an input of 3.5.
Python 3
kilometres = float(input("Kilometres: "))
metres = kilometres * 1000
print("Distance in metres:", metres)- Compare template and requirementThe conversion factor from kilometres to metres is 1000, so multiplying by 100 does not meet the requested conversion. Keep the input and display structure, but correct the calculation.
- Trace the calculationThe input text is converted to a number and stored in kilometres. Multiplying by 1000 gives the value stored in metres.
- Check the displayThe label identifies the result as metres, matching the specification. The expected line is derived from the traced value.
Answer: Expected display: Distance in metres: 3500.0
Check: The calculation uses the required conversion and the output labels the result in metres.
Worked example
Add a required summary to a template
Specification: Ask for two whole-number scores, calculate their total, and display both the total and the message “Scores recorded.” The template calculates the total but does not display the required message. Adapt it and trace inputs 6 and 8.
Python 3
first = int(input("First score: "))
second = int(input("Second score: "))
total = first + second
print("Total:", total)
print("Scores recorded.")- Keep what already meets the specificationThe template already receives two whole numbers and calculates their total. Those parts match the requirements, so they can remain.
- Add the missing outputThe specification also requires a particular message. Add a display statement for it rather than changing the calculation.
- Trace the valuesThe first input is stored as 6 and the second as 8. Adding these values gives 14, which the program displays before the required message.
Answer: Expected display: Total: 14, followed by Scores recorded.
Check: Both requested outputs are present, and the total follows from the two traced inputs.
Common mistakes and how to avoid them
Changing code before identifying what the specification actually requires.
Correction: List the required input, processing, and output first, then compare the template with that list.
Assuming a program meets the specification because it contains no obvious syntax problem.
Correction: Trace an input and check each stated requirement, including required prompts and displayed messages.
Adding features that were not requested.
Correction: Keep the adaptation focused. Do not treat an invented feature as a requirement.
Lesson summary
- A template is a starting point; a specification describes the program’s required behaviour.
- Map requirements to input, processing, and output before editing.
- Preserve parts of the template that already match, and change the parts that do not.
- Trace inputs and variable values to derive expected output, then compare it with the specification.
Check your understanding
Question 1
A specification requires a program to ask for a city and display “Visit ” followed by the entered city. A template displays a fixed city name. What change best meets the specification?
- Replace the fixed city with an input statement and use the entered value in the display.
- Keep the fixed city and change only the punctuation.
- Remove the display statement so the program asks for a city only.
- Add an unrelated calculation before displaying the fixed city.
Show answer and explanation
Replace the fixed city with an input statement and use the entered value in the display.
The required input must come from the user, and the display must use that entered value.
Question 2
A template adds two inputs and displays their total. The specification also requires the message “Done.” Which adaptation is needed?
- Replace the addition with subtraction.
- Add a display statement for “Done.” while keeping the correct total calculation.
- Remove the total because the message is required.
- Change the inputs to fixed sample values.
Show answer and explanation
Add a display statement for “Done.” while keeping the correct total calculation.
The calculation already meets one requirement. Adding the missing message meets the other.
Key terms
- Program template
- Existing program code provided as a starting point for changes.
- Specification
- A description of what a program is required to do.
- Trace
- Follow the program statements and record the values and expected output for chosen input.
Continue through ICS3U
View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation B2.1. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.