闽江学院2015-2016学年下学期《软件测试》课程-第二次作业(个人作业)

题目一:

1. 写一个Java程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);

2. 编写单元测试进行测试;

3. 用ElcEmma查看代码覆盖率,要求覆盖率达到100%。

package cn.he.mju;

import java.util.*;

 

public class Action {

public void findWord(String str){

String[] arrayWord =str.split(" ");

Action a = new Action();

a

.frequency(arrayWord);

}

public void frequency(String[] arrayWord){

String[] word = new String[arrayWord.length];

int time[]=new int[arrayWord.length];

boolean t = true;

for(int i=0;i<arrayWord.length;i++){

for(int j=0;j<arrayWord.length;j++){ if(arrayWord[i].equals(word[j])){

System.out.println("单词重复!!!");

t=false; }

}

if(t==true){

word[i]=arrayWord[i];

time[i]=1;

for(int j=i+1;j<arrayWord.length;j++){ if(arrayWord[i].equals(arrayWord[j])){

time[i]++;

}

}

}

}

for(int i = 0;i<arrayWord.length;i++){ if(word[i]!=null){

System.out.println("单词:"+word[i]+"出现了"+time[i]+"次。");

}

}

}

     }

package cn.he.mju;

public class Main {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

View v = new View();

}

}

package cn.he.mju;

import java.util.Scanner;

 

public class View {

public View(){

Scanner input =new Scanner(System.in);

System.out.println("请输入字符串,单词用空格隔开,回车结束:");

String str=input.nextLine();

Action a = new Action();

a.findWord(str);

}

}

题目二:

1.写一个Java程序,把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;

2.编写单元测试进行测试;

3.用ElcEmma查看代码覆盖率,要求覆盖率达到100%

package cn.he.mju;

 

import java.util.Scanner;

 

public class Main {

public static void main (String[] args){

Main a = new Main();

a.View();

}

public void findWord(String str){

String[] arrayWord =str.split(" ");

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

System.out.print(arrayWord[i]+" ");

}

}

public void View(){

Scanner input =new Scanner(System.in);

System.out.println("请输入字符串,单词用空格隔开,回车结束:");

String str=input.nextLine();

Main a = new Main();

a.findWord(str);

}

}

posted @ 2016-03-27 15:28  爱吃鱼の猫  阅读(100)  评论(0编辑  收藏  举报