ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
13 marks · Classes and objects
The main program contains the following code. Line 1 DECLARE post1 INITIALLY Post('Ally', 'Loving this beautiful day at the beach!') Line 2 <create an array of objects called posts> Line 3 DECLARE posts AS ARRAY OF Post INITIALLY [] * 5 Line 4 SET posts[0] TO post1 Line 5 posts[0].addLike() Line 6 posts[0].addShare() Line 7 posts[0].addLike() Line 8 posts[0].addShare() Line 9 posts[0].addLike() Line 10 posts[0].displayPost() Using appropriate object‑oriented terminology, describe the effect of the code at line 1. Write the output produced by line 10. The code to implement video uploads is shown below. CLASS VideoPost INHERITS Post WITH {STRING videoFilename, INTEGER duration, INTEGER views} METHODS CONSTRUCTOR (STRING creator, STRING postText, STRING videoFilename, INTEGER duration) DECLARE THIS.videoFilename INITIALLY videoFilename DECLARE THIS.duration INITIALLY duration DECLARE THIS.views INITIALLY 0 DECLARE THIS.creator INITIALLY creator DECLARE THIS.postText INITIALLY postText DECLARE THIS.likes INITIALLY 0 DECLARE THIS.shares INITIALLY 0 DECLARE THIS.comments INITIALLY [] END CONSTRUCTOR PROCEDURE addView() SET THIS.views TO THIS.views + 1 END PROCEDURE OVERRIDE PROCEDURE displayPost() SEND "Video posted by " & THIS.creator TO DISPLAY SEND "Text: " & THIS.postText TO DISPLAY <code used to display video content> SEND "Duration: " & THIS.duration & "s" TO DISPLAY SEND "Views: " & THIS.views TO DISPLAY SEND "Likes: " & THIS.likes TO DISPLAY SEND "Shares: " & THIS.shares TO DISPLAY SEND "Comments: " TO DISPLAY IF length(THIS.comments) = 0 THEN SEND "No user comments yet." TO DISPLAY ELSE FOR EACH comment FROM THIS.comments DO SEND comment TO DISPLAY END FOR END IF END PROCEDURE END CLASS The countInteractions() method inherited from the Post class does not take into account the number of times a video has been viewed. Using a programming language of your choice, write the countInteractions() method for the VideoPost class. The programmer uses the code below to create a video post and add it in the posts array of objects from part (a). Line 1 DECLARE post2 INITIALLY VideoPost('Charlie', 'Watch this amazing trick!', 'myVideo.mp4', 30) Line 2 SET posts[1] to post2 Line 3 posts[1].addView() Explain why the code in line 3 would generate an error. The programmer uses the insertion sort algorithm to sort the contents of the array of objects posts in descending order of the number of user interactions. This will allow the most engaging content to be displayed at the top of users’ feeds. The design for the insertion sort algorithm used is shown below. 1. start fixed loop i from 1 to (length of posts array) − 1 2. set temp object = posts(i) 3. set j = i 4. while j > 0 and (number of interactions for posts(j−1) < number of interactions for temp object) 5. set posts(j) = posts(j−1) 6. set j = j − 1 7. end loop 8. set posts(j) = temp object 9. end loop Data associated with the five objects of the posts array is shown below. Index posts[index].creator posts[index].countInteractions() 0 Ally 834 1 Charlie 923 2 Jude 1228 3 Sophie 846 4 Priya 791 The comparisons made when the insertion sort is applied to the array of objects posts to sort contents in descending order of number of interactions are shown in the table below. State the missing content of cells A, B and C. Iteration Comparison temp.creator Move Insertion 1 834 < 923 Charlie Object at index 0 copied to index 1 Temp object inserted at index 0 2 A Jude Object at index 1 copied to index 2 2 923 < 1228 Jude B Temp object inserted at index 0 3 834 < 846 Sophie Object at index 2 copied to index 3 3 923 < 846 Sophie No move needed C 4 834 < 791 Pryia No move needed Temp object inserted at index 4 As the social media app becomes more popular, the programmer realises that there is a need for a new Creator class to allow creators to make an account. The original UML class diagram is shown below. Post - creator: String - postText: String - likes: Integer - shares: Integer - comments[]: array of String + Post() + addComment() + addLike() + addShare() + countInteractions() + displayPost() VideoPost Image - photoFilename: String - videoFilename: String - duration: Integer - views: Integer + Image() + displayPost() + VideoPost() + addView() + countInteractions() + displayPost() The programmer would like to store a creator’s name, date of birth, e‑mail address and references to each post that the creator makes. A creator should be able to add, edit and delete posts. Copy and complete the partially updated UML class diagram below for the new Creator class. Creator Post - postText: String - likes: Integer - shares: Integer - comments[]: array of String + Post() + addComment() + addLike() + addShare() + countInteractions() + displayPost() A programmer is developing a social media app using object‑oriented programming. The app will allow a creator to post text, videos and images and allow other users to interact with the posts through comments, likes, shares and video views. Posts with more interactions appear in more user feeds. A simplified UML class diagram for the program is shown below. Post - creator: String - postText: String - likes: Integer - shares: Integer - comments[]: array of String + Post() + addComment() + addLike() + addShare() + countInteractions() + displayPost() VideoPost Image - photoFilename: String - videoFilename: String - duration: Integer - views: Integer + Image() + displayPost() + VideoPost() + addView() + countInteractions() + displayPost() Some of the code used to implement the social media app is shown below. CLASS Post IS {STRING creator, STRING postText, INTEGER likes, INTEGER shares, ARRAY OF STRING comments} METHODS CONSTRUCTOR (STRING creator, STRING postText) DECLARE THIS.creator INITIALLY creator DECLARE THIS.postText INITIALLY postText DECLARE THIS.likes INITIALLY 0 DECLARE THIS.shares INITIALLY 0 DECLARE THIS.comments INITIALLY [] END CONSTRUCTOR PROCEDURE addLike() SET THIS.likes TO THIS.likes + 1 END PROCEDURE PROCEDURE addShare() SET THIS.shares TO THIS.shares + 1 END PROCEDURE FUNCTION countInteractions() RETURNS INTEGER DECLARE interactions INITIALLY 0 SET interactions TO length(THIS.comments)+ THIS.likes + THIS.shares RETURN interactions END FUNCTION … PROCEDURE displayPost() SEND "Post by " & THIS.creator TO DISPLAY SEND "Text: " & THIS.postText TO DISPLAY SEND "Likes: " & THIS.likes TO DISPLAY SEND "Shares: " & THIS.shares TO DISPLAY SEND "Comments: " TO DISPLAY IF length(THIS.comments) = 0 THEN SEND "No user comments yet." TO DISPLAY ELSE FOR EACH comment FROM THIS.comments DO SEND comment TO DISPLAY END FOR END IF END PROCEDURE END CLASS
• this statement creates an instance of the Post class called post1 • the constructor of the Post class then assigns the following values to each of the instance variables of the post1 object: Award 1 mark for instantiation of the object post1 of the Post class. Award 1 mark for the correct assignment of all instance variables. Creator = Ally Text = Loving this beautiful day at the beach Likes = 0 Shares = 0 Comments = null • post by Ally text: Loving this beautiful day at the beach • likes: 3 shares: 2 • comments: No user comments yet Award 1 mark for the correct row headings. Award 1 mark for the correct number of likes and shares. Award 1 mark for No user comments yet. See sample solution below. Award 1 mark for correct calculation as part of a function. OVERRIDE FUNCTION countInteractions() RETURNS INTEGER DECLARE interactions INITIALLY 0 SET interactions TO THIS.likes + THIS.shares + length (THIS.comments) + THIS.views RETURN interactions END FUNCTION • posts is an array of objects of type Post. Due to polymorphism, posts can contain objects of type Post, Image or VideoPost • posts[1] points to variable post2 which is an instance of the VideoPost class, however the array posts has been defined as type Post. As a result, the only methods that can be accessed are those in the Post superclass • the addView() method is only associated with the VideoPost subclass so an error will be generated when any of the array elements try to access it Award 1 mark for each bullet. Maximum 2 marks. • A: 834 < 1228 • B: Object at index 0 copied to index 1 • C: Temp object inserted at index 2 See sample solution below. Award 1 mark for correct properties, types and encapsulation. Award 1 mark for correct methods, including constructor.