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

This class should be called Program1 and not be in a package. Using java.util.Tr

ID: 3704432 • Letter: T

Question

This class should be called Program1 and not be in a package. Using java.util.TreeSet, build aSet of unique String values such that, when iterating over the contents of the Set, the values are ordered by the number of words contained in the String. (The definition of word is: whatever a Scanner’s next() method returns when using the default delimiter). If two strings have the same length, print them in lexicographical order (the default for String objects). To accomplish this, you will need to pass a custom Comparator instance to your TreeSet constructor (it’s easy – look it up!). Make sure to implement this Comparator using an inner class, such that the entirety of Program1 is in a single java file. Demonstrate your set with String values of your choosing. This program does not need to be interactive. Make sure to show what happens when duplicate strings and multiple strings with the same lengths are included. Here’s my main method. My custom Comparator is called WordCountComp. I defined it earlier in the same java file.

Explanation / Answer

Below is your code: - -

public class Program1 {

// inner class

// Make it private

private static class WordCountComp implements Comparator<String> {

@Override

public int compare(String s1, String s2) {

if (s1.split("\s+").length > s2.split("\s+").length)

return 1;

else if (s1.split("\s+").length < s2.split("\s+").length)

return -1;

else

return s1.compareTo(s2);

}

}

public static void main(String[] args) {

Set<String> aSet = new TreeSet<>(new WordCountComp());

// 12 words

aSet.add("Lorem Ipsum is simply dummy text of the printing and typesetting industry");

// 31 words

aSet.add(

"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.");

// 17 words

aSet.add(

"It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.");

// 16 words

aSet.add(

"It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.");

// duplicate sentence

aSet.add("Lorem Ipsum is simply dummy text of the printing and typesetting industry");

// 12 words String of same length as first, hence would be sorted

// lexicographically..

aSet.add("Ipsum Ipsum is complex dummy text of the printing and typesetting industry");

for (String str : aSet) {

System.out.println(str);

}

}

}

Output

Ipsum Ipsum is complex dummy text of the printing and typesetting industry
Lorem Ipsum is simply dummy text of the printing and typesetting industry
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.