ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
13 marks · Inheritance and polymorphism
With reference to the UML class diagram and code, explain the use made of overriding. The following code appears within the main program that controls the app. DECLARE r1 AS Rectangle ("white", "black", 20, 16) Using appropriate object-oriented terminology, explain the effect of this statement. State the output that would be produced by the following code: SEND "The area is " & r1.getArea() TO DISPLAY r1.calcArea() SEND "The area is " & r1.getArea() TO DISPLAY A young child uses the app to create a simple image consisting of a house, a tree and a wagon. The final image is shown below. Within the app, an array of Shape objects called list is used to keep track of the individual shapes used in the image: Index Object referenced 0 r1 1 r2 2 r3 3 r4 4 t1 5 r5 6 c1 7 r6 8 c2 9 c3 Where used in the image House building Window left Window right Door Roof Tree trunk Tree top Wagon Wheel left Wheel right Describe the effect of the following statement: list[4].setFillColour("red") Explain why the following statement generates an error: list[8].getCircumference() The statement sort(list) in the main program invokes the procedure sort() and passes the actual parameter list. This procedure makes use of the bubble sort algorithm to arrange the contents of the variable list into descending order of area. The design and data flow for this procedure is shown below. 1. procedure sort(array) 2. set n = number of shapes in the array 3. set swapped = true 4. start while loop IN: array 5. set swapped = false 6. start fixed loop for i from 0 to n−2 7. if area of array(i) and array(i+1) are in the wrong order 8. swap contents of array(i) and array(i+1) 9. set swapped = true 10. end if 11. end fixed loop 12. set n = n−1 13. end conditional loop 14. end procedure Using a programming language of your choice, write code to implement the following steps of the design: Step 4 Step 7 Step 8 Object-oriented code is used to create a drawing app for children. The simplified UML class diagram below represents the classes used in the app. - fillColour: string - lineColour: string - area: integer + draw() + setFillColour() + setLineColour() + calcArea() + getArea() Triangle Rectangle Circle - height: integer - base: integer - length: integer - breadth: integer - radius: integer + Triangle() + Rectangle() + Circle() + calcArea() + calcArea() + calcArea() + getPerimeter() + getPerimeter() + getCircumference() Shape Some of the code used to implement the drawing app is provided below. CLASS Shape { STRING fillColour, STRING lineColour, INTEGER area} METHODS CONSTRUCTOR ( STRING fill, STRING line ) DECLARE THIS.fillColour TO fill DECLARE THIS.lineColour TO line DECLARE THIS.area TO 0 END CONSTRUCTOR … PROCEDURE setFillColour ( STRING colour ) SET THIS.fillColour TO colour END PROCEDURE PROCEDURE calcArea() SEND "The area is the space enclosed within the boundary of the shape" TO DISPLAY END PROCEDURE FUNCTION getArea() RETURNS INTEGER RETURN THIS.area END FUNCTION END CLASS CLASS Triangle INHERITS Shape WITH { INTEGER height, INTEGER base } METHODS … OVERRIDE PROCEDURE calcArea() SET THIS.area TO THIS.height * THIS.base / 2 END PROCEDURE … END CLASS CLASS Rectangle INHERITS Shape WITH { INTEGER length, INTEGER breadth } METHODS CONSTRUCTOR Rectangle ( STRING fill, STRING line, INTEGER length, INTEGER breadth ) DECLARE THIS.fillColour TO fill DECLARE THIS.lineColour TO line DECLARE THIS.length TO length DECLARE THIS.breadth TO breadth DECLARE THIS.area TO 0 END CONSTRUCTOR OVERRIDE PROCEDURE calcArea() SET THIS.area TO THIS.length * THIS.breadth END PROCEDURE … END CLASS CLASS Circle INHERITS Shape WITH { INTEGER radius } METHODS CONSTRUCTOR Circle ( STRING fill, STRING line, INTEGER radius ) DECLARE THIS.fillColour TO fill DECLARE THIS.lineColour TO line DECLARE THIS.radius TO radius DECLARE THIS.area TO 0 END CONSTRUCTOR OVERRIDE PROCEDURE calcArea() SET THIS.area TO 3.14 * THIS.radius² END PROCEDURE … END CLASS
Overriding is used to define the calcArea() method for each of the 3 subclasses of the Shape superclass (Triangle, Rectangle and Circle) and replace the code within the method inherited from the superclass. In this way, each of the subclasses can respond to the calcArea() method in a way that is unique to that subclass. 1 mark for use of overriding to replace inherited method. 1 mark for use of overriding to create unique responses for each subclass. This statement creates an instance of the Rectangle class called r1. The constructor of the Rectangle class then assigns the following values to each of the instance variables of the r1 object: fillColour = white lineColour = black base = 20 height = 16 area = 0 1 mark for instantiation of the object r1 of the Rectangle class 1 mark for correct assignment of all instance variables. The area is 0 The area is 320 1 mark for area = 0 1 mark for area = 320 The fill colour of the roof is set to red. Accepts also: The fill colour of object t1 is set to red. The object list is an array of objects of type Shape. Due to polymorphism, list can contain objects of type Shape or of any of its subclass types. Although the list[8] references the variable c2 which is an instance of the Circle class, the variable list has been defined as a Shape object and acts as Shape type container for each object it references. As a result, the only methods that can be accessed by elements of the array are those that are known to the Shape superclass. Since the getCircumference() method is only associated with the Circle subclass, an error will be generated when any of the array elements attempt to invoke it. 1 mark each for a maximum of 2 marks: • list is a polymorphic array of objects of type Shape • list[8] references an object belonging to the Circle class • the variable list acts as a Shape type container for the objects it references • the getCircumference() method is not accessible to Shape class objects WHILE swapped AND n>=0 DO Also accept: WHILE swapped = TRUE AND n>=0 DO IF array[i].getArea() < array[i+1].getArea() THEN 1 mark for correct use of getArea() method 1 mark for correct comparison. Also accept the following: • IF array[i+1] > array[i} THEN SET temp TO array[i] SET array[i] TO array[i+1] SET array [i+1] TO temp Also accept: SET temp TO array[i+1] SET array[i+1] TO array[i] SET array [i] TO temp