标签:
(1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
(2)编写单元测试进行测试;
(3)用ElcEmma查看代码覆盖率,要求覆盖达到100%。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class WordFreq {
private Map<String,Integer> wordsMap;
public WordFreq(String strWords){
wordsMap=this.getArray(strWords);
}
private Map<String, Integer> getArray(String strWords) {
// TODO Auto-generated method stub
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);
}
}
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+"出现了"+wordsMap.get(o)+"次");
}
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class WordFreqTest {
@Test
public void test() {
WordFreq word=new WordFreq("Hello World My First Unit Test");
word.OutputResult();
}
}
标签:
原文地址:http://www.cnblogs.com/daniaoguai/p/5518173.html