The shape of the lesson
By the end of the lesson, learners can
- Non-composite: enumerated and pointer: User-defined types are necessary because a language cannot predict every kind of data.
- Composite: record, set and class: A composite type is a collection of data that can consist of one or more data types, grouped under one identifier.
- Choosing a type for the problem: Exam questions describe a situation and ask you to choose and design a suitable user-defined type.
- Non-composite: enumerated
TYPE T = (A, B, C); pointerTYPE P = ^INTEGER. - Pointers:
^Xis the address of X;Ptr^is the value at the address in Ptr. - Composite: record (TYPE ... ENDTYPE), set (
SET OF+DEFINE), class (CLASS ... ENDCLASS, NEW). - Choose by shape: fixed list of values, address, mixed fields, membership only, data plus methods.
One picture every idea returns to
A user-defined data type is one the programmer defines, derived from the built-in types, because the built-in types alone do not model the data in the problem. A non-composite type is defined without reference to other user types: an enumerated type lists every value it can take, in order, and a pointer type holds the memory address of a value of a stated type. A composite type groups other data types under one identifier: a record (fields of different types), a set (unordered, no duplicates, supports set operations such as membership) and a class, whose objects combine attributes with methods.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Types you design.. Built-in types such as INTEGER and STRING cannot describe everything a program needs. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Non-composite: enumerated and pointer | User-defined types are necessary because a language cannot predict every kind of data. Model the worked example Trace a pointer on the board, one line at a time. Your turn: Define an enumerated type Grade for the values Distinction, Merit, Pass and Fail, then declare a variable Result of that type and assign it Merit. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Composite: record, set and class | A composite type is a collection of data that can consist of one or more data types, grouped under one identifier. | Pairs |
| 21 to 30 min | Teach and check: Choosing a type for the problem | Exam questions describe a situation and ask you to choose and design a suitable user-defined type. Model the worked example Design a type on the board, one line at a time. Your turn: A program checks that every character of a password is one of the symbols!,?, # or $. Which user-defined type suits the list of symbols? Write its definition. | Mix-Pair-Share |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Choosing a type for the problem, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Mix-Pair-Share
Students mix around the room until you call pair; they pair with the nearest classmate, discuss the prompt, then mix again. Three prompts of rising difficulty, three different partners.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| Writing TYPE Season = ("Spring", "Summer",...) loses the mark. | Enumerated values are not strings, so they have no quotation marks. |
| A set is not an array: it has no index and no order, and adding a value that is already present changes nothing. | Do not describe a set as "a list of values with positions". |
User-defined data types: worksheet
6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.
Explain why user-defined data types are necessary.
State the difference between a composite and a non-composite data type, giving one example of each.
Write pseudocode to define an enumerated type Light with values Red, Amber and Green, declare a variable Signal of this type, and set it to Green.
TYPE TIntPointer = ^INTEGER. Count and Total are INTEGER variables; P is a TIntPointer.
Count ← 5 Total ← 9 P ← ^Count P^ ← P^ * 3 P ← ^Total P^ ← P^ - Count
State the final values of Count and Total, and what P holds.
A school records, for each club: its name, the day it meets (one of Monday to Friday), the maximum number of members and the fee per term. Design user-defined data types for this data in pseudocode.
A game stores the power-ups a player has collected. Each power-up is identified by one letter, a player cannot hold the same power-up twice, and the order of collection does not matter. Justify the choice of a set and write a suitable type definition.
Answer key and marking notes
- Built-in (primitive) types cannot represent every kind of data a problem needs.
- The programmer can define a type, derived from the built-in types, that matches the problem / makes the code clearer and lets invalid values be rejected.
- Composite: built from / refers to other data types, grouped under one identifier, e.g. record, set, class.
- Non-composite: defined without reference to other data types, e.g. enumerated, pointer.
TYPE Light = (Red, Amber, Green).DECLARE Signal : Light.Signal ← Green(no quotation marks).
- Count = 15 (P points to Count, so P^ * 3 = 15 is stored in Count).
- Total = 9 - 15 = -6.
- P holds the memory address of Total.
- Enumerated type for the day:
TYPE WeekDay = (Monday, Tuesday, Wednesday, Thursday, Friday). TYPE ClubRecord...ENDTYPE.DECLARE ClubName : STRING,DECLARE MeetDay : WeekDay.DECLARE MaxMembers : INTEGER,DECLARE Fee : REAL.
- Only membership matters (does the player have it?), so no order or index is needed.
- A set holds no duplicates, matching the rule that a power-up cannot be held twice.
TYPE PowerUpSet = SET OF CHAR(and a DEFINE line for any fixed set of values).
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
Which of these is a composite user-defined data type?
- ASet
- BEnumerated
- CPointer
- DBOOLEAN
TYPE TIntPointer = ^INTEGER. Count is an INTEGER and P is a TIntPointer.
Count ← 7 P ← ^Count P^ ← P^ + 5 Count ← Count * 2 OUTPUT P^What is output?
- A24
- B12
- C14
- DThe memory address of Count
TYPE Size = (Small, Medium, Large, XLarge) DECLARE S : Size S ← Small S ← S + 2What does S hold after the last line?
- ALarge
- BMedium
- C2
- DNothing: an error occurs because Small is not a number
A pizza app stores the toppings a customer has chosen from a fixed menu. A topping can be chosen only once and the order of choosing does not matter; the app only needs to check whether a topping has been chosen. Which user-defined type best suits the chosen toppings?
- AA set
- BAn enumerated type
- CA record
- DA pointer type
CLASS Account
PRIVATE Balance : REAL
PUBLIC PROCEDURE NEW(Start : REAL)
Balance ← Start
ENDPROCEDURE
PUBLIC FUNCTION GetBalance() RETURNS REAL
RETURN Balance
ENDFUNCTION
ENDCLASS
DECLARE Acc : Account
Acc ← NEW Account(50.00)
OUTPUT Acc.GetBalance()
Acc.Balance ← Acc.Balance + 10.00Which line of the main program is invalid?- A
Acc.Balance ← Acc.Balance + 10.00 - B
DECLARE Acc : Account - C
Acc ← NEW Account(50.00) - D
OUTPUT Acc.GetBalance()
A and B are INTEGER variables; P and Q are of type TIntPointer = ^INTEGER.
A ← 3 B ← 10 P ← ^A Q ← ^B P^ ← Q^ - P^ Q ← P Q^ ← Q^ * 2 OUTPUT A, " ", BWhat is output?
- A14 10
- B7 20
- C14 20
- D7 10
Quiz key
A set is a collection of values of a base type, so it is composite, like a record or a class. Enumerated and pointer types are the two non-composite user-defined types, and BOOLEAN is a built-in type, not a user-defined one.
P holds the address of Count, so P^ ← P^ + 5 changes Count to 12, and Count ← Count * 2 makes it 24. P^ is the value at that address, so 24 is output. 12 forgets that the later change to Count is seen through P, 14 ignores the change made through P, and the address would be output only by OUTPUT P, without the caret.
Enumerated values are ordered, so adding 2 moves two values on from Small to Large. Medium is only one step on, S cannot hold the integer 2 because its type is Size, and arithmetic on an enumerated value is allowed precisely because its values are ordered.
A set is unordered, holds no duplicates and supports membership tests, which matches the data exactly. An enumerated type could list the menu but a variable of that type holds only one value at a time, a record groups different facts about one thing, and a pointer only holds an address.
Balance is PRIVATE, so code outside the class cannot read or change it directly; it may only be reached through a method. Declaring a variable of class type is valid, NEW Account(50.00) calls the constructor in the correct 9618 form, and GetBalance is a PUBLIC method, so calling it is allowed.
P^ ← Q^ - P^ sets A to 10 - 3 = 7. Q ← P copies the address of A into Q, so Q no longer points at B, and Q^ ← Q^ * 2 doubles A to 14 while B stays 10. 7 20 assumes Q still points at B, 14 20 changes both variables, and 7 10 ignores the last line.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.