X351: Turn Bag to Set

Recall that a Set is an abstract data type somewhat similar to a Bag, they can store a finite collection of objects without any particular order. However, unlike a Bag, a Set cannot contain duplicates. The add() method for a Set is therefore very similar to the add() method for a Bag, with the additional requirement that it must first confirm that the item being added is unique. Write the member method addLikeASet that implements the following logic: When invoked the method takes the T parameter 'anEntry' and determines if 'anEntry' already exists in the bag. If the bag does not already contain 'anEntry' then the method attempts to add it to the bag, returning true when successful. If the bag already contained 'anEntry' the method does not attempt to add 'anEntry' and returns false. Your implementation code for this problem may NOT access/invoke any of the Bag API methods. (Your solution code may also include helper methods.

Reset

Practice a different Java exercise