Skip to content

20.1 Programming Paradigms

A Level · 6 questions found

  • What a programming paradigm is
  • Low-level: addressing modes (immediate, direct, indirect, indexed, relative)
  • Imperative/Procedural: variables, constructs, procedures, functions
  • OOP: objects, attributes, methods, classes, inheritance, polymorphism, encapsulation, getters/setters, instances
  • Design appropriate classes; write OOP code demonstrating these concepts
  • Declarative: write facts and rules; satisfy a goal using logic
Q8
May/Jun 2024 Paper 3 v1 8 marks
Question 8 — page 1Question 8 — page 2
8 A declarative programming language is used to represent the features that are available and the features that are unavailable on different body styles of a car. 01 feature(sunroof). 02 feature(automatic_tailgate). 03 feature(heated_seats). 04 feature(extra_seats). 05 feature(reversing_camera). 06 feature(dashboard_camera). 07 feature(air_conditioning). 08 feature(heated_windscreen). 09 feature(satnav). 10 bodystyle(saloon). 11 bodystyle(hatchback). 12 bodystyle(estate). 13 bodystyle(minivan). 14 bodystyle(convertible). 15 available(sunroof, hatchback). 16 available(sunroof, minivan). 17 available(reversing_camera, hatchback). 18 available(extra_seats, minivan). 19 available(reversing_camera, saloon). 20 unavailable(sunroof, convertible). 21 unavailable(automatic_tailgate, saloon). 22 unavailable(extra_seats, hatchback). These clauses have the meanings: Clause Meaning 01 Sunroof is a feature. 10 Saloon is a body style. 15 Sunroof is available on a hatchback. 20 Sunroof is unavailable on a convertible. (a) Sliding doors is a feature that is available on a minivan but unavailable on a hatchback. Write additional clauses to represent this information. 23 ............................................................................................................................................. 24 ............................................................................................................................................. 25 ............................................................................................................................................. [3] (b) Using the variable Options, the goal: available(Options, saloon) returns Options = reversing_camera Write the result returned by the goal: available(Options, hatchback) Options = ........................................................................................................................ [1] (c) F may be available for B if F is a feature and B is a body style and F is not unavailable for that body style. Write this as a rule: may_choose_option(F, B) IF ............................................................................................................................................. ............................................................................................................................................. [4]
Show mark scheme
8(a) [3 marks]
mark for each correctly completed clause (
Max 3
)
(23) feature(sliding_doors).
(24) available(sliding_doors, minivan).
(25) unavailable(sliding_doors, hatchback).
8(b) [1 mark]
(Options =) sunroof, reversing_camera
8(c) [4 marks]
mark per mark point (
Max 4
)
feature(F)
bodystyle(B)
unavailable(F, B)
all correct Boolean operators and punctuation (allow , for AND) and no additional lines of code
Example answers
may_choose_option(F, B)
feature(F) AND bodystyle(B) AND NOT unavailable(F, B).
feature(F), bodystyle(B), NOT unavailable(F, B).
Q10
May/Jun 2024 Paper 3 v2 9 marks
Question 10 — page 1Question 10 — page 2
10 A declarative programming language is used to allow clients to choose daily activities at the beach. 01 activity(paddleboarding). 02 activity(sailing). 03 activity(rowing). 04 activity(kayaking). 05 activity(jetskiing). 06 client(stevie). 07 client(antonio). 08 client(henry). 09 client(eliza). 10 client(rebeka). 11 client(danny). 12 client(erik). 13 client(simone). 14 client(petra). 15 client(frankie). 16 choice(petra, rowing). 17 choice(frankie, sailing). 18 choice(erik, sailing). 19 choice(eliza, rowing). 20 choice(stevie, jetskiing). 21 choice(henry, sailing). 22 done(henry, jetskiing). 23 done(rebeka, jetskiing). 24 done(antonio, kayaking). These clauses have the meanings: Clause Meaning 01 Paddle boarding is an activity. 06 Stevie is a client. 16 Petra has chosen rowing. 22 Henry has already done jet skiing. (a) Jane is a client who would like to choose the activity surfing and she has already done sailing. Write additional clauses to represent this information. 25 ............................................................................................................................................. 26 ............................................................................................................................................. 27 ............................................................................................................................................. 28 ............................................................................................................................................. [4] (b) Using the variable List, the goal: choice(List, rowing) returns List = petra, eliza Write the result returned by the goal: choice(List, sailing) List = .............................................................................................................................. [1] (c) C is a client who would like to choose A if A is an activity and C has not already done A. Write this as a rule: may_choose_activity(C, A) IF ............................................................................................................................................ ............................................................................................................................................. [4]
Show mark scheme
10(a) [4 marks]
mark for each correctly completed clause (
Max 4
)
Example answer
(25) client(jane).
(26) activity(surfing).
(27) choice(jane, surfing).
(28) done(jane, sailing).
10(b) [1 mark]
(List =) frankie, erik, henry
10(c) [4 marks]
mark per mark point (
Max 4
)
client(C)
activity(A)
done(C, A)
all correct Boolean operators and punctuation (allow , for AND). There must be the correct number of terms and
no additional lines of code
Example answers
may_choose_activity(C, A)
client(C) AND activity(A) AND NOT done(C, A).
client(C), activity(A), NOT (done(C, A)).
Q8
May/Jun 2024 Paper 3 v3 8 marks
Question 8 — page 1Question 8 — page 2
8 A declarative programming language is used to represent the features that are available and the features that are unavailable on different body styles of a car. 01 feature(sunroof). 02 feature(automatic_tailgate). 03 feature(heated_seats). 04 feature(extra_seats). 05 feature(reversing_camera). 06 feature(dashboard_camera). 07 feature(air_conditioning). 08 feature(heated_windscreen). 09 feature(satnav). 10 bodystyle(saloon). 11 bodystyle(hatchback). 12 bodystyle(estate). 13 bodystyle(minivan). 14 bodystyle(convertible). 15 available(sunroof, hatchback). 16 available(sunroof, minivan). 17 available(reversing_camera, hatchback). 18 available(extra_seats, minivan). 19 available(reversing_camera, saloon). 20 unavailable(sunroof, convertible). 21 unavailable(automatic_tailgate, saloon). 22 unavailable(extra_seats, hatchback). These clauses have the meanings: Clause Meaning 01 Sunroof is a feature. 10 Saloon is a body style. 15 Sunroof is available on a hatchback. 20 Sunroof is unavailable on a convertible. (a) Sliding doors is a feature that is available on a minivan but unavailable on a hatchback. Write additional clauses to represent this information. 23 ............................................................................................................................................. 24 ............................................................................................................................................. 25 ............................................................................................................................................. [3] (b) Using the variable Options, the goal: available(Options, saloon) returns Options = reversing_camera Write the result returned by the goal: available(Options, hatchback) Options = ........................................................................................................................ [1] (c) F may be available for B if F is a feature and B is a body style and F is not unavailable for that body style. Write this as a rule: may_choose_option(F, B) IF ............................................................................................................................................. ............................................................................................................................................. [4] ,   ,
Show mark scheme
8(a) [3 marks]
mark for each correctly completed clause (
Max 3
)
(23) feature(sliding_doors).
(24) available(sliding_doors, minivan).
(25) unavailable(sliding_doors, hatchback).
8(b) [1 mark]
(Options =) sunroof, reversing_camera
8(c) [4 marks]
mark per mark point (
Max 4
)
feature(F)
bodystyle(B)
unavailable(F, B)
all correct Boolean operators and punctuation (allow , for AND) and no additional lines of code
Example answers
may_choose_option(F, B)
feature(F) AND bodystyle(B) AND NOT unavailable(F, B).
feature(F), bodystyle(B), NOT unavailable(F, B).
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 v2 2 marks
Question 2 — page 1Question 2 — page 2Question 2 — page 3Question 2 — page 4Question 2 — page 5
2 A binary tree stores data in ascending order. For example: 15 19 8 3 10 A computer program stores integers in a binary tree in ascending order. The program uses Object‑Oriented Programming (OOP). The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value and a right pointer. The class Node stores the data about a node. Node LeftPointer : INTEGER Data : INTEGER RightPointer : INTEGER stores the index of the node to the left in the binary tree stores the node’s data stores the index of the node to the right in the binary tree Constructor() GetLeft() GetRight() GetData() SetLeft() SetRight() SetData() initialises Data to its parameter value initialises LeftPointer and RightPointer to −1 returns the left pointer returns the right pointer returns the data value assigns the parameter to the left pointer assigns the parameter to the right pointer assigns the parameter to the data (a) (i) Write program code to declare the class Node and its constructor. Do not declare the other methods. Use the appropriate constructor for your programming language. 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 GetLeft(), GetRight()and GetData()each return the relevant attribute. Write program code for the three get methods. Save your program. Copy and paste the program code into part 2(a)(ii) in the evidence document. [3] (iii) The set methods SetLeft(), SetRight()and SetData()each take a parameter and then store this in the relevant attribute. Write program code for the three set methods. Save your program. Copy and paste the program code into part 2(a)(iii) in the evidence document. [3] (b) The class TreeClass stores the data about the binary tree. TreeClass Tree[0:19] : Node FirstNode : INTEGER NumberNodes : INTEGER an array of 20 elements of type Node stores the index of the first node in the tree stores the quantity of nodes in the tree Constructor() InsertNode() OutputTree() initialises FirstNode to −1 and NumberNodes to 0 initialises each element in Tree to a Node object with the data value of −1 takes a Node object as a parameter, inserts it in the array and updates the pointer for its parent node outputs the left pointer, data and right pointer of each node in Tree Nodes cannot be deleted from this tree. (i) Write program code to declare the class TreeClass and its constructor. Do not declare the other methods. Use the appropriate constructor for your programming language. If you are writing in Python, include attribute declarations using comments. Save your program. Copy and paste the program code into part 2(b)(i) in the evidence document. [4] (ii) The method InsertNode()takes a Node object, NewNode, as a parameter and inserts it into the array Tree. InsertNode()first checks if the tree is empty. If the tree is empty, InsertNode(): • stores NewNode in the array Tree at index NumberNodes • increments NumberNodes • stores 0 in FirstNode. If the tree is not empty, InsertNode(): • stores NewNode in the array Tree at index NumberNodes • accesses the data in the array Tree at index FirstNode and compares it to the data in NewNode • repeatedly follows the pointers until the correct position for NewNode is found • once the position is found, InsertNode()sets the left or right pointer of its parent node • increments NumberNodes. Write program code for InsertNode(). Save your program. Copy and paste the program code into part 2(b)(ii) in the evidence document. [6] (iii) The method OutputTree()outputs the left pointer, the data and the right pointer for each node that has been inserted into the tree. The outputs are in the order they are saved in the array. If there are no nodes in the array, the procedure outputs ‘No nodes’. Write program code for OutputTree(). Save your program. Copy and paste the program code into part 2(b)(iii) in the evidence document. [4] (c) (i) The main program declares an instance of TreeClass with the identifier TheTree. Write program code for the main program. Save your program. Copy and paste the program code into part 2(c)(i) in the evidence document. [1] (ii) The main program inserts the following integers into the binary tree in the order given: 10 11 5 1 20 7 15 The main program then calls the method OutputTree(). Write program code to amend the main program. Save your program. Copy and paste the program code into part 2(c)(ii) in the evidence document. [4] (iii) Test your program. Take a screenshot of the output(s). Save your program. Copy and paste the screenshot into part 2(c)(iii) in the evidence document. [1]
Show mark scheme
2(a)(i)
Python
class Node():
def init (self, PData):
self. LeftPointer = -1 #int
self. Data = PData #int
self. RightPointer = -1 #int
2(a)(ii)
def GetRight(self):
return self. RightPointer
def GetData(self):
return self. Data
2(a)(iii)
self. RightPointer = NewRight
def SetData(self, NewData):
self. Data = NewData
2(b)(i)
For x = 0 To 19
Tree(x) = New Node(-1)
Next
End Sub
End Class
Python
class TreeClass():
def init (self):
self. Tree = [] #type node 20 spaces
self. FirstNode = -1 #int
self. NumberNodes = 0 #int
for x in range(20):
self. Tree.append(Node(-1))
2(b)(ii)
NodeAccess = self. Tree[NodeAccess].GetLeft()
Direction = "left"
elif NewNode.GetData() > self. Tree[NodeAccess].GetData():
NodeAccess = self. Tree[NodeAccess].GetRight()
Direction = "right"
if(Direction == "left"):
self. Tree[Previous].SetLeft(self. NumberNodes)
else:
self. Tree[Previous].SetRight(self. NumberNodes)
self. NumberNodes = self. NumberNodes + 1
2(b)(iii)
Python
def OutputTree(self):
if self. NumberNodes == 0:
print("No nodes")
else:
for x in range(0, self. NumberNodes):
print(self. Tree[x].GetLeft(), " ", self. Tree[x].GetData(), " ",self.
Tree[x].GetRight())
2(c)(i) [1 mark]
1 mark for
Instance of
TreeClass
created with identifier
TheTree
public static void main(String args[]){
TreeClass TheTree = new TreeClass();
VB.NET
Sub Main(args As String())
Dim TheTree As TreeClass = New TreeClass()
End Sub
Python
TheTree = TreeClass()
2(c)(ii)
Python
TheTree = TreeClass()
TheTree.InsertNode(Node(10))
TheTree.InsertNode(Node(11))
TheTree.InsertNode(Node(5))
TheTree.InsertNode(Node(1))
TheTree.InsertNode(Node(20))
TheTree.InsertNode(Node(7))
TheTree.InsertNode(Node(15))
TheTree.OutputTree()
2(c)(iii) [1 mark]
1 mark for correct output e.g.
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)