This quiz asks you to complete the two methods described below. Each item to be
ID: 3769871 • Letter: T
Question
This quiz asks you to complete the two methods described below. Each item to be filled in is marked by ?n?. Question n asks you to fill in ?n?. The intersect method should return a Set of Strings that contains the intersection of two given Sets of Strings. That is, set3 should contain a Set of Strings, each of which is in both setl and set2. public static Set intersect(Set setl, Set set2) { Set set3 = new TreeSet0; for (?3? element: set1) { if (set2.?4?(?5?)) { set3.?6?(?7?); } } return ?8?; } The countWords method should read all the words/tokens from the given Scanner and keep a count of how many times each word appears. It should return a Map with this information. public static Map countWords(Scanner scanner) { Map map = new HashMap0; while (scanner.?11 ?()) { String token = scanner.?12?(); if (map.containsKey(token)) { int count = ?13?.?14?(?15?); count++; map.put(?16?, ?17?); } else { map.put(?18?, ?19?); } } return ?20?; }Explanation / Answer
public static Set<String> intersect(Set<String> set1, Set<String> set2)
{
Set<String> set3 = new TreeSet<String>();
for(String element:set1)
{
if(set2.contains(element))
{
set3.add(element);
}
}
return set3;
}
public static Map<String, Integer> countWords(Scanner scanner)
{
Map<String, Integer> map = new HashMap<String, Integer>();
while(scanner.hasNext()){
String token = scanner.next();
if(map.containsKey(token)){
int count = map.get(token);
count++;
map.put(token,count);
}else{
map.put(token,1);
}
}
return map;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.