软工个人作业2

1.码云项目地址

项目地址

2.PSP表格

|PSP2.1 | 个人开发流程 | 个人开发流程 | 实际耗费时间(分钟) |
| -------- | -----: | :----: |:----: | :----: |
| Planning | 计划 | 30 | 50 |
| Estimate | 明确需求和其他相关因素,估计每个阶段的时间成本 | 50 | 70 |
| Development | 开发 | 650 | 700 |
| Analysis | 需求分析 (包括学习新技术) | 500 | 650 |
| Design Spec | 生成设计文档 | 100 | 150 |
| Design Review | 设计复审 | 60 | 70 |
| Coding Standard | 代码规范 | 50 | 30 |
| Design | 具体设计 | 200 | 160 |
| Coding | 具体编码 | 180 | 200 |
| Code Review | 代码复审 | 60 | 50 |
| Test | 测试(自我测试,修改代码,提交修改) | 60 | 190 |
| Reporting | 报告 | 100 | 190 |
| · | 测试报告 | 40 | 40 |
| · | 计算工作量 | 60 | 60 |
| · | 并提出过程改进计划 | 50 | 30 |

3.解题思路描述

  1. 编程语言:JAVA

  2. 具体思路:对文件进行按行读取,对每一行进行循环遍历当遇到英文单词以及特殊字符时将其放入map中,放入之前要判断是否存在,若存在则将其出现的次数加一,不存在则将入,将其存在次数置为1。

4.设计实现过程

1 . 相关类

  • FileUtil:存放与文件读写相关的方法。

  • WordUtil:存放对单词进行排序分类等相关操作。

  • Main: 测试类

2 . 对应方法

  • FileUtil类:

readFile方法:读取文档,并对其中字符进行统计。

  • WordUtil类

sortWord方法:对单词进行排序输出。

5. 代码说明

 public WordUtil readFile(String filePath) {
        WordUtil count = new WordUtil();
        Map<String, String> dic = new TreeMap<String, String>();
		ArrayList<String> array = new ArrayList<>();
        int characters = 0,words = 0,lines = 0, int line = 0;
		String s = "^&*().|,\t? \\!@#$%";
        String str = "";
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lines++;
                    characters = characters + lineTxt.length();
                    str = "";
                    if (lineTxt.length() == 0) {
                        line++;
                    }
                    for (int i = 0; i < lineTxt.length(); i++) {
                        if (s.indexOf(lineTxt.charAt(i)) >= 0) {
                            if (str.length() >= 4) {
                                if (array.contains(str.toLowerCase())) {
                                    int value = Integer.parseInt(dic.get(str.toLowerCase())) + 1;
                                    dic.put(str.toLowerCase(), "" + value);
                                    str="";
                                } else {
                                    array.add(str.toLowerCase());
                                    dic.put(str.toLowerCase(), "" + 1);
                                    str="";
                                    words++;
                                }
                            } else {
                                str = "";
                            }
                        }
                        if ((lineTxt.charAt(i) >= 'a' && lineTxt.charAt(i) <= 'z') || (lineTxt.charAt(i) >= 'A' && lineTxt.charAt(i) <= 'Z')) {
                            str = str + lineTxt.charAt(i);
                        }
                        if (lineTxt.charAt(i) >= '0' && lineTxt.charAt(i) <= '9') {
                            if (str.length() >= 4) {
                                str = str + lineTxt.charAt(i);
                            } else {
                                str = "";
                            }
                        }
                        if (i == lineTxt.length() - 1) {
                            if (str.length() >= 4) {
                                if (array.contains(str.toLowerCase())) {
                                    int value = Integer.parseInt(dic.get(str.toLowerCase())) + 1;
                                    dic.put(str.toLowerCase(), "" + value);
                                } else {
                                    array.add(str.toLowerCase());
                                    dic.put(str.toLowerCase(), "" + 1);
                                }
                            } else {
                                str = "";
                            }
                        }
                    }
                }
                read.close();
                count.setCharacters(characters + lines-1);
                count.setLines(lines - line);
                count.setWords(words);
                count.setDic(dic);
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        if(characters==0)
            return null;
        else
            return count;
    }

说明: 设置一个空的数组,判断是否为单词。对文件进行按行读取,对每一行进行循环遍历,判断字符是否为英文字母,如果是就存入一个String对象中。判断字符是否是数字符号,是就对其ASCLL码值进行判断,有就加入。遇到特殊的字符后,对其ASCLL码值进行判断,是就接着判断其是否存在于数组中,若不存在就保存到数组和map中,若map中存在则对其值加一。

		public List<Map.Entry<String, String>> sort	Word() {
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(this.dic.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            public int compare(Entry<String, String> o1, Entry<String, String> o2) {
                int value1=Integer.parseInt(o1.getValue());
                int value2=Integer.parseInt(o2.getValue());
                if(value1==value2){
                    return 0;
                }
                else{
                    return value2-value1;
                }
            }
        });
        if(list.size()==0){
            return null;
        }else{
            return list;
        }
    }

说明: 通过重写Comparator以及排序函数来实现对Map中关键字的排序。

6.单元测试效能分析

  • 单元测试使用junit4

7.过程总结

这次实验耗时很久,我刚开始看到题目时,主要想着怎么把功能实现,没有考虑异常以及效能方面,后期单元测试以及效能分析让我觉得很棘手,让我明白了,一个完整的程序不仅仅要实现其具体的功能还要从异常以及效能等多方面考虑。

posted @ 2018-09-17 23:53  Dzwh  阅读(191)  评论(8编辑  收藏  举报