CSE 241/505Object Oriented ProgrammingFall 2018Homework # 8Generics and Collections in JavaDue date Jan 20rd 2019 (No late submission for this HW)As we discussed in the lectures, Java has a very structured Collections library. We can develop asimilar simplified library for Java that uses arrays underneath. We will implement some the interfacessome abstract class, some concrete classes, and some of the helper classes such as iterators.
The above figure shows a simplified version of the Collections. We will write corresponding genericclasses for Collection, Set, List, Queue, which are all interfaces. HashSet, ArrayList, and LinkedListare concrete classes. Note that LinkedList uses multiple inheritance. Following table defines thefunctions for each classClass Name Public Method Name DefinitionCollection This is a generic class with one generic parameters which is the generic type E.iterator()Returns an iterator over the collectionadd(E e)Ensures that this collection contains the specified elementaddAll(Collection c)Adds all of the elements in the specified collection to thiscollectionclear()Removes all of the elements from this collectioncontains(E e)Returns true if this collection contains the specified element.containsAll(Collection c)Returns true if this collection contains all of the elements in thespecified collection.isEmpty()Returns true if this collection contains no elements.remove(E e)Removes a single instance of the specified element from thiscollection, if it is presentremoveAll(Collection c)Removes all of this collections elements that are also containedin the specified collectionretainAll(Collection c)Retains only the elements in this collection that are contained in the specified collectionsize()Returns the number of elements in this collection.SetA collection that contains no duplicate elements. There is no order for this collection. In other words, you dont have to keep the insertion order of the elements.ListAn ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted.QueueQueues order elements in a FIFO (first-in-first-out) manner. There is no random access with this Collection. Some functions throw exceptions.add(E e)Inserts the specified element into this queueelement()Retrieves, but does not remove, the head of this queue.offer(E e)Inserts the specified element into this queuepoll()Retrieves and removes the head of this queue, or throws if this queue is empty.HashSetImplements Set functionsArrayListImplements List functionsLinkedListImplements both List and Queue functions. Your class does not have to have a linked list to implement these.IteratorhasNext()Returns true if the iteration has more elements.next()Returns the next element in the iteration and advances the iterator.remove()Removes from the underlying collection the last element returned by this iteratorYour Java Collections hierarchy should use only Java arrays in the concrete classes to implement all the methods.You will test each method of each concrete class with generic parameters of int and string.
Reviews
There are no reviews yet.