I am coding in blueJ and what I am trying to do is this:
1.a) Create a getWordSet()
method in WordGroup
which:
- takes another
WordGroup
object as a parameter - creates
a HashSet<String>
- uses two for loops to put all the words from this and the parameter
WordGroup
into theHashSet
- returns the
HashSet<String>
1.b) In the main
method:
- use the
getWordSet()
method using the twoWordGroup
s - iterate or loop over the
HashSet
returned and print the words from it
2.a) Create a method in the WordGroup
called getWordCounts()
which:
- creates a
HashMap<String, Integer>
- loops over all the words returned by
getWordArray()
and puts each word into theHashMap
with the number of times it occurs - returns
HashMap<String, Integer>
2.b) In the main
method:
- call
getWordCounts()
on the twoWordGroup
s - use
keySet()
to retrieve the set of keys (the String part of the mapping) - loop over this set and print out the word and its count for both
WordGroup
s - use the
getWordSet()
method to make complete set of all the words from bothWordGroup
s - loop over the new
HashSet
to print a complete list of all words with the sum counts from each of theHashMap
s
My code so far:
public class Main{
public static void main(String[] args){
WordGroup wordgroupOne= new WordGroup ("You can discover more about a person in an hour of play than in a year of conversation");
WordGroup wordgroupTwo= new WordGroup ( "When you play play hard when you work dont play at all");
String[] quoteOne = wordgroupOne.getWordArray();
String[] quoteTwo = wordgroupTwo.getWordArray();
for (String words : quoteOne){
System.out.println(words);
}
for (String words : quoteTwo){
System.out.println(words);
}
}
}
WordGroup class:
import java.util.HashSet;
import java.util.HashMap;
public class WordGroup {
public String words;
public WordGroup (String getWords){
words = getWords.toLowerCase();
}
public String[] getWordArray(){
return words.split(" ");
}
public HashSet<String> getWordSet(){
HashSet<String> set = new HashSet<String>();
for (String words : quoteOne){
words.add(word);
}
return words;
}
public HashMap<String, Integer> getWordCounts() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String words : words) {
words.add(word);
}
return HashMap<String, Integer>;
}
}
I have got this far and now I am stuck. I cannot figure out how to get the words from teh array into the hashset and hashmap and how to return them in the desired form. p.s. sorry about the wierd question layout- the string kept disappearing after hashset if it was not in the code format)