2004w06.exam

2004w06.exam - Question 1 [22 marks] We wish to write a...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Question 1 [22 marks] We wish to write a class that will maintain a resizable list of objects that are sorted in ascending order. We want to re-use ArrayList , but have to decide whether to use inheritance or composition. In a real implementation, SortedList would implement List with over 15 methods. We will focus on the three methods shown in the class diagrams. You may add any variables or methods that you need. /** void add(Object o) * Inserts the object o in the list such that the elements in the list remain in ascending * order; ie. if o is stored at index i, then the object stored at i-1 is less or equal to o * whereas the object stored at i+1 is greater than o. * Hint: This is not a question on Iterators. Use the index to traverse the list. * @throws ClassCastException: If the object o does not implement Comparable */ /** void remove(int i) * Removes the element at index i * @throws IndexOutOfBoundsException: If the index i is negative or > the list’s size */ a) [10 marks] Write the complete implementation of the SortedList class using inheritance . For each of the three methods shown, you must choose whether to inherit (don’t write the method) or override (write the method). ArrayList // API on back page SortedList +add(o:Object) +remove(i:int) +equals(o:Object):boolean
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
b) [8 marks] Now, re-write the complete implementation of the SortedList class using composition . ArrayList // API on back page SortedList -contents:ArrayList +add(o:Object) +remove(i:int) +equals(o:Object):boolean
Background image of page 2
[4 marks] Is inheritance or composition a better choice for the SortedList ? Explain your reasons by discussing the strengths and weaknesses of the two approaches Question 2 [21 marks] a) [3 marks] In the HashMap class, three methods are provided to support iteration : public Set entrySet() public Set keySet() public Collection values() Using your knowledge of HashMap and the Collection framework, explain why the first two return a Set while the third returns a Collection ?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
b) [5 marks] A LoginDatabase uses a HashMap to maintain a database of {username,password} for all registered users. The key is the username, the value is the password. Below, PW is used as an abbreviation for password. Write the implementation of the reset () method. /** Resets the password stored for the given username to be newPW. * @throws IllegalAccessException if the oldPW does not match the one currently
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

2004w06.exam - Question 1 [22 marks] We wish to write a...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online