第一题

package cn.wu.com;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
private Map<String, Integer> wordsMap;
public WordFreq(String strWords) {
wordsMap = this.getArray(strWords);
}
public Map<String, Integer> getArray(String strWords) {
String[] words_Array = strWords.split(" ");
Map<String, Integer> words_Map = new HashMap<String, Integer>();

int arrlength = words_Array.length;
for (int i = 0; i < arrlength; i++) {
if (!words_Map.containsKey(words_Array[i])) {
words_Map.put(words_Array[i], 1);
} else {

}
}
return words_Map;
}

public void OutputResult() {
Set s = wordsMap.keySet();
Iterator i = s.iterator();
while (i.hasNext()) {
Object o = i.next();
System.out.println(o + "出现了" + wordsMap.get(o)+"次");
}
}
}

单元测试方法:
package cn.wu.com;

import static org.junit.Assert.*;

import org.junit.Test;


public class WordFreqTest {

	@Test
	public void test() {
	String strWords = "Hello World My First Unit Test";
	WordFreq test = new WordFreq(strWords);
	test.OutputResult();
	}
}

 

 

第二题

 

package cn.wu.com.j;

import java.util.Scanner;

public class Test2{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("请输入要测试的字符串:");
String str = input.nextLine();
Test2.test(str);
}
public static void test(String str){

String[] strArr = str.split("\\s+|[,]");

StringBuffer result = new StringBuffer();

for(int i = strArr.length -1;i >=0; i--){

result.append(strArr[i] + " ");


}

result.setCharAt(str.length()-0, (char) 0);

System.out.println("颠倒顺序后的字符串:: "+result.toString());

}
}
单元测试方法:
package cn.wu.com.j;

import org.junit.After;
import org.junit.Test;

public class Test2Test {

@Test
public void test() throws Exception {
String str="how are you";
Test2.test(str);
}

}