• 软件测试:(实验二)


     

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

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

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

    package com.czh;

    import java.io.BufferedReader;

    import java.io.FileReader;

    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.Comparator;

    import java.util.List;

    import java.util.Map;

    import java.util.TreeMap;

    import java.util.regex.Matcher;

    import java.util.regex.Pattern;

    public class Test1 {

    public static void main(String[] args) throws Exception {

    long time1 = System.currentTimeMillis();

    BufferedReader reader = new BufferedReader(new FileReader(

    "D:\wordtest.txt"));

    StringBuffer buffer = new StringBuffer();

    String line = null;

    while ((line = reader.readLine()) != null) {

    buffer.append(line);

    }

    reader.close();

    Pattern expression = Pattern.compile("[a-zA-Z]+");

    String string = buffer.toString();

    Matcher matcher = expression.matcher(string);

    Map<String, Integer> map = new TreeMap<String, Integer>();

    String word = "";

    int times = 0;

    while (matcher.find()) {// 是否匹配单词

    word = matcher.group();

    if (map.containsKey(word)) {// 如果包含该键,单词出现过

    times = map.get(word);

    map.put(word, times + 1);

    else {

    map.put(word, 1);// 否则单词第一次出现,添加到映射中

    }

    }

    List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(

    map.entrySet());

    Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {

    public int compare(Map.Entry<String, Integer> left,

    Map.Entry<String, Integer> right) {

    return (left.getValue()).compareTo(right.getValue());

    }

    };

    Collections.sort(list, comparator);

    int last = list.size() - 1;

    try{

    for (int i = last; i > last-10; i--) {

    String key = list.get(i).getKey();

    Integer value = list.get(i).getValue();

    System.out.print("Top"+i+" : ");

    System.out.println(key + " " + value);

    }

    }catch(Exception e){

    //System.out.println("");

    }

    long time2 = System.currentTimeMillis();

    System.out.print("耗时:");

    System.out.println(time2 - time1+"ms");

    }

    }

    二、

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

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

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

    package com.czh;

    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();

    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());

    }

    }

  • 相关阅读:
    JVM系列六(自定义插入式注解器).
    JVM系列五(Javac 字节码编译器).
    2019 — 求不得,放不下
    Mybatis 条件判断单双引号解析问题
    JVM系列四(对象分配策略).
    JVM系列三(垃圾收集器).
    Spring MVC -- Spring Tool Suite和Maven(安装Tomcat、JDK)
    Spring MVC -- 单元测试和集成测试
    Spring MVC -- 下载文件
    Spring MVC -- 上传文件
  • 原文地址:https://www.cnblogs.com/qq429314399/p/5523757.html
Copyright © 2020-2023  润新知