====================================================================== Q1 ====================================================================== public class SetByInheritance extends Vector implements Set { public boolean isEmptySet() { // 5 marks return isEmpty(); } public boolean addItem(Object item) { // 9 marks if (indexOf(item) == -1) { add(item); return true; } else { return false; } } public boolean removeItem(Object item) { // 9 marks int index = indexOf(item); if (indexOf(item) == -1) { return false; } else { remove(index); return true; } } // if item is contained in Set, remove it and return true // otherwise return false public boolean containsItem(Object item) { // 7 marks return (indexOf(item) != -1); } public int cardinality() { // 5 marks return size(); } } ====================================================================== Q2 ====================================================================== a) output: 2.5 marks each 7 false true false false true false 43 true false true false b) Is the way equality testing has been implemented for classes A and B correct or not? If your answer is ``no'', then please explain your answer: Yes, it is correct. [5 marks] ====================================================================== Q3 [6 marks each] ====================================================================== (a) What is the main advantage of inheritance over composition? substitutability (sub-type property) (b) What are advantages of composition over inheritance? simple, makes all methods explicit, everything is specified locally easy to change underlying data-structure [ even dynamically ] (c) What is the difference between an abstract class and an interface? an abstract class may supply some code and data members in addition to method signatures; a subclass can only directly extend from one abstract class, but implement as many interfaces as you like (d) Why would it not make sense for a method in Java to be declared both {\tt abstract} and {\tt final}? abstract methods must be implemented in a non-abstract subclass, but you could not do that if the method was declared final as well. (e) What is the difference between the $==$ operator and the {\tt equals} method? == tests for object identity (two references to exactly the same memory location), whereas equals implements a notion of semantically equivalent objects