Layer 1: Theoretical Foundation

πŸ“ 13.1 User-defined data types πŸŽ“ Platform: A Level Computer Science

πŸ”’ Sign in to view this topic's basics.

Sign in.

Syllabus Update

13.1 User-defined data types

Computer Science A level notes, prepared by Dr. Hamdeni

13.1a: Show understanding of why user-defined types are necessary

What is a user-defined data type?

  • A user-defined data type is created by the programmer so the program’s data matches its requirements exactly.
    • After defining the type, the programmer can declare variables of that type and use it.
    • Using user-defined data type to restrict allowed day values
      Imagine we need a day value that accepts only real days.
      The seven days can be defined as a type listing Monday to Sunday.
      A variable today uses this type, and setting today = Wednesday is valid.
      Result: values outside the list are rejected.
  • User-defined types fall into two families. A non-composite type is defined without referring to any other type. A composite type is defined with reference to at least one other type. Details about these two families will be in next sections.

Why user-defined types are necessary

  • User-defined types are necessary when built-in types are not sufficient to express the program’s specific data or constraints.
    • Defining a type lets the programmer state exactly which values and structure are allowed.
    • User-defined types add flexibility and help organize complex data, which can improve code readability and maintainability.
    • Built-in types are standard compartments. A user-defined type is a suitcase you redesign to fit your items exactly.

13.1b: Define and use non-composite types including enumerated, pointer

  • A non-composite user-defined type is defined without reference to any other type. Two examples of non-composite user-defined types in the syllabus are the enumerated type and the pointer type.

Pointer types

  • Pointer type stores the memory location of a value. To read through a pointer, go to that location and fetch the value.
    • The pointer’s definition states the type it points to. You can declare a pointer, store a location in it, and read the value via the pointer.
    • Pointers let programs refer to values indirectly, which is useful when building data structures that can grow or shrink while the program runs.
    • Pointer pseudocode
      TYPE TIntPointer = ^INTEGER
      DECLARE MyPointer : TIntPointer

Enumerated types

  • Enumerated type is a type where all possible values are explicitly listed by the programmer with an implied order. You can later declare variables of this type.
    • Because there is an order, the program can compare listed values, such as checking whether one value comes after another in the sequence.
    • Enumeration pseudocode
      To represent days of the week using an enumerated type:
      TYPE TDays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
      DECLARE Today: TDays
      Today = Wednesday
    • Seasons enumeration pseudocode
      TYPE Season = (Spring, Summer, Autumn, Winter)
    • Pointer usage with an enumeration
      TYPE Season = (Spring, Summer, Autumn, Winter)
      DECLARE ThisSeason : Season
      DECLARE NextSeason : Season
      DECLARE MyPointer : TIntPointer
      ThisSeason ← Spring
      MyPointer ← ^ThisSeason
      NextSeason ← MyPointer^ + 1
      Result: NextSeason becomes the value immediately after ThisSeason in the enumeration order.
    • An enumeration is like a short menu. You must choose one listed dish. A pointer is like the table number that tells you where the dish is, not the dish itself.

13.1c: Define and use composite data types including set, record and class/object

  • A composite user-defined type is defined with reference to at least one other type. Composite user-defined types include sets, records, and classes (objects).

Records

  • A record groups named fields that match the program’s data.
    • Each field in a record can be of a different data type.
    • Record for a flight
      We want to design a flight as a record data type. We will declare the following fields:
      FlightNumber (integer), FlightTime (minutes), StartLocation (text), EndLocation (text), Plane (code).
      One example instance Flight1 has:
      β€’ FlightNumber = 7837
      β€’ StartLocation = β€œBerlin”
      β€’ EndLocation = β€œLondon”
      β€’ FlightTime = 110
      β€’ Plane = β€œD242”.
    • Record pseudocode
      TYPE StudentRecord
      DECLARE LastName : STRING
      DECLARE FirstName : STRING
      DECLARE DateOfBirth : DATE
      DECLARE YearGroup : INTEGER
      DECLARE FormGroup : CHAR
      ENDTYPE

Classes

  • A class groups properties (attributes) and methods (procedures or functions) that its objects can use.
  • Simple class design (Pet)
    Suppose we design a class Pet with properties PetName, PetType, OwnerTel (text), UniqueID (integer), regDate (date).
    Methods include CreatePet, which assigns a unique ID and sets regDate, and GetContact, which returns PetName and OwnerTel.
    This shows how a class combines data and behaviour.

Sets

  • Set is an unordered collection. Use set operations to combine or compare sets.
    • Union combines two sets into one set that contains every element that appears in either set (no duplicates).
    • Intersection keeps only the elements that are present in both sets.
    • Each element in a set is unique; duplicates are not stored.
    • Set pseudocode
      TYPE LetterSet = SET OF CHAR
      DEFINE Vowels ('A','E','I','O','U') : LetterSet

13.1d: Choose and design an appropriate user-defined data type for a given problem

  • An appropriate user-defined type matches the scenario’s data:
    • Fixed, known list of values β†’ choose an enumerated type.
    • Several labelled pieces of data together β†’ choose a record.
    • Many similar items with actions β†’ choose a class.
    • Need unique membership checks and set operations β†’ choose a set.
    • Need to store a location (address) of a value β†’ choose a pointer type.
    • Matching scenarios to types
      A fixed set of colours to represent in a program: an enumeration suits this because it lists every allowed value.
      Details about each house for sale: a record with one field per detail to keep related data together.
      Storing locations of integer values in memory: a pointer type to hold the location of those values.
    • If you can list every valid value, choose an enumeration. If you need several labelled pieces together, choose a record. If you will create many similar items with actions, choose a class. If you need unique membership checks, choose a set. If you need to store locations of values, choose a pointer.

Quick navigation: jump to another layer

Now that you have a general overview of the basics, you can move to Layer 2 by clicking on one of the following buttons: