2018-2019-2 20175216实验三《敏捷开发与XP实践》实验报告
实验三 敏捷开发与XP实践
实验步骤
1、 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA
参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题。
在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能。提交截图,加上自己学号水印。
实验代码:
public class CodeStandard {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
if(buffer.capacity()<20)
buffer.append("1234567");
for(int i=0; i<buffer.length();i++)
System.out.println(buffer.charAt(i));
}
}
- 规范后的代码格式
/**
* Demo class
*
* @author 20175216
* @date 2019/04/29
*/
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer buffer = new StringBuffer("StringBuffer");
@Test
public void testcharAt(){
assertEquals('S',buffer.charAt(0));
}
@Test
public void testcapacity(){
assertEquals(28,buffer.capacity());
}
@Test
public void testindexof(){
assertEquals(1,buffer.indexOf("tring"));
}
@Test
public void testtoString(){
assertEquals("StringBuffer",buffer.toString());
}
}
-
IDEA中的插件,通过Jetbrains官方仓库安装:打开
Settings
->Plugins
->Marketplace
在搜索框输入alibaba
即可看到Alibaba Java Code Guidelines
插件,点击Install进行安装,然后重启IDE生效:
-
在项目名称上单击右键,在弹出菜单上选择
编码规约扫描
:
-
在IDEA中使用工具(
Code
->Reformate Code
)把下面代码重新格式化
-
Code菜单中选项的简单用法:
名称 | 快捷键 | 功能 |
---|---|---|
Override Methods | Ctrl + O | 重写覆盖方法 |
Implements Methods | Ctrl + I | 实现接口方法 |
Gengrate | Alt + Insert | 生成Construct、Getter/Setter、toString等 |
Surround With | Ctrl + Alt +T | 生成包围代码 |
Unwarp/Remove | Ctrl + Shift + Delete | 取消代码包围 |
Cyclic Expand Word | Alt + / | 循环往上选择单词 |
Cyclic Expand Word | Backwrad | 循环往下选择单词 |
Comment with Line Comment | Ctrl + / | 行注释 |
Comment with Block Comment | Ctrl + Shift + / | 块注释 |
Reformat Code | Ctrl + Alt + L | 格式化代码 |
Move Statement Down | Ctrl + Shift + ↓ | 方法、代码下移 |
Move Statement Up | Ctrl + Shift + ↑ | 方法、代码上移 |
2、在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;
提交搭档项目git log的截图,包含上面git commit的信息,并加上自己的学号水印信息。
- 以开发者的身份在码云上将学习搭档加入自己的项目中
- 下载搭档实验二的Complex代码,加入JUnit单元测试用例:
public class Complex {
public double getRealPart() {
return RealPart;
}
public double getImagePart() {
return ImagePart;
}
double RealPart;
double ImagePart;
public void setRealPart(double realPart) {
RealPart = realPart;
}
public void setImagePart(double imagePart) {
ImagePart = imagePart;
}
public Complex(){}
public Complex(double R,double I){
this.ImagePart = I;
this.RealPart = R;
}
@Override
public String toString() {
return "Complex{" +
"RealPart=" + RealPart +
", ImagePart=" + ImagePart +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Complex complex = (Complex) o;
return Double.compare(complex.getRealPart(), getRealPart()) == 0 &&
Double.compare(complex.getImagePart(), getImagePart()) == 0;
}
Complex ComplexAdd(Complex a) {
return new Complex(this.RealPart + a.RealPart,this.ImagePart + a.ImagePart);
}
Complex ComplexSub(Complex a){
return new Complex(this.RealPart - a.RealPart,this.ImagePart - a.ImagePart);
}
Complex ComplexMulti(Complex a){
return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart,
this.ImagePart*a.RealPart+this.RealPart*a.ImagePart);
}
Complex ComplexDiv(Complex a){
double scale = a.getRealPart()*a.getRealPart() + a.getImagePart()*a.getImagePart();
Complex b = new Complex(a.getRealPart() / scale, - a.getImagePart() / scale);
return this.ComplexMulti(b);
}
}
- 编写测试代码并上传到搭档码云项目上:
- 提交搭档项目git log的截图:
3、实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA。
完成重构内容的练习,下载搭档的代码,至少进行三项重构,提交重构后代码的截图,加上自己的学号水印。提交搭档的码云项目链接。
-
需要重构的地方:
代码重复、方法过长、参数列过长、条件逻辑过度复杂、分支语句
-
规范后的代码
/**
* Demo class
*
* @author 20175203
* @date 2019/04/29
*/
public class Complex {
public double getRealPart() {
return RealPart;
}
public double getImagePart() {
return ImagePart;
}
double RealPart;
double ImagePart;
public void setRealPart(double realPart) {
RealPart = realPart;
}
public void setImagePart(double imagePart) {
ImagePart = imagePart;
}
public Complex(){}
public Complex(double R,double I){
this.ImagePart = I;
this.RealPart = R;
}
@Override
public String toString() {
return "Complex{" +
"RealPart=" + RealPart +
", ImagePart=" + ImagePart +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Complex complex = (Complex) o;
return Double.compare(complex.getRealPart(), getRealPart()) == 0 &&
Double.compare(complex.getImagePart(), getImagePart()) == 0;
}
Complex ComplexAdd(Complex a) {
return new Complex(this.RealPart + a.RealPart,this.ImagePart + a.ImagePart);
}
Complex ComplexSub(Complex a){
return new Complex(this.RealPart - a.RealPart,this.ImagePart - a.ImagePart);
}
Complex ComplexMulti(Complex a){
return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart,
this.ImagePart*a.RealPart+this.RealPart*a.ImagePart);
}
Complex ComplexDiv(Complex a){
double scale = a.getRealPart()*a.getRealPart() + a.getImagePart()*a.getImagePart();
Complex b = new Complex(a.getRealPart() / scale, - a.getImagePart() / scale);
return this.ComplexMulti(b);
}
}
4、参考 http://www.cnblogs.com/rocedu/p/6683948.html,以结对的方式完成Java密码学相关内容的学习,结合重构,git,代码标准。
提交学习成果码云链接和代表性成果截图,要有学号水印。
实验结果:
码云链接:https://gitee.com/besti20175216/20175216_snow_plains/tree/master/20175216/exp3
码云链接:
搭档20175203码云链接:https://gitee.com/mayuda20175203
我的20175216码云链接:https://gitee.com/besti20175216/20175216_snow_plains
实验总结
通过Java第二次实验,我学会了更多的知识,例如:代码标准,代码重构,Java中密码学算法等内容。
代码标准和重构的学习,使我注意到自己之前编写代码时的很多不规范之处,而且通过使用alibaba的实时编码扫描,使我在编写代码的同时,注意规范代码的标准格式,使写出的代码更加客观好看,容易分析,类名和变量名的规范,也使我在编写代码时,减少了许多不必要的注释。