-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCount.java
More file actions
85 lines (73 loc) · 2.54 KB
/
Copy pathWordCount.java
File metadata and controls
85 lines (73 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.HashMap;
/**
* This class has a word list that is used to store the words. It provides some functionality such as to add
* a word to the list, count a number of time a word that is in the list, and remove all
* words in the list.
*/
public class WordCount {
//HashMap collection to store words
private HashMap<String, Integer> wordList;
/**
* This constructor initializes the wordList
*/
public WordCount() {
wordList = new HashMap<String, Integer>();
}
/**
* This constructor initializes the word list to the words in the string,
* separated by spaces.if String words is null or empty, initialize wordList
* to be empty HashMap.
* @param a String of words
*/
public WordCount(String words) {
if(words == null || words.isEmpty() ){
wordList = new HashMap<String, Integer>();
} else {
String[] spacer = words.split(" ");
wordList = new HashMap<>();
for (String token: spacer){
if(wordList.containsKey(token)){
int count = wordList.get(token);
wordList.replace(token, ++count);
} else{
wordList.put(token, 1);
}
}
}
}
/**
* This method adds a word to the list of words stored by the object. If
* String toAdd is null or empty, do not add to the word list.
* @param toAdd
*/
public void addWord(String toAdd) {
if(!(toAdd==null)){ // If the word is already present in the hashmap then increase its count
if(wordList.containsKey(toAdd)){
int count = wordList.get(toAdd);
wordList.replace(toAdd, ++count);
} else {
wordList.put(toAdd, 1);
}
}
}
/**
* This method removes all words from the list of words stored by the object
*/
public void startOver() {
wordList.clear();
}
/**
* This method returns an integer representing the number of times that a word is
* in the list of words stored by the object. If the String word is null or
* empty return 0
* @param the word to be checked for number of time that in the list of words
* @return the number of times a word is in the list
*/
public int countWord(String word) {
if(wordList.containsKey(word)){
return wordList.get(word);
} else {
return 0;
}
}
}