• 软件测试实验二


    1.(1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。

     (2)编写单元测试进行测试;

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

    import java.util.HashMap;

    import java.util.Iterator;

    import java.util.Map;  

    public class one

    {

         private Map<String, Integer> wordsMap;  

        public one(String strWords)

       {

        wordsMap=this.getArray(strWords);

       }

    public Map<String,Integer> getArray(String strWords){  String[] wordsArray = strWords.split("\s");  

     Map<String, Integer> wordsMap=new HashMap<String, Integer>();

     int arrLength=wordsArray.length;  int currentNum=0;

     for(int i=0;i<arrLength;i++)

    {

      if("".equals(wordsArray[i].trim()))

    {  

    continue;

     }

     if(!wordsMap.containsKey(wordsArray[i]))

     {      wordsMap.put(wordsArray[i],1);  }

     else {

    currentNum=wordsMap.get(wordsArray[i])+1;  

    wordsMap.remove(wordsArray[i]);  

    wordsMap.put(wordsArray[i],currentNum);  }  

    }     

       return wordsMap;

     }  

    public void outputResult(){

     Iterator iterator=wordsMap.entrySet().iterator();

     while(iterator.hasNext())

    { Map.Entry entry=(Map.Entry)iterator.next();  

    System.out.println(entry.getKey()+"出现了"+entry.getValue());     }

     }  

    }

    --------------------------------------------------------------------------------------------------------------------------

    import static org.junit.Assert.*;

    import org.junit.Test;

    public class oneTest {

     public void test() {  

      String str="Whatever is worth doing is worth doing well";  

      one wordFreq=new one(str);  

       wordFreq.outputResult();  

     }

    }

    2.

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

    (2)编写单元测试进行测试;

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

    public class two {  

     public String Bee(String str) {  

    String[] wordsArray = str.split("\s");  

    StringBuffer result = new StringBuffer();

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

    {

         if("".equals(wordsArray[i].trim()))

        {  

       continue;

        }

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

        }

     return result.toString();  

       }

    }

    ----------------------------------------------------------------------------------------

    public class two {  

     public String Bee(String str) {

     String[] wordsArray = str.split("\s");  

    StringBuffer result = new StringBuffer();

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

    {

    if("".equals(wordsArray[i].trim()))

    {  

        continue;

     }  

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

    }

        return result.toString();  }

    }

  • 相关阅读:
    muduo 库解析之四:TimeZone
    muduo 库解析之三:Date
    muduo 库解析之九:Condition
    muduo 库解析之八:Mutex
    muduo 库解析之七:Atomic
    [linux]查看文件目录是否为硬链接
    [go] 解决:concurrent write to websocket connection
    [windows] 保存浏览器网页长截图
    [项目] 系统迁移的几个注意点
    [MySQL] order by field 自定义排序
  • 原文地址:https://www.cnblogs.com/Darlingtuan/p/5365843.html
Copyright © 2020-2023  润新知