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.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.