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

AH Computing Science 2018 Question 3 (CfE)

15 marks · Inheritance and polymorphism

Line 1 of the code begins the definition of the House Class. Using a programming language with which you are familiar, write the equivalent line of code for the ForSale Class. Using appropriate object-oriented terminology, explain the operation and effect of executing the following code. DECLARE saleHouse1 INITIALLY ForSale ("12 Albert Road", "Dundee", 2, "Well presented bungalow", 140000, 150000, "20/6/18", FALSE, FALSE) saleHouse1.updateBedrooms(3) Using appropriate object-oriented terminology, explain why the following statements would be invalid. SET saleHouse1.description TO "Well presented house with garden." saleHouse1.updateRentStatus(TRUE) As the program stores the details of over one million houses for sale, the management of the estate agency is keen to use this data to analyse trends in specific towns. Within the program, the house data is stored in an array of House objects called houseList and the name of the town to be analysed is stored in a variable called targetTown. Using a programming language with which you are familiar, write the code needed to calculate the average house value for a given town. Maintenance is needed to allow the program to store details of whether rental houses are furnished or unfurnished. Explain the type of maintenance that is needed. Explain how this additional feature could be implemented in this program. A program is being written for an estate agency to store the details of houses for sale or available to rent. A simplified version of the UML class diagram and the code used to define the House class is shown below. -address: String -town: String -bedrooms: Integer -description: String -houseValue: Integer -askingPrice: Real -closingDate: String -rentalCost: Real -deposit: Real -underOffer: Boolean -sold: Boolean -rentalLength: Integer -rented: Boolean +getValue() +getTown() +updateBedrooms() House ForSale ForRent +updateAskingPrice() +updateRentStatus() +updateSoldStatus() Line 1 CLASS House IS {STRING address, STRING town, INTEGER bedrooms, STRING description, INTEGER houseValue} Line 2 METHODS Line 3 CONSTRUCTOR (STRING address, STRING town, INTEGER bedrooms, STRING description, INTEGER houseValue) Line 4 DECLARE THIS.address INITIALLY address Line 5 DECLARE THIS.town INITIALLY town Line 6 DECLARE THIS.bedrooms INITIALLY bedrooms Line 7 DECLARE THIS.description INITIALLY description Line 8 DECLARE THIS.houseValue INITIALLY houseValue Line 9 END CONSTRUCTOR Line 10 PROCEDURE updateBedrooms (INTEGER noOfBedrooms) Line 11 SET THIS.bedrooms TO noOfBedrooms Line 12 END PROCEDURE Line 13 FUNCTION getTown() RETURNS STRING Line 14 RETURN THIS.town Line 15 END FUNCTION Line 16 FUNCTION getValue() RETURNS INTEGER Line 17 RETURN THIS.houseValue Line 18 END FUNCTION Line 19 END CLASS

Marking scheme

CLASS ForSale INHERITS House WITH {REAL askingPrice, STRING closingDate, BOOLEAN underOffer, BOOLEAN sold} 1 mark for an indication of inheritance. 1 mark for ‘WITH’ or equivalent to indicate additional properties. Instantiates an object (called saleHouse1) that belongs to the ForSale class (with given values/actual parameters). 1 mark each for any 2 of the following: • instantiates/create object • class or using values/parameters • constructor method invoked. The procedure call invokes the updateBedrooms method of the saleHouse1 object (with the value/actual parameter 3). The method updateBedrooms is inherited from House superclass. A value of 3 is assigned to the property bedrooms (in the saleHouse1 object). 1 mark each for any 2 of the following: • invoking method (or procedure) • use made of inheritance • updating property. Encapsulation has been used to restrict access to the description variable/property. This property can only be accessed through methods in ForSale class (or inherited methods from House class). 1 mark for encapsulation or use of private property. 1 mark for method required to use/change property. Since the updateRentStatus method is not part of saleHouse1 object. OR The updateRentStatus method is only available to objects that belong to the ForRent class. DECLARE total AS INTEGER INITIALLY 0 DECLARE average AS REAL INITIALLY 0.00 DECLARE count AS INTEGER INITIALLY 0 FOR EACH house FROM houseList DO IF house.getTown() = targetTown THEN SET total TO total + house.getValue() SET count TO count + 1 END IF END FOR EACH SET average TO total / count Alternative loop FOR house FROM 0 TO <end of list- 1> DO IF houseList[house].getTown() = targetTown THEN SET total TO total + houseList[house].getValue() SET count TO count + 1 END IF END FOR Award 1 mark each for any 4 of the following: • appropriate loop to traverse the array • conditional statement, using houseList array, needed to total correct town • correct use of getTown() method • correct total/average that makes use of a count • correct use of getValue() method. Perfective maintenance since implementing additional functionality. 1 mark for perfective maintenance with appropriate justification. NOTE: no marks for perfective without justification. New sub-class of ForRent class (or House class) with one additional property. OR A new property could be added to the ForRent class. 1 mark for new sub-class. OR 1 mark for new property in the ForRent class.

Previous question: AH Computing Science 2018 Question 2 (CfE)

Next question: AH Computing Science 2018 Question 4 (CfE)