Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement a generic and sorted bag collection, called GenericSortedArrayBag, to maintain a sorted list of anything a) Define the collection class header so that the

implement a generic and sorted bag collection, called GenericSortedArrayBag, to maintain a sorted list of anything

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

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

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 class, then implement the following methods in your collection class:

1. insert: the insert method must insert elements in sorted order according to the compareTo 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 compareTo method. Moreover, according to compareTo, the items will be sorted either in ascending or descending order.

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.

3. 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 the array elements up to fill the gap that is created by the deleted item.

4. toString: this method returns a String representation of the collection. Note that, similar to compareTo and equals, it is up to the Thing class to format the string representation of one Thing.

5. countRange: this method takes two input parameters, start and end, both of them are of type E. The method then counts and returns how many elements in the collection fall in the range between start and end inclusive. If end < start then the method should return 0. Note that elements are compared according to the compareTo method.

6. replace: the replace method takes two inputs, oldThing and newThing, each of them is of type E. 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.

7. 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 null if index is beyond the size of the collection.

8. size: a method that returns the number of elements in the collection

9. sameContents: this method a one input parameters of type GenericSortedArrayBag. 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]

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions