Multiple choiceQuestion 1 · Recall
Loading…
By the end you can explain why built-in types are sometimes insufficient, define and use enumerated types, pointer types, set types, record types, and class/object types, and select the most appropriate user-defined type for a given problem.
Built-in types (INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE, ARRAY, FILE) cover many situations but cannot naturally represent:
An enumerated type defines a variable that can take only one of a listed set of named values. The values are constants; internally they are often represented as integers.
Pseudocode example:
TYPE DayOfWeek = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
TYPE Suit = (Clubs, Diamonds, Hearts, Spades)
TYPE TrafficLight = (Red, Amber, Green)
DECLARE today : DayOfWeek
today <- Monday
IF today = Saturday OR today = Sunday THEN
OUTPUT "Weekend"
ENDIF
Benefits:
A pointer type holds the memory address of another variable or data structure. Pointers enable dynamic data structures (linked lists, trees, graphs) and passing large structures by reference.
Pseudocode example:
TYPE NodePtr = POINTER TO Node
TYPE Node = RECORD
data : INTEGER
next : NodePtr
ENDRECORD
DECLARE head : NodePtr
head <- NULL
Key concepts:
NULL (or NIL) represents a pointer that points to nothinghead^.data)NEW(ptr) allocates memory, DISPOSE(ptr) frees itA set type represents an unordered collection of distinct values from a base type (usually an ordinal type like an enumerated type or a subrange of integers).
Pseudocode example:
TYPE Skills = SET OF (Python, Java, SQL, HTML, CSS, JavaScript)
DECLARE mySkills : Skills
mySkills <- {Python, SQL, JavaScript}
DECLARE required : Skills
required <- {Python, Java}
IF Python IN mySkills THEN OUTPUT "Has Python" ENDIF
IF required <= mySkills THEN OUTPUT "Meets requirements" ENDIF // subset check
Set operations:
x IN setset1 + set2 (or set1 UNION set2)set1 * set2 (or set1 INTERSECT set2)set1 - set2set1 <= set2set1 = set2A record (already introduced in topic 10.1) groups related fields of different types under one identifier. At A Level, records can be nested and can contain pointers.
Pseudocode example:
TYPE Address = RECORD
street : STRING
city : STRING
postcode : STRING
ENDRECORD
TYPE Student = RECORD
name : STRING
age : INTEGER
homeAddress : Address
subjects : SET OF (Maths, Physics, Chemistry, Biology)
ENDRECORD
A class defines a template for objects that encapsulate both data (attributes) and behaviour (methods). This is the foundation of object-oriented programming.
Pseudocode example:
CLASS BankAccount
PRIVATE balance : REAL
PUBLIC accountNumber : STRING
CONSTRUCTOR(accNum : STRING)
accountNumber <- accNum
balance <- 0.0
ENDCONSTRUCTOR
METHOD deposit(amount : REAL)
IF amount > 0 THEN balance <- balance + amount ENDIF
ENDMETHOD
METHOD withdraw(amount : REAL) RETURNS BOOLEAN
IF amount > 0 AND amount <= balance THEN
balance <- balance - amount
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDMETHOD
METHOD getBalance() RETURNS REAL
RETURN balance
ENDMETHOD
ENDCLASS
DECLARE myAccount : BankAccount
myAccount <- NEW BankAccount("ACC12345")
myAccount.deposit(100.0)
Key OOP concepts:
NEW ClassName(...) creates an object instanceWorked example: Choosing the right user-defined type
For each scenario, choose the most appropriate user-defined type: