A Level · Topic 13.1
A-Level Computer Science 9618 / Topic 13 / A2

Types you design.

Built-in types such as INTEGER and STRING cannot describe everything a program needs. A user-defined type lets the programmer create exactly the type the problem calls for, from a list of named values to a record, a set or a class.

The key idea

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.

User-defined data types Non-composite Composite defined without other types built from other data types EnumeratedPointer RecordSetClass / object (Spring, Summer, ...)^INTEGER TYPE ... ENDTYPESET OF CHAR the programmer defines the type when the built-in types do not fit the problem
Fig. 1: the two families of user-defined data type named in the 9618 syllabus
Section 01

Non-composite: enumerated and pointer.

User-defined types are necessary because a language cannot predict every kind of data. Defining your own type makes code easier to read, lets the translator reject invalid values and keeps the program close to the problem. The two non-composite types in the syllabus are enumerated and pointer.

TYPE Season = (Spring, Summer, Autumn, Winter)
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
ThisSeason ← Summer

TYPE TIntPointer = ^INTEGER
DECLARE Score : INTEGER
DECLARE MyPointer : TIntPointer
Score ← 40
MyPointer ← ^Score          // MyPointer now holds the address of Score
MyPointer^ ← MyPointer^ + 2 // the value at that address becomes 42
Worked example Trace a pointer
Using the code above, what are the values of Score and MyPointer^ after the last line? What would OUTPUT MyPointer display?
  1. Score ← 40 stores 40 in the memory location called Score.
  2. MyPointer ← ^Score stores the address of Score in MyPointer, not the value 40.
  3. MyPointer^ ← MyPointer^ + 2 reads the value at that address (40), adds 2 and writes 42 back to the same location, which is Score.
  4. So Score and MyPointer^ are both 42. OUTPUT MyPointer would display a memory address, not 42.
Answer: Score = 42 and MyPointer^ = 42; MyPointer holds the address of Score
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.
Examiner trap

Writing TYPE Season = ("Spring", "Summer", ...) loses the mark. Enumerated values are not strings, so they have no quotation marks.

Section 02

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. Records were met at AS; A Level adds sets and classes.

TYPE StudentRecord
    DECLARE LastName : STRING
    DECLARE YearGroup : INTEGER
    DECLARE FormGroup : CHAR
ENDTYPE

TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A', 'E', 'I', 'O', 'U') : LetterSet

CLASS Ticket
    PRIVATE Seat : STRING
    PRIVATE Price : REAL
    PUBLIC PROCEDURE NEW(GivenSeat : STRING, GivenPrice : REAL)
        Seat ← GivenSeat
        Price ← GivenPrice
    ENDPROCEDURE
    PUBLIC FUNCTION GetPrice() RETURNS REAL
        RETURN Price
    ENDFUNCTION
ENDCLASS

DECLARE MyTicket : Ticket
MyTicket ← NEW Ticket("F12", 18.50)
OUTPUT MyTicket.GetPrice()
TypeWhat it holdsKey property
Recordfields, which may be of different data typesfields reached by dot notation, e.g. Pupil1.YearGroup
Seta collection of values of one base typeunordered, no duplicates; supports membership tests, union, intersection and difference
Classattributes (data) and methods (procedures and functions)a template: each object is an instance created with NEW; PRIVATE attributes are reached only through methods
Examiner trap

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".

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which of these is a non-composite user-defined data type?

Quick check+10 XP

MyPointer is of type TIntPointer = ^INTEGER and holds the address of Total. What does MyPointer^ give?

Quick check+10 XP

Which statement correctly defines an enumerated type?

Quick check+10 XP

Which property distinguishes a set from a 1D array?

Section 03

Choosing a type for the problem.

Exam questions describe a situation and ask you to choose and design a suitable user-defined type. Match the shape of the data to the type, then write the definition in exact 9618 pseudocode.

Worked example Design a type
A hotel stores, for each room: the room number, the type of room (one of Single, Double, Twin or Suite), the nightly rate and whether it is occupied. Design suitable user-defined data types.
  1. The room type takes one of four fixed values, so define an enumerated type: TYPE RoomType = (Single, Double, Twin, Suite).
  2. Each room has several facts of different types, so define a record.
  3. Choose a type for each field: room number INTEGER, kind RoomType, rate REAL, occupied BOOLEAN.
  4. Write the record with TYPE ... ENDTYPE, one DECLARE per field.
Answer:
TYPE RoomType = (Single, Double, Twin, Suite)
TYPE RoomRecord
    DECLARE RoomNumber : INTEGER
    DECLARE Kind : RoomType
    DECLARE Rate : REAL
    DECLARE Occupied : BOOLEAN
ENDTYPE
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.
Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Why are user-defined data types needed?

Exam style+20 XP

TYPE Day = (Mon, Tue, Wed, Thu, Fri). Today holds Tue. What is Today + 2?

Exam style+20 XP

Which line creates a new object of class Ticket?

Exam style+20 XP

A program must store, for each loan, a book ID, the date due back and whether a fine is owed. Which type is most suitable?

Skill unlocked

User-defined data types, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Why user-defined types are needed, enumerated and pointer types, records, sets and classes, and designing a type for a given problem in 9618 pseudocode.