X670: Turn Bag to Set

For this assignment, create a method that will turn a fixed-size arrayBag into a set.

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, and return true.
  • If the bag already contained anEntry the method does not attempt to add anEntry and returns false.

Again, this arrayBag is of a fixed size. It will not have the expandCapacity method available. Once the bag is full, nothing else can be added. Remember to review the Bags module, specifically the add() method implementation for Bags as it will be helpful here.

Your implementation code for this problem may NOT access/invoke any of the Bag API methods but, since it is a member method, your code may access the fields numberOfEntries (an int) and contents (an array of items in the bag). Your solution code may also include helper methods.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.