標籤:
1. 寫一個Java程式,用於分析一個字串中各個單詞出現的頻率,並將單詞和它出現的頻率輸出顯示。(單詞之間用空格隔開,如“Hello World My First Unit Test”);
2. 編寫單元測試進行測試;
3. 用ElcEmma查看程式碼涵蓋範圍,要求覆蓋率達到100%。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class one {
public static void main(String[] args) {
String str = "Hello World My First Unit Test";
String[] items = str.split(" ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : items) {
if (map.containsKey(s))
map.put(s, map.get(s) + 1);
else {
map.put(s, 1);
}
}
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>();
for (Entry<String, Integer> entry : map.entrySet()) {
list.add(entry);
}
Collections.sort(list, new EntryComparator());
for (Entry<String, Integer> obj : list) {
System.out.println(obj.getKey() + " 出現的頻率為:" + obj.getValue());
}
}
}
class EntryComparator implements Comparator<Entry<String, Integer>> {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue() > o2.getValue() ? 0 : 1;
}
}
題目二:
1.寫一個Java程式,把一個英語句子中的單詞次序顛倒後輸出。例如輸入“how are you”,輸出“you are how”;
2.編寫單元測試進行測試;
3.用ElcEmma查看程式碼涵蓋範圍,要求覆蓋率達到100%
public class two {
public static void main(String[] args) {
System.out.println(reverse("how are you"));
}
public static String reverse(String str) {
String temp = "";
StringBuffer buf = new StringBuffer();
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
if (c == ‘ ‘) {
buf.append(temp);
buf.append(c);
temp = "";
} else {
temp = c + temp;
}
}
buf.append(temp);
return buf.toString();
}
}
閩江學院2015-2016學年下學期《軟體測試》課程-第二次作業(個人作業)