结对编程作业
合作者:201631083106,201631063412
作业链接:https://gitee.com/monkeyjb/WordCount/blob/master/src/WordCount.java
代码规范
本次代码使用Java编写,经过网上查阅各个公司代码规范后决定规范参考阿里巴巴公司 java 规范,通过对其规范的阅读后,认为该规范比较合适,故拿来使用。
阿里巴巴Java代码规范官方网站:https://github.com/alibaba/p3c
作业内容
本次作业的主要目的是对上次Wordcount作业的功能进行拓展,并对各项功能实行测试,运用课上学习的单元测试,代码互审,静态代码审查等技术,对代码进行较全面的测试。
1.代码互审
通过对彼此代码的检查后发现其中有部分代码规范不同,并对代码做了缩进等修改,并制定了相对属于两个人的代码规范。
经过修改后的代码如下:
public static void main(String[] args) { String fileName; String outFile=null; for(int i=0;i<args.length;i++){ switch (args[i]) { case "-c": fileName=args[i+1]; getCharCount(fileName); System.out.println(fileName+",字符数:"+getCharCount(fileName)); break; case "-w": fileName=args[i+1]; getWordCount(fileName); System.out.println(fileName+",单词数:"+getWordCount(fileName)); break; case "-l": fileName=args[i+1]; getLineCount(fileName); System.out.println(fileName+",行数:"+getLineCount(fileName)); break; case "-o": outFile=args[i+1]; writeToFile("test.txt", outFile); System.out.println("写入成功!"); break; } } } //获取字符数量的方法 public static int getCharCount(String fileName){ int count=0; String line; try{ BufferedReader bf=new BufferedReader(new FileReader(fileName)); while((line=bf.readLine())!=null){ count+=line.length(); } bf.close(); }catch (Exception e) { e.printStackTrace(); } return count; } //获取单词数量的方法、 public static int getWordCount(String fileName){ int count=0; String line; try{ BufferedReader bf=new BufferedReader(new FileReader(fileName)); while((line=bf.readLine())!=null){ String[] strings=line.trim().split(" |,"); count+=strings.length; } bf.close(); }catch (Exception e) { e.printStackTrace(); } return count; } //获取总行数的方法 public static int getLineCount(String fileName){ int count=0; String line; try{ BufferedReader bf=new BufferedReader(new FileReader(fileName)); while((line=bf.readLine())!=null){ count++; } bf.close(); }catch (Exception e) { e.printStackTrace(); } return count; } //将结果写入文件的方法 public static void writeToFile(String fileName,String outFile){ try{ BufferedWriter bw=new BufferedWriter(new FileWriter(outFile)); bw.write(fileName+",字符数:"+getCharCount(fileName)); bw.write("\r\n"); bw.write(fileName+",单词数:"+getWordCount(fileName)); bw.write("\r\n"); bw.write(fileName+",行数:"+getLineCount(fileName)); bw.close(); }catch (Exception e) { e.printStackTrace(); } }
2.系统测试
输入
输出
总结:通过这次结对编程让我们知道了,两个人敲代码的好处多多,开始可能不够默契,漏洞百出,但随着两人合作的时间变久,代码的质量有着显著的提高,一个人发现不了的问题,另一个人不一定发现不了,一个人解决不了的问题另一个人可能一眼便知,在结对编程过程中,重要的是两人需要有一份自己的规约,并且在编程时一同去遵守,这样才能做出高质量的软件。