First question
The implementation code is as follows:
Package com.liu.ST2;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
public class Question1 {
public void Countword (String str) {
string[] Wordsarray = Str.split ("");
map<string, integer> wordsmap = new hashmap<string, integer> ();
for (String Word:wordsarray) {
if (Wordsmap.containskey (word)) {
Wordsmap.put (Word, wordsmap.get (word) + 1);
} else {
Wordsmap.put (Word, 1);
}
}
set<string> Setkey = Wordsmap.keyset ();
iterator<string> Itkey = Setkey.iterator ();
while (Itkey.hasnext ()) {
String Word = Itkey.next (). toString ();
int count = Wordsmap.get (word);
System.out.println ("word" + word + "occurrences" + Count + "Times");
}
}
}
The JUnit test code is as follows:
Package com.liu.ST2;
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Q1testtest {
@Test
public void Testcountword () {
Question1 q1 = new Question1 ();
Q1.countword ("Hello World My first Unit test test");
}
}
JUnit testing and Elcemma coverage are as follows:
Software Testing second job (1)