View the step-by-step solution to:

Question

OBJECTIVE In this assignment, you are asked to implement a generic and sorted bag collection, called

GenericSortedArrayBag, to maintain a sorted list of anything. REQUIREMENTS Follow the following steps in order to implement your collection a) Define the collection class header so that the collection is a generic class that is restricted to store only objects that implements the Comparable interface. Basically, your class header should be as follows: class GenericSortedArrayBag E extends Comparable> b) For a Thing class to be inserted in your collection, the Thing class must include the following: 1. comapreto method that takes another Thing object as input returns either 0, 1, or- 1 based on whether the input is equal to, larger than, or smaller than the current thing. The Thing class must also implement the Comparable interface. Assume, for example, your thing is called circle, the class header will be as follows: class Circle implements Comparable<circle> 2. equals method that takes an Object parameter and returns true or false based on whether the input object is equal to the current thing or no. 3.toString method that returns a string representation of the thing.

c) Add the instance variables and constructor which are the same as the ArrayBag<E> class that we discussed in class, then implement the following methods in your collection class: 1. insert: the insert method must insert elements in sorted order according to the compare to method. Note that, it is not the responsibility of the collection class to decide on how to sort things. On the other hand, sorting is dependent on the Thing class's compare to method. Moreover, according to compare to, the items will be sorted either in ascending or descending order. Do not use the java Arrays's sort method. 2. countOccurrences: the countOccurrences method takes an input of type E. The method then uses the equals method to compare the current elements in the collection with the input element. This means that it is up to the Thing class to implement its own semantics of the equality. The method returns how many items in the collection are equal to the input object. delete: the delete method takes an input of type E and deletes one occurrence of E from the collection if any. Note that, this method must maintain the sorted order which means that just replacing the deleted item with the last element in the array will not work anymore because this may break the sorted order. On the other hand, you should move the array elements up to fill the gap that is created by the deleted item. toString: this method returns a String representation of the collection. Note that, similar to compare to and equals, it is up to the Thing class to format the string representation of one thing. 5. replace: the replace method takes two inputs, oldthing and new thing, each of them is of type . The method then searches the collection for an element that is equal to oldthing. If found, the method replaces (only one occurrence of oldthing with newThing while maintaining the sorted order. On the other hand, if oldthing is not found in the collection, the method does not do anything. The method then returns true or false based on whether a replacement took place or no. grab: a method that takes an integer, called index, as input and returns as output the element that is stored at position index in the collection or nullifindex is beyond the size of the collection 7. Size: a method that returns the number of elements in the collection 8. sameContents: this method a one input parameters of type GenericsortedArrayBag<E>. The method then returns true if the input bag includes the same elements as the current bag even if the count of each element may be different from one bag to the other. For example, the method should return true for the following two bags of integers because both (1,1,2,2,3] and [1,2,3)

9. Make your collection class implements the Iterable interface by adding an Iterator inner class and implementing the iterator() method as discussed in class. To test this part, write a for-each loop to iterate over all items in your collection.


Note: Use an array instead of linked list.

The thing will be CAR OR Pets.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes