ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions

AH Computing Science 2025 Question 3

13 marks · Inheritance and polymorphism

Explain why the Order class does not need a constructor method. Using appropriate object-oriented terminology, explain why the calcOrderTotal() method appears in both the Order and Delivery classes. The UML class diagram for the ordering system is repeated below. Order OrderItem - orderNumber: integer - orderItems[]: array of OrderItem - orderStatus: string - orderTotal: real - numberItems: integer - itemName: string - itemCost: real + addItem() + updateOrderStatus() + calcOrderTotal() + getOrderTotal() + getOrderNumber() + OrderItem() + getItemCost() + setItemName() + setItemCost() SitIn Delivery Collection - numCust: integer - orderTime: string - orderTime: string - collectTime: string - tableNum: integer - driver: string - staffName: string - deliveryCharge: real + SitIn() + addExtraItem() + Delivery() + calcOrderTotal() + Collection() The following code is used during ongoing testing to check whether the system is working correctly. Line 1 DECLARE order1 INITIALLY Delivery (1, [<array of OrderItem>], "In Progress", 0.0, 4, "18:07", "Jemma", 3.50) Line 2 DECLARE order2 INITIALLY SitIn (2, [<array of OrderItem>], "In Progress", 0.0, 5, 2, 7, "Max") Line 3 DECLARE order3 INITIALLY Collection (3, [<array of OrderItem>], "In Progress", 0.0, 7, "18:11", "18:31") Line 4 DECLARE orderList AS ARRAY OF Order INITIALLY [order1, order2, order3] Line 5 SEND order2.orderStatus TO display Line 6 orderList[1].addExtraItem("Ice cream", 4.99) At line 4, an array of Order objects is created. Explain how it is possible for this array to store several different types of objects. Explain why line 5 cannot be executed. Line 6 has been written to add an extra item to an existing order. Explain why line 6 cannot be executed. Describe one way that an additional item could be successfully added to orderList[1]. Your answer should refer to the variable declarations used during ongoing testing. At the end of each day, the owners want to view details of the three most expensive orders for that day. Sample data in the orderList array that is processed at the end of a day is shown below. orderNumber orderItems orderStatus orderTotal numberItems … … … … … 34 < array of OrderItem > Complete 45.97 6 35 < array of OrderItem > Complete 56.87 8 36 < array of OrderItem > Complete 23.34 3 37 < array of OrderItem > Complete 73.29 9 … … … … … Using pseudocode, design the procedure bubbleSort(orderList). This procedure will be used to sort the orderList array and display the orderNumber and orderTotal of the three most expensive orders in the sorted array. Your procedure should make use of the bubble sort algorithm. FlavourFusion Cafe is creating an ordering system. Customers can make sit-in, delivery and collection orders. The ordering system will be implemented using an object-oriented programming language. The following constraints will apply to the system: • An order can contain a maximum of 50 items from the cafe menu. • All items for delivery and collection orders must be added at the time an order is made. • Extra items can be added to sit-in orders during the meal — for example, adding a dessert or a drink. • All orders for delivery will have a delivery charge. A simplified UML Class diagram of the system is shown below. Order OrderItem - orderNumber: integer - orderItems[]: array of OrderItem - orderStatus: string - orderTotal: real - numberItems: integer - itemName: string - itemCost: real + addItem() + updateOrderStatus() + calcOrderTotal() + getOrderTotal() + getOrderNumber() + OrderItem() + getItemCost() + setItemName() + setItemCost() SitIn Delivery Collection - numCust: integer - orderTime: string - orderTime: string - collectTime: string - tableNum: integer - driver: string - staffName: string - deliveryCharge: real + SitIn() + addExtraItem() + Delivery() + calcOrderTotal() + Collection() Some of the code used to implement the ordering system is shown on the page opposite. CLASS Order { INTEGER orderNumber, ARRAY OF OrderItem orderItems[], STRING orderStatus, REAL orderTotal, INTEGER numberItems } METHODS … PROCEDURE calcOrderTotal() SET THIS.orderTotal TO 0 FOR i FROM 0 TO THIS.numberItems −1 DO SET THIS.orderTotal TO THIS.orderTotal + THIS.orderItems[i].getItemCost() END FOR END PROCEDURE … END CLASS CLASS SitIn INHERITS Order WITH { INTEGER numCust, INTEGER tableNum, STRING staffName } METHODS CONSTRUCTOR (INTEGER howMany, INTEGER table, STRING staff ) SET THIS.orderNumber TO <next order number> SET THIS.orderItems[0] TO <empty object of type OrderItem> SET THIS.orderStatus TO "In Progress" SET THIS.orderTotal TO 0.00 SET THIS.numberItems TO 0 SET THIS.numCust TO howMany SET THIS.tableNum TO table SET THIS.staffName TO staff END CONSTRUCTOR PROCEDURE addExtraItem(extraName, extraCost) SET THIS.numberItems TO THIS.numberItems + 1 THIS.orderItems[THIS.numberItems].setItemName(extraName) THIS.orderItems[THIS.numberItems].setItemCost(extraCost) END PROCEDURE END CLASS

Marking scheme

• all orders received must be one of the three types: SitIn, Delivery or Collection, which each have a constructor method • there would never be a need to create an Order object Award 1 mark for all orders being one of the subclass types. Accept also Order is an abstract class. Do not accept Order is superclass. • the calcTotalOrder() method in the Delivery class overrides the inherited method from the Order class • this is because the method in the Delivery class will add functionality to the inherited class to allow for the delivery charge to be added Award 1 mark for use of override. Award 1 mark for additional functionality in context. • this is possible because of polymorphism • the array is of type Order which is a superclass and can store references to objects that belong to any of its subclasses since they are based on the Order class Award 1 mark for polymorphism. Award 1 mark for subclass/ superclass connection. The error is because the orderStatus property is private due to encapsulation. A separate getOrderStatus() method would be needed. Award 1 mark for private property that is encapsulated. Accept a (getter) method is needed to access the data. • although order2 is an object of the SitIn class, in this situation, it is being indexed as an element of the orderList array which is of type Order • as the Order class does not have an addExtraItem() method, the subclass method is not accessible Award 1 mark for class of objectList[1] being Order object not a SitIn object. Award 1 mark for Order class not being able to access addExtraItem() method belonging to the SitIn subclass. Use the code order2.addExtraItem() to add an item to the order so that objectList[1] is updated. Award 1 mark for description of a suitable solution. See sample solution below. Award 1 mark for inner (fixed) and outer (conditional) loops correct (lines 2, 10 and 4 of the solution below). Award 1 mark for sorting in descending order indicating correct use of getters (line 5). Award 1 mark for swap objects (line 6). Award 1 mark for displaying top three after the sort, indicating correct use of getters (lines 11 & 12). Note: if candidate has sorted in ascending order and displays the last three elements in descending order, maximum of 4 marks can still be awarded. 1. procedure bubbleSort(orderList[]) 2. start conditional loop 3. 4. 5. 6. 7. 8. 9. 10. end conditional loop when swapped = false set swapped = false start fixed loop from 0 to length of orderList[] – 2 if orderList[loop].getOrderTotal < orderList[loop + 1].getOrderTotal then swap the two order objects set swapped = true end if end fixed loop 11 start fixed loop from 0 to 2 12. display orderList[loop].getOrderNmber and orderList[loop].getOrderTotal 13. end fixed loop 14. end procedure

Previous question: AH Computing Science 2025 Question 2

Next question: AH Computing Science 2025 Question 4