软工第二次作业
软工个人作业二
1.码云项目地址
https://gitee.com/highO/software/tree/master/201621123002
PSP2.1 | 个人开发流程 | 个人开发流程 | 实际耗费时间(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 50 |
Estimate | 明确需求和其他相关因素,估计每个阶段的时间成本 | 30 | 50 |
Development | 开发 | 500 | 700 |
Analysis | 需求分析 (包括学习新技术) | 300 | 600 |
Design Spec | 生成设计文档 | 100 | 150 |
Design Review | 设计复审 | 50 | 60 |
Coding Standard | 代码规范 | 30 | 30 |
Design | 具体设计 | 120 | 200 |
Coding | 具体编码 | 200 | 300 |
Code Review | 代码复审 | 60 | 50 |
Test | 测试(自我测试,修改代码,提交修改) | 60 | 200 |
Reporting | 报告 | 100 | 190 |
· | 测试报告 | 40 | 40 |
· | 计算工作量 | 60 | 100 |
· | 并提出过程改进计划 | 50 | 30 |
3.解题思路描述
选择Java语言进行编程,首先是要确定都需要写什么函数来对文件中的字符进行判断,判断后要怎样进行分类存放,考虑好这些问题就开始写了......
4.设计实现过程
1.Main类
其实就是控制台读入文件,然后输出结果
2.WordCount类
对记录单词词频的Map进行排序
3.FileWord类
对字符进行判断分类,用map记录单词词频,统计characters,lines,words
5.代码说明
1.使用sortMap方法排序
public List<Map.Entry<String, String>> sortMap()
{
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;
}
}
2.对characters,lines进行统计
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++;
}
3.判断是否是单词,将单词和词频添加到map中,并统计words
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); //添加到Map中
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 = "";
}
}
}
}
6.解决项目的心路历程与收获
以前编写程序一般都是读完题后就开始写,最后在测试的过程中再对代码进行容错处理,其实这样会浪费很多时间;在本次作业的项目要求上让我意识到,编程前应该先梳理好整个思路然后对各种情况都要实现想好解决方案