Minjiang College School of the next 2015-2016 years software testing Course-second assignment (personal assignment) first question

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%.

Code:

Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Scanner;


public class Test1 {
Private hashmap< String, Integer > dictionary;
private int wordscount;
Public Test1 () {
Dictionary = new hashmap< String, Integer > ();
Wordscount = 0;
}
public void Insert (String word) {
if (Dictionary.containskey (word)) {
int currentcount = Dictionary.get (word);
Dictionary.put (Word, currentcount + 1);
} else {
Dictionary.put (Word, 1);
}
wordscount++;
}

public int Getdifferentwordsnum () {
return Dictionary.size ();
}

public int Getallwordsnum () {
return wordscount;
}

public void Displaydictionary () {
For (iterator< String > it = Dictionary.keyset (). Iterator (); It.hasnext ();) {
String key = It.next ();
System.out.print (key);
System.out.print (":");
System.out.println (Dictionary.get (key));
}
}
public static void Main (string[] args) throws Exception {
String passage = "Hello World My first Unit Test";
Scanner Scanner = new Scanner (passage);
Test1 dict = new Test1 ();
while (Scanner.hasnextline ()) {
String line =scanner.nextline ();
Boolean isblankline = Line.matches ("\\w") | | Line.length () = = 0;
if (isblankline) {
Continue
}
string[] Words = Line.split ("\\w");
for (String word:words) {
if (Word.length ()! = 0) {
Dict.insert (word);
}
}
}
Dict.displaydictionary ();
}
}

Run: Code Coverage:

Minjiang College School of the next 2015-2016 years software testing Course-second assignment (personal assignment) first question

Related Article

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.