1.结对项目PSP编写
我们的项目于4月1日正式开始,在开始编写代码之前,我们先对我们如何完成项目以及对老师布置的作业等进行再一次的商定和讨论。并结合我们平时上课的时间以及项目外等因素,约定了我们一起进行结对编程的时间和地点。最后一起完成了PSP表格的填写:
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 |
30 |
· Estimate |
· 估计这个任务需要多少时间 |
30 |
30 |
Development |
开发 |
740 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
50 |
|
· Design Spec |
· 生成设计文档 |
30 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
20 |
|
· Design |
· 具体设计 |
60 |
|
· Coding |
· 具体编码 |
500 |
|
· Code Review |
· 代码复审 |
30 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
20 |
|
Reporting |
报告 |
90 |
|
· Test Report |
· 测试报告 |
50 |
|
· Size Measurement |
· 计算工作量 |
20 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
20 |
|
|
合计 |
860 |
|
总结分析:这次在项目完成后,发现预估时间和实际耗时相差比较大,其主要表现在开发阶段。由于我们在代码自审和互审阶段,都发现了编码所存在的不规范,所以花了更多的时间去修改代码。还有就是在编码的时候,我们要完成的内容比我们想象得要困难一点,所以在编码的时候花了更多的时间。最后就是测试,这次项目开发采用了TDD,我们要先编写测试代码,再实现接口,这与我们之间采用的方式也不同,所以也花了更多的时间。
2.代码互审
2.1总体情况分析
在审查之后发现,队友的代码已经根据了我们一起编写的小组编程规范进行了修改。修改后的代码风格简洁明了,非常容易理解和阅读。但是仍需注意的地方是,方法里的代码注释要写在对应代码的上面而不是后面,这样会使代码阅读更加清晰。除此之外,在互审之后,发现代码存在一个小bug需要进行改正。
2.2 问题
出现问题的模块在CountUtil类中的returnWords方法。
问题是在测试时发现:当一个文件为空的时候,它会返回单词数为1,而且当有两个文件同时测试时,如果第一个文件的单词数比第二个文件的单词数多的话,只显示第一个的单词数。
2.3 解决方法
在问题提出后,我和队友一起解决了问题,发现问题出在每次统计单词数目所要用的StringBuffer没有进行清空的操作。
3.设计
3.1 结构设计
功能类:主函数类(WordCount类) 接口类(CountUtil类)
WordCount类:
CountUtil类:
3.2 类间关系
3.3 WordCount类的流程图
4.代码说明
4.1扩展功能:
(1)当输入-e时,后面跟停用词表stoplist.txt
首先解析停用词表中的单词:
/**
* 用来解析停用词表中单词
* **/
public String[] stoplistWord(String stopList)
{
String lineString = null;
String[] stopListBuffer = null;
File sl = new File(stopList);
try {
BufferedReader br = new BufferedReader(new FileReader(sl));
while((lineString = br.readLine())!=null) {
//当遇到空格的时候,就为一个单词
stopListBuffer = lineString.split("\\s+");
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stopListBuffer;
}
然后判断文件中的单词是否是停用词表中的单词:
/*** * * 判断该词是否存在于停用词表内 * */ public static boolean inStop(String str, String[] fileBuffer) { int count = 0; for(int i = 0;i<fileBuffer.length;i++) {
//将获得的单词,与停用词表中的单词一个一个的与进行比较 if(str.equals(fileBuffer[i])) { count++; } } if(count > 0) { return true; }else return false; }
(2)当输入-a时,查询文件中的代码行数、注释行数、空行数
getCodeLines() 函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/**
* 查询文件的代码行数
* @param filePaths 所有有效文件的集合
* @return String类型 查询结果
*/
public
String getCodeLines(Set<String> filePaths) {
// 结果字符串
String result =
""
;
// 缓存变量
String tmp =
""
;
// 统计次数
int
count =
0
;
// 得到输入流
FileInputStream is =
null
;
InputStreamReader ist =
null
;
BufferedReader br =
null
;
try
{
for
(String filePath : filePaths) {
is =
new
FileInputStream(filePath);
ist =
new
InputStreamReader(is);
br =
new
BufferedReader(ist);
while
((tmp = br.readLine())!=
null
) {
// 去除读取的空格,方便识别内容类型
tmp = tmp.replace(
" "
,
""
);
// 改行不为空,则计数+1
if
(!
""
.equals(tmp)&&!tmp.startsWith(
"//"
)&&tmp.indexOf(
"//"
)!=
1
) {
count++;
}
}
// 拼接结果字符串
result+=filePath+CODE_LINES+count+SPACE;
// 重置count计数变量
count =
0
;
}
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
// 关闭输入流
is.close();
ist.close();
br.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
return
result;
}
|
getEmptyLines() 函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/**
* 查询文件的空行数
* @param filePaths 所有有效文件的集合
* @return String类型 查询结果
*/
public
String getEmptyLines(Set<String> filePaths) {
// 结果字符串
String result =
""
;
// 缓存变量
String tmp =
""
;
// 统计次数
int
count =
0
;
// 得到输入流
FileInputStream is =
null
;
InputStreamReader ist =
null
;
BufferedReader br =
null
;
try
{
for
(String filePath : filePaths) {
is =
new
FileInputStream(filePath);
ist =
new
InputStreamReader(is);
br =
new
BufferedReader(ist);
while
((tmp = br.readLine())!=
null
) {
// 改行不为空,则计数+1
if
(
""
.equals(tmp)) {
count++;
}
}
// 拼接结果字符串
result+=filePath+EMPTY_LINES+count+SPACE;
// 重置count计数变量
count =
0
;
}
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
// 关闭输入流
is.close();
ist.close();
br.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
return
result;
}
|
getNoteLines() 函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/**
* 查询文件的注释行数
* @param filePaths 所有有效文件的集合
* @return String类型 查询结果
*/
public
String getNoteLines(Set<String> filePaths) {
// 结果字符串
String result =
""
;
// 缓存变量
String tmp =
""
;
// 统计次数
int
count =
0
;
// 得到输入流
FileInputStream is =
null
;
InputStreamReader ist =
null
;
BufferedReader br =
null
;
try
{
for
(String filePath : filePaths) {
is =
new
FileInputStream(filePath);
ist =
new
InputStreamReader(is);
br =
new
BufferedReader(ist);
while
((tmp = br.readLine())!=
null
) {
// 去除读取的空格,方便识别内容类型
tmp = tmp.replace(
" "
,
""
);
// 改行不为空,则计数+1
if
(!
""
.equals(tmp)&&tmp.startsWith(
"//"
)||tmp.indexOf(
"//"
)==
1
) {
count++;
}
}
// 拼接结果字符串
result+=filePath+NOTE_LINES+count+SPACE;
// 重置count计数变量
count =
0
;
}
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
// 关闭输入流
is.close();
ist.close();
br.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
return
result;
}
|
(3)当输入-s时,递归处理目录下符合条件的文件
/** * 得到输入文件中指定文件和指定文件夹下符合条件的文件 * @param names 输入文件的集合 * @param condition 文件夹下的筛选条件 * @return Set<String>类型 所有指定文件和指定文件夹下符合条件的文件 */ public Set<String> getAllFileWithCondition(Set<String> names,String condition) { // 存储输入文件和输入文件夹下符合指定条件的文件的集合 Set<String> filePaths = new HashSet<String>(); for (String name : names) { File file = new File(name); if (file.isFile()) { filePaths.add(name); }else if (file.isDirectory()) { // 调用处理文件夹下查询符合指定条件的文件 filePaths.addAll(getDirectoryFilesWithCondition(file, condition)); } } return filePaths; }
4.2高级功能:
当输入-x的时候,打开图形化界面操作,调用getUI()方法
5.测试过程
5.1测试模块建立
此次测试使用的是java提供的单元测试工具进行测试,新建了一个CountUtilTest类,对CountUtil中的接口方法进行测试。在此次单元测试的设计中,我们所设计的测试用例包括了没有内容的.c文间,只有一个单词的.c文件等比较典型的测试用例。
5.2 单元测试结果
5.3 exe文件测试结果
所有的文件:
out.c用例:
(1)代码行数、注释行数、空行数测试:
(2)单词数、行数、字符数测试
(3)字符数、行数、单词数和代码数、注释行数、空行数测试:
(4)基础功能和扩展功能测试
(5)停用词表测试
(6)UI界面
6.总结
在这次结对项目实践完成之后,我发现1+1是大于2的。我们项目完成的效率远比当时我一个人完成的时候要高出很多,而且我们一起编程可以及时解决所遇到的问题和避免一些不必要的问题。避免走了很多的弯路。除此之外,我还学习到了我队员的编程思想和他的学习方式,这些东西都给我带来了很多的启发和新的思路。总得来说,整个项目的完成也比之前的更加顺利和充满动力。提高了我对完成项目的积极性。