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

AH Computing Science 2022 Question 4

14 marks · Inheritance and polymorphism

Describe how inheritance affects the Plane class. Initial values may be set using a constructor method. Name the constructor method for either of the superclasses shown on the UML class diagram. The first line of code in the class declaration for the Aircraft class is provided below. CLASS Aircraft IS { STRING aircraftID, REAL hoursSinceService, REAL hoursToNextService } Using a programming language of your choice, write the equivalent code for the Helicopter class. The nextService() methods of the Aircraft and Glider classes are shown below. Aircraft class FUNCTION nextService () RETURNS REAL SET THIS.hoursToNextService = 100 – THIS.hoursSinceService RETURN THIS.hoursToNextService END FUNCTION Glider class OVERRIDE FUNCTION nextService () RETURNS REAL SET THIS.hoursToNextService = 250 – THIS.hoursSinceService RETURN THIS.hoursToNextService END FUNCTION Using appropriate object‑oriented terminology, explain the use made of the OVERRIDE statement in the nextService() method of the Glider class. A program makes use of the classes in the UML class diagram. Some of the code from this program is shown below. Line 54 DECLARE plane1 INITIALLY Plane ("ABC123", 0.0, 100.0, 28.9, "jet") … … Line 81 DECLARE fleet AS ARRAY OF Plane INITIALLY [NULL] * 76 Line 82 SET fleet[0] TO plane1 … … Line 93 DECLARE numberPlanes INITIALLY countServiceDue(fleet) Line 94 SEND numberPlanes & " are due a service" TO DISPLAY Using appropriate object‑oriented terminology, explain the purpose of Line 54 of this program. Using appropriate object‑oriented terminology, explain the purpose of Line 81 and Line 82 of this program. The countServiceDue() function called at Line 93 is used to count the number of planes in the fleet that return a value of no more than 12 when the nextService() function is applied. Using an object‑oriented language of your choice, write the code for the countServiceDue() function. An object‑oriented program is written to store and process aircraft details. Different types of aircraft include fixed wing planes, helicopters and gliders. Fixed wing planes and helicopters are serviced every 100 flight hours whereas gliders are serviced every 250 flight hours. A simplified version of the UML class diagram for the program is shown below. Aircraft ‑aircraftID: String ‑hoursSinceService: Real ‑hoursToNextService: Real +Aircraft() +nextService() +updateHours() Helicopter FixedWing ‑wingSpan: Real ‑numberRotors: Integer ‑engineType: String +FixedWing() +Helicopter() Plane Glider ‑engineType: String ‑launchType: String +Plane() +Glider() +nextService()

Marking scheme

The Plane class will inherit the properties/instance variables and the methods from both the Aircraft class and the FixedWing class. 1 mark for properties/instance variables and methods 1 mark for Aircraft and FixedWing classes Aircraft() Or FixedWing() Accept FixedWing() and Aircraft() (b) CLASS Helicopter INHERITS Aircraft WITH { INTEGER numberRotors, STRING engineType } 1 mark for indicating inheritance of superclass 1 mark for additional parameters with correct data types Note: solutions written in other languages are acceptable The OVERRRIDE statement is used to redefine/modify the inherited method. This means that the calculation performed by the Glider class would be different from the calculation inherited from the Aircraft superclass. This is an example of polymorphism. 1 mark each for any two statements A new object of the Plane class has been instantiated and populated with a value for each instance variable. The instance variables of the plane1 object include its own property in addition to those inherited from the FixedWing and Aircraft classes. 1 mark for instantiation of an object 1 mark for explaining or exemplifying the inheritance of instance variables from super classes Note: accept the following as an alternative to an explanation of inherited instance variables: aircraftID = “ABC123” hoursSinceService = 0.0 hoursToNextService = 100.0 wingSpan = 28.9 engineType = “jet” Declaration of array of objects called fleet with 76 elements that belongs to the Plane class. Assignment of values stored in plane1 object to element 0 of the fleet array. 1 mark for declaration of an array of objects with 76 elements 1 mark for assignment of plane1 object to the first element of the fleet array FUNCTION countServiceDue (ARRAY OF Plane group) RETURNS INTEGER DECLARE totalPlanes INITIALLY 0 FOR index FROM 0 TO 75 DO IF group[index].nextService() <=12 THEN SET totalPlanes TO totalPlanes + 1 END IF END FOR RETURN totalPlanes END FUNCTION 1 mark for initialising, incrementing and returning a local variable such as totalPlanes 1 mark for correct use of fleet array of objects (formal parameter group in this solution) within a fixed loop 1 mark for correct use of nextService() method

Previous question: AH Computing Science 2022 Question 3

Next question: AH Computing Science 2022 Question 5