Projects When you create or modify classes in the following projects, be sure to test your changes by running an appropr
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Projects When you create or modify classes in the following projects, be sure to test your changes by running an appropr
6. A sorted set behaves just like a set, but allows the user to visit its items in ascend- ing order with a for loop, and supports a logarithmic search for an item. Draw a class diagram that shows where you would place a new class for sorted sets in the collection framework shown in Figure 6-4. 7. Complete the new class for sorted sets. 8. Someone notices that the remove operation performs two searches of a bag: one during the test of the method's precondition (using the in operator) and the other to locate the position of the target item to actually remove it. One way to eliminate the redundant search is to track the position of the target item in an instance variable. In the case of an array-based bag, this position would be -1 at startup and whenever a target item is not found. If the in operator finds a target item, the position variable is set to that item's index in the array; otherwise, it is reset to -1. After the remove method checks its precondition, no search loop is necessary; the method can just close the hole in the array lising the position vari- able. Modify the ArrayBag class to support this capability. Note that you will now have to add a contains method to ArrayBag that performs this customized search. 9. The modified remove method of Project 8 no longer works correctly for a sorted bag. The reason for this is that the contains method in ArrayšortedBag does not update the new position variable in ArrayBag. Nlodify the method ArraySortedBay. contains so that the remove tellivu wonksuurteouly for sorted bags. 10. The remove method in the LinkedBag class has the redundant search described in Project 8. Modify this class so that thus redundancy no longer exists.