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