StringSetjava Can you please help me the JAVA program Earli

StringSet.java

Can you please help me the JAVA program?

------------------------------------------------------

Earlier we wrote a class StringSet. That program\'s original specification is included for convenience:

Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series.

The StringSet class has the following specification:

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.

------------------------------------------------------

Here is OLD requirement. https://www.chegg.com/homework-help/questions-and-answers/stringsetjava-please-help-java-program--please-show-output-also-q15958409

------------------------------------------------------

Here is the code from OLD requirement:

class StringSet
{
//An instance variable of type String[]
String[] set;
//An int instance variable that indicates the number of String objects that the StringSet currently contains.
int numOfStrings;
//A no argument constructor.
public StringSet()
{
numOfStrings = 0;
set = new String[10];
}
//A mutator that adds a String newStr to the StringSet object.
void add(String newStr)
{
set[numOfStrings++] = newStr;
}
//An accessor that returns the number of String objects that have been added to this StringSet object.
int size()
{
return numOfStrings;
}
//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(int i = 0; i < numOfStrings; i++)
sum += set[i].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(int i = 0; i < numOfStrings; i++)
if(set[i].length() == len)
count++;
return count;
}
}

And the code for StringSetTester.java is:

import java.util.*;
class StringSetTester
{
public static void main(String[] args)
{
Scanner kybd = new Scanner(System.in);
System.out.print(\"How many strings will you enter? \");
int numStr = kybd.nextInt();
kybd.nextLine();
StringSet ss = new StringSet();
for(int i = 0; i < numStr; i++)
{
System.out.print(\"Enter string \" + (i+1) + \": \");
String temp = kybd.nextLine();
ss.add(temp);
}
System.out.println(\"The size of the StringSet is: \" + ss.size());
System.out.println(\"The number of characters in StringSet is: \" + ss.numChars());
System.out.println(\"The number of strings of length 5 are: \" + ss.countStrings(5));
System.out.println(\"The number of strings of length 7 are: \" + ss.countStrings(7));
}
}

Solution

Below is the code implementing ArrayList. There is no need to change anything in the StringSetTester.java file.

StringSet.java

class StringSet
{

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

//A no argument constructor.
public StringSet()
{
arraylist=new ArrayList<String>(); //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;
}
}

StringSet.java Can you please help me the JAVA program? ------------------------------------------------------ Earlier we wrote a class StringSet. That program\
StringSet.java Can you please help me the JAVA program? ------------------------------------------------------ Earlier we wrote a class StringSet. That program\
StringSet.java Can you please help me the JAVA program? ------------------------------------------------------ Earlier we wrote a class StringSet. That program\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site