Skip to content

13.1 User-defined Data Types

A Level · 5 questions found

  • Why user-defined types are necessary
  • Non-composite types: enumerated, pointer
  • Composite types: set, record, class/object
  • Choose and design an appropriate user-defined type for a given problem
Q3
May/Jun 2024 Paper 3 v1 7 marks
Question 3 — page 1Question 3 — page 2
3 (a) Explain what is meant by non-composite and composite data types. ............................................................................................................................................. [3] (b) Write pseudocode statements to declare the record data type FootballClub to hold data about football clubs in a league, to include: • name of team • date team joined the league • main telephone number • name of the manager • number of members • current position in the league. ............................................................................................................................................. [4]
Show mark scheme
3(a) [3 marks]
mark per mark point (
Max 2
)
non-composite data types
Non-composite data types can both be user-defined
or
primitive
Non-composite data types do not refer to other data types in their definition / contain one data type in their
definition
Non-composite data types can be primitive/enumerated/pointer
mark per mark point (
Max 2
)
composite data types
Composite data types can be user-defined
or
primitive
Composite data types refer to other data types in their definition/contain more than one data type in their
definition
Composite data types can be record/set/class
3(b) [4 marks]
mark for
TYPE FootballClub
and
ENDTYPE
correct
mark for every two correct declarations
Example answer
TYPE FootballClub
DECLARE TeamName : STRING
DECLARE DateOfJoining : DATE
DECLARE MainTelephone : STRING
DECLARE ManagerName : STRING
DECLARE NumberOfMembers : INTEGER
DECLARE LeaguePosition : INTEGER
ENDTYPE
Q3
May/Jun 2024 Paper 3 v2 7 marks
Question 3 — page 1Question 3 — page 2
3 (a) Explain what is meant by the term non-composite data type and give an example of a non-composite data type. Example .................................................................................................................................... [3] (b) Write pseudocode statements to declare the set data type EvenNumbers to hold this set of even numbers between 2 and 12: 2, 4, 6, 8, 10, 12 ............................................................................................................................................. [4]
Show mark scheme
3(a) [3 marks]
mark per mark point (
Max 3
)
A data type that is defined without referencing another data type.
It can be a primitive data type found in a programming language
or
a user-defined data type.
Example – enumerated data type / pointer data type / allow a correct example of an enumerated or pointer data
type declaration.
3(b) [4 marks]
mark per mark point (
Max 4
)
Type statement fully correct
DEFINE EvenNumbers
Correct list of values in brackets
:
from Type statement used
Example answer
TYPE Numbers = SET OF INTEGER
DEFINE EvenNumbers (2, 4, 6, 8, 10, 12)
:
Numbers
Q3
May/Jun 2024 Paper 3 v3 7 marks
Question 3 — page 1Question 3 — page 2
3 (a) Explain what is meant by non-composite and composite data types. ............................................................................................................................................. [3] (b) Write pseudocode statements to declare the record data type FootballClub to hold data about football clubs in a league, to include: • name of team • date team joined the league • main telephone number • name of the manager • number of members • current position in the league. ............................................................................................................................................. [4] ,  ,
Show mark scheme
3(a) [3 marks]
mark per mark point (
Max 2
)
non-composite data types
Non-composite data types can both be user-defined
or
primitive
Non-composite data types do not refer to other data types in their definition / contain one data type in their
definition
Non-composite data types can be primitive/enumerated/pointer
mark per mark point (
Max 2
)
composite data types
Composite data types can be user-defined
or
primitive
Composite data types refer to other data types in their definition/contain more than one data type in their
definition
Composite data types can be record/set/class
3(b) [4 marks]
mark for
TYPE FootballClub
and
ENDTYPE
correct
mark for every two correct declarations
Example answer
TYPE FootballClub
DECLARE TeamName : STRING
DECLARE DateOfJoining : DATE
DECLARE MainTelephone : STRING
DECLARE ManagerName : STRING
DECLARE NumberOfMembers : INTEGER
DECLARE LeaguePosition : INTEGER
ENDTYPE
Q2
May/Jun 2024 Paper 4 v1 5 marks
Question 2 — page 1Question 2 — page 2Question 2 — page 3Question 2 — page 4Question 2 — page 5Question 2 — page 6
2 A computer program will store data about trees. The user can enter their requirements for a tree and a suitable tree will be selected. The program is written using object‑oriented programming. The class Tree stores data about the trees. Tree TreeName : STRING HeightGrowth : INTEGER MaxHeight : INTEGER MaxWidth : INTEGER Evergreen : STRING stores the name of the tree stores the number of cm the tree will grow each year stores the maximum height in cm that the tree will grow stores the maximum width in cm that the tree will grow stores whether the tree keeps its leaves as "Yes", or loses its leaves as "No" Constructor() GetTreeName() GetGrowth() GetMaxHeight() GetMaxWidth() GetEvergreen() initialises TreeName, HeightGrowth, MaxHeight, MaxWidth and Evergreen to its parameter values returns the name of the tree returns the number of cm the tree will grow each year returns the maximum height in cm that the tree will grow returns the maximum width in cm that the tree will grow returns whether the tree keeps its leaves or loses its leaves (a) (i) Write program code to declare the class Tree and its constructor. Do not declare the other methods. Use the appropriate constructor for your programming language. All attributes must be private. If you are writing in Python, include attribute declarations using comments. Save your program as Question2_J24. Copy and paste the program code into part 2(a)(i) in the evidence document. [4] (ii) The get methods GetTreeName(), GetGrowth(), GetMaxHeight(), GetMaxWidth() and GetEvergreen() each return the relevant attribute. Write program code for the get methods. Save your program. Copy and paste the program code into part 2(a)(ii) in the evidence document. [3] (b) The text file Trees.txt stores data about 9 trees. The data in the file is stored in the format: Tree name,Height growth each year,Maximum height,Maximum width,Evergreen For example, the first row of data is: Beech,30,400,200,No The tree is a Beech. It can grow 30 cm each year. It has a maximum height of 400 cm. It has a maximum width of 200 cm. It is not evergreen (it loses its leaves). The function ReadData(): • creates an array of type Tree • reads the data from the file • raises an exception if the file is not found • creates a new object of type Tree for each tree in the file • appends each object to the array • returns the array. Write program code for ReadData(). Save your program. Copy and paste the program code into part 2(b) in the evidence document. [7] (c) The procedure PrintTrees() takes a Tree object as a parameter and outputs the tree’s name, height growth each year, maximum height, maximum width and whether it is evergreen. The output message changes depending on whether it is evergreen. If it is evergreen, it is in the format: TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows HeightGrowth cm a year. It does not lose its leaves. If it is not evergreen, it is in the format: TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows HeightGrowth cm a year. It loses its leaves each year. Write program code for PrintTrees(). Save your program. Copy and paste the program code into part 2(c) in the evidence document. [4] (d) The main program calls ReadData(), stores the return value and calls PrintTrees() with the first object in the returned array. (i) Write program code for the main program. Save your program. Copy and paste the program code into part 2(d)(i) in the evidence document. [2] (ii) Test your program. Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 2(d)(ii) in the evidence document. [1] (e) The procedure ChooseTree() takes an array of Tree objects as a parameter. The procedure prompts the user to input their requirements for a tree. The user needs to enter: • the maximum height the tree can be in cm • the maximum width the tree can be in cm • whether they want the tree to be evergreen, or not evergreen. A tree meets the requirements if: • the tree’s maximum height is not more than the user’s input and • the tree’s maximum width is not more than the user’s input and • the tree matches their evergreen input. The procedure creates a new array of all the Tree objects that meet all the requirements. The procedure calls PrintTrees() for each Tree object that meets all the requirements. If there are no trees that meet all the requirements, a suitable message is output. (i) Write program code for ChooseTree(). Save your program. Copy and paste the program code into part 2(e)(i) in the evidence document. [6] (ii) The procedure ChooseTree() needs amending. After the procedure has output the list of trees that meet all the requirements, the procedure needs to: • take as input the name of one of the trees that the user would like to buy from those that meet all the requirements • take as input the height of the tree in cm when it is bought • calculate and output how many years it will take the tree to grow to its maximum height. For example, the user inputs the tree, Beech. The tree’s height is 40 cm when bought. The tree will take 12 years to reach its maximum height of 400 cm. Write program code to amend ChooseTree(). Save your program. Copy and paste the program code into part 2(e)(ii) in the evidence document. [2] (iii) Write program code to amend the main program to call ChooseTrees(). Test your program with the following tree requirements: • a maximum height of 400 cm • a maximum width of 200 cm • a tree that is evergreen (does not lose its leaves). When asked for the tree selection, use the following data: • first tree name entered is ‘Blue Conifer’ • starting height is 100 cm. Take a screenshot of the outputs. Save your program. Copy and paste the screenshot into part 2(e)(iii) in the evidence document. [2]
Show mark scheme
2(a)(i)
VB.NET
Class Tree
Private TreeName As String
Private HeightGrowth As Integer
Private MaxHeight As Integer
Private MaxWidth As Integer
Private Evergreen As String
Sub New(Name, HGrowth, MaxH, MaxW, PEvergreen)
TreeName = Name
HeightGrowth = HGrowth
MaxHeight = MaxH
MaxWidth = MaxW
Evergreen = PEvergreen
End Sub
End Class
Python
class Tree:
def __init__(self, Name, HGrowth, MaxH, MaxW, PEvergreen):
self.__TreeName = Name
self.__HeightGrowth = HGrowth
self.__MaxHeight = MaxH
self.__MaxWidth = MaxW
self.__Evergreen = PEvergreen
2(a)(ii)
VB.NET
Function GetTreeName()
Return TreeName
End Function
Function GetMaxHeight()
Return MaxHeight
End Function
Function GetMaxWIdth()
Return MaxWidth
End Function
Function GetGrowth()
Return HeightGrowth
End Function
Function GetEvergreen()
Return Evergreen
End Function
Python
def GetTreeName(self):
return self.__TreeName
def GetMaxHeight(self):
return self.__MaxHeight
def GetMaxWidth(self):
return self.__MaxWidth
def GetGrowth(self):
return self.__HeightGrowth
def GetEvergreen(self):
return self.__Evergreen
2(b)
Python
def ReadData():
TreeObjects=[]
try:
File = open("Trees.txt")
TreeData = []
TreeData = File.read().split("\n")
SplitTrees = []
for Item in TreeData:
SplitTrees.append(Item.split(","))
File.close()
for Item in SplitTrees:
TreeObjects.append(Tree(Item[0],int(Item[1]),int(Item[2]),int(Item[3]),Item[4]))
except IOError:
print ("invalid file")
return TreeObjects
2(c)
Python
def PrintTrees(Item):
Final = "does not lose its leaves"
if Item.GetEvergreen() == "No":
Final = "loses its leaves each year"
print(Item.GetTreeName(), "has a maximum height", Item.GetMaxHeight(),"a maximum
width",Item.GetMaxWidth(),"and grows", Item.GetGrowth(),"cm a year. It",Final)
2(d)(i) [2 marks]
1 mark each
Calling
ReadData()
and
storing/using return value (as array of type
Tree
)…
…calling
PrintTrees()
with first object in returned array as parameter
Tree[] TreeData = new Tree[20];
TreeData = ReadData();
PrintTrees(TreeData[0]);
VB.NET
Sub Main(args As String())
Dim TreeObjects(10) As Tree
TreeObjects = ReadData()
PrintTrees(Treeobjects(0))
End Sub
Python
TreeObjects = ReadData()
PrintTrees(TreeObjects[0])
2(d)(ii) [1 mark]
Screenshot showing output
2(e)(i)
End If
Dim count As Integer = 0
For x = 0 To 8
If Trees(x).GetMaxHeight() <= MaxHeight And Trees(x).GetMaxWidth() <= MaxWidth
And keep = Trees(x).GetEvergreen() Then
Options(count) = Trees(x)
PrintTrees(Trees(x))
count = count + 1
End If
Next x
If count = 0 Then
Console.WriteLine("No suitable trees")
End If
End Sub
Python
def ChooseTree(Trees):
Evergreen = input("Do you want a tree that loses its leaves (enter lose), or keeps its
leaves (enter keep)")
MaxHeight = int(input("What is the maximum tree height in cm"))
MaxWidth = int(input("What is the maximum tree width in cm"))
Options = []
if Evergreen.lower() == "keep" or Evergreen.lower() == "keep leaves" or
Evergreen.lower() == "keeps its leaves":
keep = "Yes"
else:
keep = "No"
for Item in Trees:
if Item.GetMaxHeight() <= MaxHeight and Item.GetMaxWidth() <= MaxWidth and keep ==
Item.GetEvergreen():
Options.append(Item)
PrintTrees(Item)
if len(Options) == 0:
print("No suitable trees")
2(e)(ii)
Integer Start;
Float Height;
Float Growth;
Float Years;
while(Valid == false){
System.out.println("Enter the name of the tree you want");
String Choice = scanner.nextLine();
for(Integer X = 0; X < Counter; X++){
if((Options[X].GetTreeName()).compareTo(Choice)==0){
Valid = true;
Selected = Options[X];
System.out.println("Enter the height of the tree you would like to start
with in cm");
Start = Integer.parseInt(scanner.nextLine());
Height = (Selected.GetMaxHeight()).floatValue();
Growth = (Selected.GetGrowth()).floatValue();
Years = (Height - Start) / Growth;
System.out.println("Your tree should be full height in approximately "+
Years + " years");
}
}
Python:
Valid = False
while Valid == False:
Choice = input("Enter the name of the tree you want")
for Item in Options:
if Item.GetTreeName() == Choice:
Valid = True
Selected = Item
Start = int(input("Enter the height of the tree you would like to start with in
cm"))
Years = (Selected.GetMaxHeight() - Start)/Selected.GetGrowth()
print("Your tree should be full height in approximately", Years,"years")
2(e)(iii) [2 marks]
1 mark each
Screenshot shows the user requirements input (height 400, width 200, evergreen)
and
outputs the correct trees (Blue
conifer and green conifer)
Screenshot shows the tree selection input (Blue Conifer with height 100)
and
outputs the correct result (3 years / 3.75
/ 4 years)
Q2
May/Jun 2024 Paper 4 v3 5 marks
Question 2 — page 1Question 2 — page 2Question 2 — page 3Question 2 — page 4Question 2 — page 5Question 2 — page 6
2 A computer program will store data about trees. The user can enter their requirements for a tree and a suitable tree will be selected. The program is written using object‑oriented programming. The class Tree stores data about the trees. Tree TreeName : STRING HeightGrowth : INTEGER MaxHeight : INTEGER MaxWidth : INTEGER Evergreen : STRING stores the name of the tree stores the number of cm the tree will grow each year stores the maximum height in cm that the tree will grow stores the maximum width in cm that the tree will grow stores whether the tree keeps its leaves as "Yes", or loses its leaves as "No" Constructor() GetTreeName() GetGrowth() GetMaxHeight() GetMaxWidth() GetEvergreen() initialises TreeName, HeightGrowth, MaxHeight, MaxWidth and Evergreen to its parameter values returns the name of the tree returns the number of cm the tree will grow each year returns the maximum height in cm that the tree will grow returns the maximum width in cm that the tree will grow returns whether the tree keeps its leaves or loses its leaves (a) (i) Write program code to declare the class Tree and its constructor. Do not declare the other methods. Use the appropriate constructor for your programming language. All attributes must be private. If you are writing in Python, include attribute declarations using comments. Save your program as Question2_J24. Copy and paste the program code into part 2(a)(i) in the evidence document. [4] (ii) The get methods GetTreeName(), GetGrowth(), GetMaxHeight(), GetMaxWidth() and GetEvergreen() each return the relevant attribute. Write program code for the get methods. Save your program. Copy and paste the program code into part 2(a)(ii) in the evidence document. [3] (b) The text file Trees.txt stores data about 9 trees. The data in the file is stored in the format: Tree name,Height growth each year,Maximum height,Maximum width,Evergreen For example, the first row of data is: Beech,30,400,200,No The tree is a Beech. It can grow 30 cm each year. It has a maximum height of 400 cm. It has a maximum width of 200 cm. It is not evergreen (it loses its leaves). The function ReadData(): • creates an array of type Tree • reads the data from the file • raises an exception if the file is not found • creates a new object of type Tree for each tree in the file • appends each object to the array • returns the array. Write program code for ReadData(). Save your program. Copy and paste the program code into part 2(b) in the evidence document. [7] (c) The procedure PrintTrees() takes a Tree object as a parameter and outputs the tree’s name, height growth each year, maximum height, maximum width and whether it is evergreen. The output message changes depending on whether it is evergreen. If it is evergreen, it is in the format: TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows HeightGrowth cm a year. It does not lose its leaves. If it is not evergreen, it is in the format: TreeName has a maximum height MaxHeight a maximum width MaxWidth and grows HeightGrowth cm a year. It loses its leaves each year. Write program code for PrintTrees(). Save your program. Copy and paste the program code into part 2(c) in the evidence document. [4] (d) The main program calls ReadData(), stores the return value and calls PrintTrees() with the first object in the returned array. (i) Write program code for the main program. Save your program. Copy and paste the program code into part 2(d)(i) in the evidence document. [2] (ii) Test your program. Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 2(d)(ii) in the evidence document. [1] (e) The procedure ChooseTree() takes an array of Tree objects as a parameter. The procedure prompts the user to input their requirements for a tree. The user needs to enter: • the maximum height the tree can be in cm • the maximum width the tree can be in cm • whether they want the tree to be evergreen, or not evergreen. A tree meets the requirements if: • the tree’s maximum height is not more than the user’s input and • the tree’s maximum width is not more than the user’s input and • the tree matches their evergreen input. The procedure creates a new array of all the Tree objects that meet all the requirements. The procedure calls PrintTrees() for each Tree object that meets all the requirements. If there are no trees that meet all the requirements, a suitable message is output. (i) Write program code for ChooseTree(). Save your program. Copy and paste the program code into part 2(e)(i) in the evidence document. [6] (ii) The procedure ChooseTree() needs amending. After the procedure has output the list of trees that meet all the requirements, the procedure needs to: • take as input the name of one of the trees that the user would like to buy from those that meet all the requirements • take as input the height of the tree in cm when it is bought • calculate and output how many years it will take the tree to grow to its maximum height. For example, the user inputs the tree, Beech. The tree’s height is 40 cm when bought. The tree will take 12 years to reach its maximum height of 400 cm. Write program code to amend ChooseTree(). Save your program. Copy and paste the program code into part 2(e)(ii) in the evidence document. [2] (iii) Write program code to amend the main program to call ChooseTrees(). Test your program with the following tree requirements: • a maximum height of 400 cm • a maximum width of 200 cm • a tree that is evergreen (does not lose its leaves). When asked for the tree selection, use the following data: • first tree name entered is ‘Blue Conifer’ • starting height is 100 cm. Take a screenshot of the outputs. Save your program. Copy and paste the screenshot into part 2(e)(iii) in the evidence document. [2]
Show mark scheme
2(a)(i)
VB.NET
Class Tree
Private TreeName As String
Private HeightGrowth As Integer
Private MaxHeight As Integer
Private MaxWidth As Integer
Private Evergreen As String
Sub New(Name, HGrowth, MaxH, MaxW, PEvergreen)
TreeName = Name
HeightGrowth = HGrowth
MaxHeight = MaxH
MaxWidth = MaxW
Evergreen = PEvergreen
End Sub
End Class
Python
class Tree:
def __init__(self, Name, HGrowth, MaxH, MaxW, PEvergreen):
self.__TreeName = Name
self.__HeightGrowth = HGrowth
self.__MaxHeight = MaxH
self.__MaxWidth = MaxW
self.__Evergreen = PEvergreen
2(a)(ii)
VB.NET
Function GetTreeName()
Return TreeName
End Function
Function GetMaxHeight()
Return MaxHeight
End Function
Function GetMaxWIdth()
Return MaxWidth
End Function
Function GetGrowth()
Return HeightGrowth
End Function
Function GetEvergreen()
Return Evergreen
End Function
Python
def GetTreeName(self):
return self.__TreeName
def GetMaxHeight(self):
return self.__MaxHeight
def GetMaxWidth(self):
return self.__MaxWidth
def GetGrowth(self):
return self.__HeightGrowth
def GetEvergreen(self):
return self.__Evergreen
2(b)
Python
def ReadData():
TreeObjects=[]
try:
File = open("Trees.txt")
TreeData = []
TreeData = File.read().split("\n")
SplitTrees = []
for Item in TreeData:
SplitTrees.append(Item.split(","))
File.close()
for Item in SplitTrees:
TreeObjects.append(Tree(Item[0],int(Item[1]),int(Item[2]),int(Item[3]),Item[4]))
except IOError:
print ("invalid file")
return TreeObjects
2(c)
Python
def PrintTrees(Item):
Final = "does not lose its leaves"
if Item.GetEvergreen() == "No":
Final = "loses its leaves each year"
print(Item.GetTreeName(), "has a maximum height", Item.GetMaxHeight(),"a maximum
width",Item.GetMaxWidth(),"and grows", Item.GetGrowth(),"cm a year. It",Final)
2(d)(i) [2 marks]
1 mark each
Calling
ReadData()
and
storing/using return value (as array of type
Tree
)…
…calling
PrintTrees()
with first object in returned array as parameter
Tree[] TreeData = new Tree[20];
TreeData = ReadData();
PrintTrees(TreeData[0]);
VB.NET
Sub Main(args As String())
Dim TreeObjects(10) As Tree
TreeObjects = ReadData()
PrintTrees(Treeobjects(0))
End Sub
Python
TreeObjects = ReadData()
PrintTrees(TreeObjects[0])
2(d)(ii) [1 mark]
Screenshot showing output
2(e)(i)
End If
Dim count As Integer = 0
For x = 0 To 8
If Trees(x).GetMaxHeight() <= MaxHeight And Trees(x).GetMaxWidth() <= MaxWidth
And keep = Trees(x).GetEvergreen() Then
Options(count) = Trees(x)
PrintTrees(Trees(x))
count = count + 1
End If
Next x
If count = 0 Then
Console.WriteLine("No suitable trees")
End If
End Sub
Python
def ChooseTree(Trees):
Evergreen = input("Do you want a tree that loses its leaves (enter lose), or keeps its
leaves (enter keep)")
MaxHeight = int(input("What is the maximum tree height in cm"))
MaxWidth = int(input("What is the maximum tree width in cm"))
Options = []
if Evergreen.lower() == "keep" or Evergreen.lower() == "keep leaves" or
Evergreen.lower() == "keeps its leaves":
keep = "Yes"
else:
keep = "No"
for Item in Trees:
if Item.GetMaxHeight() <= MaxHeight and Item.GetMaxWidth() <= MaxWidth and keep ==
Item.GetEvergreen():
Options.append(Item)
PrintTrees(Item)
if len(Options) == 0:
print("No suitable trees")
2(e)(ii)
Integer Start;
Float Height;
Float Growth;
Float Years;
while(Valid == false){
System.out.println("Enter the name of the tree you want");
String Choice = scanner.nextLine();
for(Integer X = 0; X < Counter; X++){
if((Options[X].GetTreeName()).compareTo(Choice)==0){
Valid = true;
Selected = Options[X];
System.out.println("Enter the height of the tree you would like to start
with in cm");
Start = Integer.parseInt(scanner.nextLine());
Height = (Selected.GetMaxHeight()).floatValue();
Growth = (Selected.GetGrowth()).floatValue();
Years = (Height - Start) / Growth;
System.out.println("Your tree should be full height in approximately "+
Years + " years");
}
}
Python:
Valid = False
while Valid == False:
Choice = input("Enter the name of the tree you want")
for Item in Options:
if Item.GetTreeName() == Choice:
Valid = True
Selected = Item
Start = int(input("Enter the height of the tree you would like to start with in
cm"))
Years = (Selected.GetMaxHeight() - Start)/Selected.GetGrowth()
print("Your tree should be full height in approximately", Years,"years")
2(e)(iii) [2 marks]
1 mark each
Screenshot shows the user requirements input (height 400, width 200, evergreen)
and
outputs the correct trees (Blue
conifer and green conifer)
Screenshot shows the tree selection input (Blue Conifer with height 100)
and
outputs the correct result (3 years / 3.75
/ 4 years)