1.(1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。
(2)编写单元测试进行测试;
(3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class one
{
private Map<String, Integer> wordsMap;
public one(String strWords)
{
wordsMap=this.getArray(strWords);
}
public Map<String,Integer> getArray(String strWords){ String[] wordsArray = strWords.split("\s");
Map<String, Integer> wordsMap=new HashMap<String, Integer>();
int arrLength=wordsArray.length; int currentNum=0;
for(int i=0;i<arrLength;i++)
{
if("".equals(wordsArray[i].trim()))
{
continue;
}
if(!wordsMap.containsKey(wordsArray[i]))
{ wordsMap.put(wordsArray[i],1); }
else {
currentNum=wordsMap.get(wordsArray[i])+1;
wordsMap.remove(wordsArray[i]);
wordsMap.put(wordsArray[i],currentNum); }
}
return wordsMap;
}
public void outputResult(){
Iterator iterator=wordsMap.entrySet().iterator();
while(iterator.hasNext())
{ Map.Entry entry=(Map.Entry)iterator.next();
System.out.println(entry.getKey()+"出现了"+entry.getValue()); }
}
}
--------------------------------------------------------------------------------------------------------------------------
import static org.junit.Assert.*;
import org.junit.Test;
public class oneTest {
public void test() {
String str="Whatever is worth doing is worth doing well";
one wordFreq=new one(str);
wordFreq.outputResult();
}
}
2.
(1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
(2)编写单元测试进行测试;
(3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%
public class two {
public String Bee(String str) {
String[] wordsArray = str.split("\s");
StringBuffer result = new StringBuffer();
for(int i = wordsArray.length -1;i >=0; i--)
{
if("".equals(wordsArray[i].trim()))
{
continue;
}
result.append(wordsArray[i] + " ");
}
return result.toString();
}
}
----------------------------------------------------------------------------------------
public class two {
public String Bee(String str) {
String[] wordsArray = str.split("\s");
StringBuffer result = new StringBuffer();
for(int i = wordsArray.length -1;i >=0; i--)
{
if("".equals(wordsArray[i].trim()))
{
continue;
}
result.append(wordsArray[i] + " ");
}
return result.toString(); }
}