一.码云地址(码云上传失败,正在解决中,提供GitHub地址)
https://edu.cnblogs.com/campus/xnsy/2018Systemanalysisanddesign
https://gitee.com/Crls7/Homework01
https://github.com/Crls2957/test/tree/master/Homework01
二.PSP表格
PSP2.1
PSP阶段
预估耗时
(分钟)
实际耗时
(分钟)
Planning
计划
30
25
· Estimate
· 估计这个任务需要多少时间
300
350
Development
开发
240
180
· Analysis
· 需求分析 (包括学习新技术)
30
20
· Design Spec
· 生成设计文档
30
30
· Design Review
· 设计复审 (和同事审核设计文档)
0
0
· Coding Standard
· 代码规范 (为目前的开发制定合适的规范)
0
0
· Design
· 具体设计
50
30
· Coding
· 具体编码
240
180
· Code Review
· 代码复审
50
180
· Test
· 测试(自我测试,修改代码,提交修改)
30
30
Reporting
报告
200
150
· Test Report
· 测试报告
60
80
· Size Measurement
· 计算工作量
300
400
· Postmortem & Process Improvement Plan
· 事后总结, 并提出过程改进计划
40
60
合计
700
600
三.需求功能分析
本项目主要基础功能是功能是实现文件的字符,单词数,行数统计。对于整个项目功能大家可能实现起来难度并不大,主要这次是我们对于软件工程项目的理解和实践,详细的去完成各个模块,才是我们本项工作的重点和学习的意义。
WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。
可执行程序命名为:wc.exe,该程序处理用户需求的模式为:
wc.exe [parameter] [input_file_name]
存储统计结果的文件默认为result.txt,放在与wc.exe相同的目录下。
参考链接:
https://edu.cnblogs.com/campus/xnsy/2018Systemanalysisanddesign/homework/2120
四.程序设计
对于文件的输入输出,Java语言提供了完善的io包,通过输入输出流的知识极易操作且安全有效的完成项目基础功能。
程序主要分为三个功能类,和主程序类。
三个功能类,分别实现统计文件字符数,单词数和行数。
主程序类主要完成对功能类的实例化和调用。
五.部分编码
CountChar类,统计字符数
package WordCount;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CountChar {
public int CharNum(String filePath)
throws IOException {
int charNumber=0
;
String line;
File file=
new File(filePath);
//从文件中读取数据入字符输入缓冲流,为后续的字符操作提供缓冲功能
FileReader fr =
null;
FileWriter fw=
null;
try {
fr =
new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br=
new BufferedReader(fr);
//将数据存入缓冲字符串
StringBuffer sb=
new StringBuffer();
//将数据一行一行的存入字符串
line=
br.readLine();
while(line!=
null) {
sb=
sb.append(line);
line=
br.readLine();
}
String info=
sb.toString();
charNumber=
info.length();
br.close();
try {
fw=
new FileWriter("D:\\eclipse\\Homework01\\workdata\\result.txt",
true);
fw.write(filePath+" "+"字符数:"+
charNumber);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///错误实例,只能读入第一行数据并判断字符数。而后更改。
/*if((line=br.readLine())!=null) {
char[] ch=line.toCharArray();
for(int i=0;i<ch.length;i++) {
if(!Character.isWhitespace(ch[i])) {
charNumber++;
}
}
}*/
return charNumber;
}
}
CountWord类,统计单词数
package WordCount;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CountWord {
public int countWord(String filePath)
throws IOException {
int wordNumber=0
;
String line;
String [] saveWord;//字符串数组,存储分割的单词
//从文件中读入数据
File file=
new File(filePath);
FileReader fr=
null;
FileWriter fw=
null;
try {
fr=
new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br=
new BufferedReader(fr);
StringBuffer sb=
new StringBuffer();
//将全部数据存入缓冲字符串
while((line=br.readLine())!=
null) {
sb.append(line);
}
//非字母字符全部用空格替换
String str=sb.toString().replaceAll("[^a-zA-Z^]", " "
);
saveWord=str.split("\\s+");
//匹配字符并进行分割
wordNumber=
saveWord.length;
br.close();
try {
fw=
new FileWriter("D:\\eclipse\\Homework01\\workdata\\result.txt",
true);
fw.write(filePath+" "+"单词数:"+
wordNumber);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return wordNumber;
}
}
CountLine类,统计行数
package WordCount;
import java.io.*
;
public class CountLine {
public int CountLine(String filePath) {
int lineNumber=0
;
String line;
//从文件中读入数据
File file=
new File(filePath);
FileReader fr=
null;
FileWriter fw=
null;
try {
fr=
new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br=
new BufferedReader(fr);
//通过行数增加,判断行数。
try {
while((line=br.readLine())!=
null) {
lineNumber++
;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw=
new FileWriter("D:\\eclipse\\Homework01\\workdata\\result.txt",
true);
fw.write(filePath+" "+"行数:"+
lineNumber);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lineNumber;
}
}
六.程序测试
单元测试
上传git以及GitHub
入口参数测试
实际文件展示
七.项目总结
本次个人项目,对于项目功能的实现,单独并不大,对于代码实现,主要是完成了我们对于Java基础知识的复习,本次项目最大的收获是对于psp表格的运用,将我们的的工作过程量化到细节,使得我们对于自己工作量,工作效能,工作成果,都有一个直观的认识,这不仅有利于我们的学习,更对于我们以后的工作,职业发展有着深远的影响。谢谢大家。
转载于:https://www.cnblogs.com/Crls/p/9697125.html
相关资源:homework01-trvrb:由GitHub Classroom创建的homework01-trvrb-源码