202103226-1 编程作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/computer-science-class1-2018
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-science-class1-2018/homework/11877
这个作业的目标 软件工程的实用题
其他参考文献 《java开发实战经典》

一、Github项目地址

https://gitee.com/Y-_Q/project-java

二、代码规范链接

https://gitee.com/Y-_Q/project-java/blob/master/20188400/代码规范

三、PSP

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

四、解题思路

项目架构:

  • Wordcount为执行文件,即用于本次作业通过命令行调用;
  • A包为这次核心模块,即需求对应的整套解决方案
  • A包下的IOHandler是针对I/O处理的工具类
  • A包下的TextParser是针对文本解析的实现类,对文本的解析分为字符解析CharParser和单词解析 WordParser两部分
  • A包下的MyWordParser和MyCharParser分别实现了字符解析CharParser和单词解析 WordParser接口,其解析规则基于本次作业的需求

IOHandler

  • 用于I/O处理的工具类;有两个静态方法readFile和writeToFile
  • readFile根据用户传入的输入文件路径;读取文本文件的内容,并返回内容字符串
  • writeToFile接收两个参数输出文件路径和输出内容;将指定内容输出至目标路径

TextParser

  • 根据用户提供的解析规则对文本内容进行解析;其中分为字符解析CharParser和解析WordParser两部分
  • 用户需要提供输入文件和输出文件的路径;并给出字符解析规则和单词解析规则
  • TextParser会调用IOHandler的静态方法,读取输入文件的内容,并存入textContent中
  • TextParser根据题目需求,提供了ASCII字符统计、有效行数统计、单词数量统计、单词频率统计方法;这些方法调用用户配置的字符解析器和单词解析器,进行解析和统计;
  • TextParser调用IOHandler的静态方法;将统计结果输出至目标文件
    自认为的亮点:将各功能与具体实现分离;用户可以根据不同的解析、统计规则,配置不同的解析器;从而降低了代码的耦合度,增加了可扩展性
public TextParser(String inputFile,String outputFile,CharParser charParser,WordParser wordParser)
  {
    this.outputFile = outputFile;
    this.inputFile = inputFile;
    this.validCharsNum=0;
    this.validLinesNum=0;
    this.wordNum=0;
    this.readFile(); //读取文件内容
    this.wordCountMap=new HashMap<>();
    this.charParser=charParser;  //配置字符解析器
    charParser.parseText(textContent);  //字符解析器解析文本
    this.wordParser=wordParser;  //配置单词解析器
  }

MyWordparser

  • 解析并统计单词
  • 一个int类型变量letterNum记录连续读取的字母数量;一个读词器StringBuilder保存这些连续读取的字母
  • 两个标志位isWordReading和isThisWordValid用来辅助判断;
  • isWordReading表示当前字段是否为单词;当前字段为单词时,字母和数字都加入读词器,若当前字段暂时还不满足单词条件,只有字母才能加入读词器
if(isWordReading)   //正在读取单词
{
    if(isValidChar(c)) //若是有效字符
    {
      wordReader.append(c);   //加入单词读取器
    }
    else  //否则
    {
      num=recordWord(num);    //将单词记入map;并统计数量
      endThisWordReading();   //结束本单词的读取;重置标志位;开始读取下一单词
    }
}

覆盖率

五、测试

  • 单词测试思路

    • 4个字母的合法单词
    • 3个字母的非法单词
    • 字母与数字混合的非法单词
    • 字母与数字混合的合法单词
    • 空字符开头
    • 空字符结尾
  • 有效行测试思路

    • 空字符开头
    • 空字符结尾
    • 文件结尾部分
    • 连续多行空字符
    • 连续多个换行
    • 字符+空白字符+换行

测试样例:

ln a calm sea every man is a pilot.
But all sunshine without shade, all pleasure without pain, is not life at all.Take the lot ofthe happiest - it is a tangled yarn.Bereavements and blessings,one following another, make us sad and blessed by turns. Even death itself makes life more loving. Men come closest to their true selves in the sober moments of life, under the shadows of sorrow and loss.
In the affairs of life or of business, it is not intellect that tells so much as character,notbrains so much as heart, not genius so much as self-control, patience,and discipline,regulated by judgment.
l have always believed that the man who has begun to live more seriously with in beginsto live more simply without. In an age of extravagance and waste, l wish l could show to theworld how few the real wants of humanity are.
To regret one's errors to the point of not repeating them is true repentance.There isnothing noble in being superior to some other man. The true nobility is in being superior toyour previous self.

结果:

六、异常处理说明

文件输入异常时的处理办法:

catch (FileNotFoundException e)
{
  e.printStackTrace();
  System.out.println("无法找到该路径下的文件!!!");
}
catch (IOException e)
{
  e.printStackTrace();
  System.out.println("文件内容读取异常!!!");
}

文件输出异常时的处理:

catch (FileNotFoundException e)
{
  e.printStackTrace();
  System.out.println("无法找到该路径下的文件!!!");
}
catch (UnsupportedEncodingException e)
{
  e.printStackTrace();
  System.out.println("不支持的编码类型!!!");
}
catch (IOException e)
{
  e.printStackTrace();
  System.out.println("IO异常!!!");
}

七、心路历程和收获

在完成词频统计程序的开发的过程中,我学会了独立完成项目,复习巩固了一些Java的知识。在这次的项目中,我学会了如何用git管理代码、查看自己的代码等,其中也付出了很多的精力。从一开始并没有用git去保存代码,很多次修改都是在本地编译软件中进行的,后面因为git的使用不熟练也吃了不少苦头,所以这次的作业完成的也是磕磕碰碰。希望在未来的开发和实践中能进一步思考,有所收获。

posted @ 2021-04-01 22:54  颜晴超甜♡  阅读(79)  评论(0编辑  收藏  举报