1. (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");
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
public class Demo {
public void Wordscount (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 + "appears" + Count + "Times");
}
}
}
(2) Writing unit tests for testing;
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Demotest {
@Test
public void Test () {
Demo Demo = new Demo ();
Demo.wordscount ("Hello This is A test test");
}
}
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
2. (1) The order of words in an English sentence is reversed and then output. For example, enter "How IS is", output "you is how";
public class Demo {
public void Upsidedown (String str) {
string[] Wordsarray = Str.split ("");
System.out.print ("Reverse word output:");
for (int i = wordsarray.length-1; I >= 0; i--) {
System.out.print (Wordsarray[i] + "");
}
}
}
(2) Writing unit tests for testing;
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Demotest {
@Test
public void Test () {
Demo Demo = new Demo ();
Demo.upsidedown ("Hello This is A test test");
}
}
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
"Software Test" experiment two unit test