Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class StringSet. A StringSet object is given a series of up to 10 String

ID: 3604918 • Letter: W

Question

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:

// an instance variable of type String[]

// an int instance variable that indicates the number of String objects that the StringSet currently contains

// a single no-argument constructor // a mutator that adds a String newStr to the StringSet object. If adding the new String to the String[] succeeds, the add method returns true.

// If adding the new String to the String[] fails (maybe the array is already full, for example), add returns false. boolean add(String newStr) // an accessor that returns the number of String objects that have been added to this StringSet object int size()

// an accessor that returns the total number of characters in all of the Strings that have been added to this StringSet object int numChars()

// an accessor that returns the number of Strings in the StringSet object that have exactly len characters int countStrings(int len)

Write a class StringSetTester that has a main method. It should ask the user for the number of Strings to add to a StringSet object. Afterward, use StringSet's size and numChars methods to print information about the collection of Strings entered. Also print the number of Strings that are exactly 5 and 7 characters long.

Hint: because Scanner's nextInt and nextLine process whitespace differently, you may want to use code similar to the following Scanner kybd = new Scanner(System.in); System.out.print("How many strings will you enter? "); int numStr = kybd.nextInt(); // stops after the number, leaves end of line or other whitespace kybd.nextLine(); // "eats" everything up to and including the next newline // the next kybd.nextLine() will read user input.

This is what I have so far:

public class StringSet {
   
   private StringSet [] array;
   private int numOfStrings;
  
        StringSet(){
           StringSet array = new StringSet();
        numOfStrings = 0;
        }
      public boolean add(String x) {
           int i = 0;
      
       if (i > 10) {
           for(i = 0; i > array.length; i++) {
               array[i] = String x;
           }
       }
      
             
         
      }
      
}

Explanation / Answer

StringSet.java

public class StringSet {

//Declaring instance variables

private String[] array;

private int numOfStrings;

//Zero argumented constructor

StringSet() {

array=new String[10];

numOfStrings = 0;

}

//This method will add the String to the String array

public boolean add(String x) {

if (numOfStrings < 10) {

array[numOfStrings] = x;

numOfStrings++;

} else if (numOfStrings == 0)

return false;

return true;

}

//This method will return the no of Strings in the Stirng array

public int size()

{

return numOfStrings;

}

//This method will return total no of characters in the String array

public int numChars()

{

int tot=0;

for(int i=0;i<array.length;i++)

{

tot+=array[i].length();

}

return tot;

}

//This method count no of String which are having length of user entered input

public int countStrings(int len)

{

int count=0;

for(int i=0;i<numOfStrings;i++)

{

if(array[i].length()==len)

{

count++;

}

}

return count;

}

}

__________________

StringSetTester.java

import java.util.Scanner;

public class StringSetTester {

public static void main(String[] args) {

// Declaring variables

int noOfStrings;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

int i = 0;

//Getting the input entered by the user

System.out.print("No of Strings you want to add :");

noOfStrings = sc.nextInt();

//Creating an StringSet class object

StringSet ss = new StringSet();

sc.nextLine();

//Adding user entered Strings to the String array

System.out.print("Enter String#" + (i + 1) + ":");

String line = sc.nextLine();

while (ss.add(line) && i < noOfStrings - 1) {

i++;

System.out.print("Enter String#" + (i + 1) + ":");

line = sc.nextLine();

}

//Displaying the results

System.out.println("The Size of StringSet is :" + ss.size());

System.out.println("The no of Strings that are exactly 5 and 7 characters :"+ (ss.countStrings(5) + ss.countStrings(7)));

}

}

________________

Output:

No of Strings you want to add :5
Enter String#1:Hello
Enter String#2:Pencil
Enter String#3:POwer
Enter String#4:Bible
Enter String#5:Cute
The Size of StringSet is :5
The no of Strings that are exactly 5 and 7 characters :3


_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote