Ask Question
2 October, 04:55

Reimplement StringSet with the exception that it should now extend ArrayList instead of encapsulating a String[]. You can easily abolish the 10 String limit for this new StringSet. You can also remove the int instance variable, as your class will no longer need it. Your existing StringSetTester should work with the new StringSet without being changed. Hint: What type of elements did the original StringSet store? What type of elements will be inserted into our ArrayList superclass? How can we tell Java that our ArrayList will be storing that type of element?

+2
Answers (1)
  1. 2 October, 06:24
    0
    Here is the program for the given question

    Explanation:

    class StringSet

    {

    ArrayList arraylist; / /a reference variable of ArrayList of generic type String

    //A no argument constructor.

    public StringSet ()

    {

    arraylist=new ArrayList (); / /instantiating the ArrayList object

    }

    //A mutator that adds a String newStr to the StringSet object.

    void add (String newStr)

    {

    arraylist. add (newStr); / / add (String) method to add string to the arraylist

    }

    //An accessor that returns the number of String objects that have been added to this StringSet object.

    int size ()

    {

    return arraylist. size (); / / size () method which gives the number of elements in the list

    }

    //An accessor that returns the total number of characters in all of the Strings that have been added to this StringSet object.

    int numChars ()

    {

    int sum = 0;

    for (String str:arraylist) / /for-each loop; can be read as for each string in arraylist

    {

    sum+=str. length ();

    }

    return sum;

    }

    //An accessor that returns the number of Strings in the StringSet object that have exactly len characters.

    int countStrings (int len)

    {

    int count = 0;

    for (String str:arraylist)

    {

    if (str. length () = = len)

    count++;

    }

    return count;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Reimplement StringSet with the exception that it should now extend ArrayList instead of encapsulating a String[]. You can easily abolish ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers