I need help with my java homework can someone help me Write

I need help with my java homework, can someone help me?

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://a single 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 void 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, query the user for each String (use Scanner\'s nextLine method), and afterward use StringSet\'s add, 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.

Solution

Please follow the code and comments for description :

CODE :

a) StringSet.java :

import java.util.Arrays; // required imports
import java.util.Scanner;

public class StringSet { // class to run the code

public static int count; // instance variables
public static String[] strData = new String[10];

public StringSet() { // constructor no arg
this.count = count;
}

public void add(String newStr) { // method to add a new data
strData[count++] = newStr; // add the data
count = count++; // update the count
}

public int size() { // method to return the size of the array
return count;
}

public int numCharacters() { // method to count the total characters
int charCount = 0;
for (int j = 0; j < count; j++) { // iterate over the loop
String elementData = strData[j];
charCount = charCount + elementData.toCharArray().length;
}
return charCount; // return the data
}

public int countStrings(int len) { // method to return the selected count

int matchCount = 0;
for (int x = 0; x < count; x++) {
String eleData = strData[x];
if (len == eleData.toCharArray().length) {
matchCount++;
} else {
continue;
}
}
return matchCount;
}
}


b) StringSetTester.java :

import static abc.StringSet.count; // required imports to get the instance variables, this can be done even by passing the data to the constructor
import static abc.StringSet.strData;
import java.util.Scanner;

public class StringSetTester { // class code

public static void main(String[] args) { // driver method

Scanner sc = new Scanner(System.in); // scanner class to run the code
StringSet ss = new StringSet(); // object for the super class
System.out.print(\"Please Enter the Number of String Objects You would like to Enter : \"); // prompt for the user
count = sc.nextInt(); // get the data
sc.nextLine();

for (int i = 0; i < count; i++) { // iterate and save the data
System.out.print(\"Enter the Element \" + (i + 1) + \" : \");
String data = sc.nextLine();
strData[i] = data;
}

System.out.println(\"\ Adding a new String to the StringSet Object...\"); // prompt for the user
System.out.println(\"Enter the New String to be Updated : \");
String newSt = sc.nextLine(); // get the data
ss.add(newSt); // call the method

System.out.println(\"\ Calculating the Size of the Array...\"); // prompt for the user
System.out.println(\"The Size of the Array is : \" + ss.size()); // message

System.out.println(\"\ Calculating the Total Number of Characters of the Array...\"); // prompt for the user
System.out.println(\"The Total Number of Character of the Array is : \" + ss.numCharacters()); // result

System.out.println(\"\ Calculating the String Elements with 5 Characters : \"); // prompt for the user
System.out.println(\"The Total Number of Strings with Exactly 5 Characters is/are : \" + ss.countStrings(5)); // result

System.out.println(\"\ Calculating the String Elements with 7 Characters : \"); // prompt for the user
System.out.println(\"The Total Number of Strings with Exactly 7 Characters is/are : \" + ss.countStrings(7)); // result
}
}

OUTPUT :

Please Enter the Number of String Objects You would like to Enter : 5
Enter the Element 1 : James
Enter the Element 2 : Faulker
Enter the Element 3 : Marsh
Enter the Element 4 : Mallows
Enter the Element 5 : Hide

Adding a new String to the StringSet Object...
Enter the New String to be Updated :
Allow

Calculating the Size of the Array...
The Size of the Array is : 6

Calculating the Total Number of Characters of the Array...
The Total Number of Character of the Array is : 33

Calculating the String Elements with 5 Characters :
The Total Number of Strings with Exactly 5 Characters is/are : 3

Calculating the String Elements with 7 Characters :
The Total Number of Strings with Exactly 7 Characters is/are : 2


Hope this is helpful.

I need help with my java homework, can someone help me? Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores thes
I need help with my java homework, can someone help me? Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores thes
I need help with my java homework, can someone help me? Write a class StringSet. A StringSet object is given a series of up to 10 String objects. It stores thes

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site