Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Problem 1 ( 35 points): Bag Client Write a client class Consonantspilter that filters out all consonants from a bag containing letters. To accomplish
1. Problem 1 ( 35 points): Bag Client Write a client class Consonantspilter that filters out all consonants from a bag containing letters. To accomplish this: a. Create three bags that hold strings. One bag is named letters and contains several one-letter strings. Another bag is named vowels and contains five strings, one for each vowel. The third bag is empty and is named consonants. i. Declare the bags of type Baginterface and use the ArrayBag class as an implementation class, e.g.: BagInterface letters = new ArrayBag (); b. One at a time i. Remove a string from the bag letters. ii. Check whether the string is in the bag vowels. Hint: look for an appropriate method in BagInterface that you can use. iii. If it is not a vowel, place it into the bag consonants. c. After you have checked all of the strings in the bag letters, report the number of consonants in the bag consonants. 2. Problem 2 (35 points): Baq Implementation: In mathematics, the concepts of bags and sets are closely related. The difference is that bags allow duplicate items, while a set does not. An operation that removes the duplicates from a bag would be helpful if one wished to implement a set. Write a method in the ArrayBag class that removes the dublicates of all items in a bag: public void removeDuplicates () \{... Write also a client class TestarrayBag to test the removeDublicates () method. Hint: You can solve Problem 2 in different ways. Here are 2 suggestions: Variant 1: In order to remove the duplicates from the bag you can use a pair of nested loops. Both loops scan the bag array (an instance variable in the ArrayBag class): 1. Write an outer loop to visit the position of each item in the array bag. 2. The inner loop shall remove the duplicates that come after the currently considered element. You can use a while loop to scan over the remaining items in the array: if the index of the current. element is 'i', remove the duplicates from position ' i+1 ' to the last element saved in the array. a. To remove an item, use the same technique we used for the modification of the remove() method: i. Copy the last item over the item to be removed. ii. Replace the last item with null. Variant 2: Use a second array (e.g. bag2), where you copy only the elements from the bag array that do not appear already there. Here is a scheleton of the method: public vold remoneDuplicates() \{ @SuppressWamings("unchecked') T] bag2 = (T]) new Object[numberOtEntries]; /f unchecked cast int sizeBag2 =0; /I number of elements in bag2 II First loop: visit each element of the array bag II Inner loop: check if the current item from bag is already in the second array, bag 2, II and if not, store it in bag2 I/ Save back the elements of bag2 in the original array (instance variable) bag \} What to turn in: The classes Consonantsfilter.java, ArrayBag-java, and
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started