Topic One:
1. Write a Java 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. Write unit tests for testing;
3. Use Elcemma to view code coverage, which requires coverage of up to 100%.
Demo class:
1 ImportJava.util.HashMap;2 ImportJava.util.Iterator;3 ImportJava.util.Map;4 ImportJava.util.Set;5 6 Public classDemo {7 8 Public voidCountword (String str) {9string[] Wordsarray = Str.split ("");Tenmap<string, integer> wordsmap =NewHashmap<string, integer>(); One for(String word:wordsarray) { A - if(Wordsmap.containskey (Word)) { -Wordsmap.put (Word, wordsmap.get (word) + 1); the}Else { -Wordsmap.put (Word, 1); - } - } + -Set<string> Setkey =Wordsmap.keyset (); +Iterator<string> Itkey =setkey.iterator (); A while(Itkey.hasnext ()) { atString Word =Itkey.next (). toString (); - intCount =wordsmap.get (word); - -System.out.println ("word" + word + "occurrences" + Count + "times"); - - } in } - to}
JUnit Unit Test class:
1 Import Staticorg.junit.assert.*;2 Importorg.junit.Test;3 4 Public classDemotest {5 6 @Test7 Public voidTestcountword () {8Demo Demo =NewDemo ();9Demo.countword ("Hello World hello Java Hello Test");Ten } One A}
Test:
Eclemma Code Coverage:
Software Testing the second job-write a Java program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears.