码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试实验二

时间:2016-03-27 23:43:36      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

2,

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

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

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

            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 Demo {

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

 

            long time1 = System.currentTimeMillis();

            BufferedReader reader = new BufferedReader(new FileReader(

            "D:\\ZMX.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);// 否则单词第一次出现,添加到映射中

            }

            }

            /*

            * 核心:如何按照TreeMap 的value排序而不是key排序.将Map.Entry放在集合里,重写比较器,在用

            * Collections.sort(list, comparator);进行排序

            */

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

            }

}

技术分享

 

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

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

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

            import java.util.Scanner;

            public class NewDemo {

            public static void main(String [] a){

            Scanner in=new Scanner(System.in);

            while(true){

            String s=in.nextLine();

            if(s.equalsIgnoreCase("quit")){

            System.exit(0);

            }

            String[] ss=s.split("\\s+");

            StringBuffer sb=new StringBuffer();

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

            sb.append(ss[i]+" ");

            }

            System.out.println(sb);

            }

            }

}

技术分享 

 

软件测试实验二

标签:

原文地址:http://www.cnblogs.com/Maxine-zmx/p/5327265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!