(1) Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");
(2) Writing unit tests for testing;
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
Package Com.kai.demo;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
public class Test2 {
Private map<string, integer> Wordsmap;
Public Test2 (String strwords) {
Wordsmap = This.getarray (strwords);
}
Public map<string, integer> GetArray (String strwords) {
string[] Words_array = Strwords.split ("");
map<string, integer> words_map = new hashmap<string, integer> ();
int arrlength = Words_array.length;
for (int i = 0; i < arrlength; i++) {
if (!words_map.containskey (Words_array[i])) {
Words_map.put (Words_array[i], 1);
} else {
int currentnum = Words_map.get (Words_array[i]) + 1;
Words_map.remove (Words_array[i]);
Words_map.put (Words_array[i], currentnum);
}
}
return words_map;
}
public void Outputresult () {
Set s = wordsmap.keyset ();
Iterator i = S.iterator ();
while (I.hasnext ()) {
Object o = I.next ();
SYSTEM.OUT.PRINTLN (o + " appears " + wordsmap.get (o) + " times ");
}
}
}
Learning the use of unit tests and code coverage tools