(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) Viewing code coverage with Elcemma, requiring coverage of up to 100%
Package Com.czh;
import Java.io.BufferedReader;
import Java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import Java.util.Map;
import Java.util.TreeMap;
import Java.util.regex.Matcher;
import Java.util.regex.Pattern;
Public class Test1 {
Public Static void Main (string[] args) throws Exception {
long time1 = System. Currenttimemillis ();
BufferedReader reader = new bufferedreader (new FileReader (
"D:\\wordtest.txt"));
StringBuffer buffer = new stringbuffer ();
String line = null;
while (line = Reader.readline ()) = null) {
Buffer.append (line);
}
Reader.close ();
Pattern expression = pattern. Compile ("[a-za-z]+");
String string = Buffer.tostring ();
Matcher Matcher = Expression.matcher (string);
map<string, integer> map = new treemap<string, integer> ();
String word = "";
int times = 0;
while (Matcher.find ()) {//whether the word matches
Word = Matcher.group ();
if (Map.containskey (Word)) {//If the key is included, the word has occurred
Times = Map.get (word);
Map.put (Word, times + 1);
} Else {
Map.put (Word, 1);//Otherwise the first occurrence of the word, added to the map
}
}
list<map.entry<string, integer>> list = new arraylist<map.entry<string, integer>> (
Map.entryset ());
comparator<map.entry<string, integer>> Comparator = new comparator<map.entry<string, Integer>> () {
Public int Compare (map.entry<string, integer> left,
Map.entry<string, integer> right) {
return (Left.getvalue ()). CompareTo (Right.getvalue ());
}
};
Collections. Sort (list, comparator);
int last = list.size ()-1;
Try {
for (int i = last; i > last-10; i--) {
String key = List.get (i). GetKey ();
Integer value = List.get (i). GetValue ();
System. out. Print ("Top" +i+ ":");
System. out. println (key + "" + value);
}
}catch(Exception e) {
System.out.println ("");
}
long time2 = System. Currenttimemillis ();
System. out. Print ("Time-consuming:");
System. out. println (time2-time1+ "MS");
}
}
Two
(1) The order of words in an English sentence is reversed and then output. For example, enter "How IS is", output "you is how";
(2) Writing unit tests for testing;
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
Package Com.czh;
import Java.util.Scanner;
Public class test2{
Public Static void Main (string[] args) {
Scanner input = new Scanner (System. in);
System. out. Print ("Please enter English:");
String str = input.nextline ();
string[] Strarr = Str.split ("\\s+|[,]");
StringBuffer result = new stringbuffer ();
for (int i = strarr.length-1;i >=0; i--) {
Result.append (Strarr[i] + "");
}
Result.setcharat (Str.length ()-0, (char) 0);
System. out. println ("Reversed order results are:" +result.tostring ());
}
}
Software Testing: (Experiment II)