第一次编程作业

------------恢复内容开始------------

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class3-2018/homework/11879
这个作业的目标 学会使用gitee并且完成词频统计编程
学好 20188495
其他参考文献 构建之法

1.码云地址

https://gitee.com/BuBu999/project-java

2.PSP表格

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 600 750
• Estimate • 估计这个任务需要多少时间 600
Development 开发 500 600
• Analysis • 需求分析 (包括学习新技术) 50 60
• Design Spec • 生成设计文档 100 110
• Design Review • 设计复审 50 60
• Coding Standard • 代码规范 (为目前的开发制定合适的规范) 10 20
• Design • 具体设计 190 220
• Coding • 具体编码 10 20
• Code Review • 代码复审 90 100
• Test • 测试(自我测试,修改代码,提交修改) 10 20
Reporting 报告 100 150
• Test Repor • 测试报告 20 30
• Size Measurement • 计算工作量 20 20
• Postmortem & Process Improvement Plan • 事后总结, 并提出过程改进计划 60 50
合计 600 150

3.解题思路描述

  • 统计字符数

  • 统计有效行数

  • 统计单词数

  • 统计词频最高的十个单词

4.主要代码

BufferedReader br = new BufferedReader(new FileReader(file));
//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {
            //使用readLine方法,一次读一行
                addList(s);//将字符串送去筛选
                s.toLowerCase();
                //charNum+= s.length();这样读取导致错误
                if (s.trim().length() == 0) continue;
                else {
                    result = result + "\n" + s;
                    lineCount++;
                }
            }
                br.close();

public void addList(String srl) {
//string from readline 将判定为的单词放入List中
srl = srl.trim();
//String[] wordArray = srl.split("[a-zA-Z]{4}([a-zA-Z0-9])*");
//String[] wordArray = srl.split("[0-9A-Za-z]+");

    String [] wordArray  = srl.split("\\W+");//分割单词
    Pattern p = Pattern.compile("[a-zA-Z]{4}([a-zA-Z0-9])*");
    //匹配以>=4位字母开头
    for (String listWord : wordArray) {
        Matcher m = p.matcher(listWord);
        if (listWord.length() != 0 && m.find()) {
            list.add(listWord);
            wordNum++;
        }
    }
}
public void addMap() {
    //将筛选好的单词放入map中,若存在则value+1,不存在则设value=1
        for (String mapWord : list) {
            if(map.get(mapWord) != null) {
                map.put(mapWord, map.get(mapWord) + 1);
            }else{
                map.put(mapWord,1);
            }
            //System.ot.println(mapWord);
        }
        //mapSort(map);
    }
public  void mapSort(Map<String,Integer> oldmap){
//定义比较器根据value的值降序排列
    ArrayList<Map.Entry<String, Integer>> newList = new ArrayList<>(oldmap.entrySet());
    Collections.sort(newList, new Comparator<>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue() - o1.getValue();//降序
        }
    });
        for (int i = 0; i < (newList.size() > 10 ? 10 : newList.size()); i++) {
            wordSet[i] = newList.get(i).getKey();
            freSet[i] = newList.get(i).getValue();
        }

}

5.功能实现


6.实验总结

这次作业相对于以前的作业难度直线上升,6000多个字的题目,不得不让我一个字一个字的去理解,通过这次作业,我也提升了不上,能够基本掌握git和巩固以前学过的知识,但是也给自己一个小目标,就是以后能够天天有代码提交量,能够好好的学习软工这门课程。

posted on 2021-04-02 20:04  Young-Lu  阅读(49)  评论(0编辑  收藏  举报

导航