Advanced Data Representation and File Organisation: Question 6
Syllabus 13.1
A vehicle-tracking app needs, for each vehicle, to store its RegistrationNumber and its
current Speed together with a self-contained operation IsSpeeding() that works out. Using
only that one vehicle's own stored data. Whether it is currently over the speed limit. The
operation must be bundled with the data it acts on, rather than written as a separate function
kept apart from the data.
Which composite user-defined data type is most suitable for representing one vehicle?
Show worked solution Hide worked solution
Worked solution
Matching the requirement to a composite data type
The syllabus lists four composite user-defined data types: set, record, and
class/object. The requirement here has two parts: (1) group RegistrationNumber and Speed
together for one vehicle, and (2) bundle in an operation, IsSpeeding(), that works directly on
that vehicle’s own stored data, as part of the same variable.
A record can satisfy the first part. It groups related fields of possibly different types
under one identifier, but a record holds data only. It has no mechanism for storing an
operation alongside its fields, so IsSpeeding() could not be bundled into it directly.
A class/object satisfies both parts. A class defines both attributes (data fields, such as
RegistrationNumber and Speed) and methods (operations, such as IsSpeeding()) together,
and an object created from that class carries its own data and can have its methods called
directly on it, exactly what the scenario asks for.
CLASS Vehicle
PRIVATE RegistrationNumber : STRING
PRIVATE Speed : INTEGER
PUBLIC FUNCTION IsSpeeding() RETURNS BOOLEAN
IF Speed > 70 THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
ENDCLASS
Why the other options are wrong
- A (record): groups data fields together but cannot also hold an operation as part of the same variable. A record has no methods.
- C (set): a set is an unordered collection of unique values (for example, a set of colours currently in use); it is not designed to model one entity’s data and behaviour together.
- D (enumerated): an enumerated type defines a fixed list of named constant values (for
example,
Easy,Medium,Hard); it has no fields and no operations at all.
Final answer
B. A class/object is the correct choice, because it is the only composite type that bundles a single entity’s data attributes together with the operations that act on that data.