2018-2019-2-20175204 张湲祯 实验三 《敏捷开发与XP实践》实验报告
实验三 Java面向对象程序设计
一.实验内容:
1.XP基础
2.XP核心实践
3.相关工具
4.编码标准:在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能。提交截图,加上自己学号水印。
5.搭档代码检验:在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;
6.重构:完成重构内容的练习,下载搭档的代码,至少进行三项重构.
二.实验步骤
(一)编码标准
1.相关标准
具有说明性的名字
清晰的表达式
直截了当的控制流
可读的代码和注释
实现这些内容时一致所使用某些规则和惯用法
2.安装alibaba插件
参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题。
打开Settings ->Plugins
在搜索框输入alibaba
即可看到Alibaba Java Code Guidelines
插件,点击Install进行安装,然后重启IDE生效:
3.在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));
}
}
4.使用:在项目名称上单击右键,在弹出菜单上选择编码规约扫描:
5.根据报错内容进行规范修改。
6.code相关功能
Override Methods
( 重写覆盖方法 ) Ctrl + O
Implements Methods
( 实现接口方法 ) Ctrl + I
Gengrate
( 用于生成Construct、Getter/Setter、toString等) Alt + Insert
Surround With
(使用if-else、for、while等语句包装代码段) Ctrl+Alt+T
SmartType
( 智能选择并实现 ) Ctrl + Shift + 空格
Cyclic Expand Word
( 循环往上选择单词 ) Alt + /
Cyclic Expand Word
(Backwrad)( 循环往下选择单词 ) Alt + Shift + /
Expand
( 方法展开 ) Ctrl + 加号
Collapse
( 方法折叠 ) Ctrl + 减号
Expand Recursively
( 同上,方法展开 ) Ctrl + Alt + 加号
Collapse Recursively
( 同上,方法折叠 ) Ctrl + Alt + 减号
Expand All
( 全部方法展开 ) Ctrl + Shift + 加号
Collapse All
( 全部方法折叠 ) Ctrl + Shift + 减号
Expand doc comments
( 展开Java doc注释 )
Collapse doc comments
( 折叠Java doc注释 )
Insert Live Template
( 选择Live Templates模板 ) Ctrl + J
Surround with Live Template
( 选择Live Templates模板 ) Ctrl + Alt + J
Comment with Line Comment
( 行注释 ) Ctrl + /
Comment with Block Comment
( 块注释 ) Ctrl + Shift + /
Reformat Code
( 格式化代码 ) Ctrl + Alt + L
Show Reformat File Dialog
( 弹出格式化弹框 ) Ctrl + Alt + Shift + L
Optimize Imports
( 去除未引用的包导入声明 ) Ctrl + Alt + O
Rearrange Code
( 重新整理代码 )
(二)搭档代码进行测试
1.将搭档拉入自己码云项目
2.下载搭档代码
public class Complex {
double RealPart;
double ImagePart;
public Complex() {}
public Complex(double R,double I) {
RealPart = R;
ImagePart = I;
}
public double getRealPart() {
return RealPart;
}
public double getImagePart() {
return ImagePart;
}
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
if(!(obj instanceof Complex))
return false;
else {
Complex complex = (Complex) obj;
if (complex.ImagePart == ((Complex) obj).ImagePart)
return true;
if (complex.RealPart == ((Complex) obj).RealPart)
return true;
}
return false;
}
public String toString() {
String str = "";
if (RealPart == 0&&ImagePart == 0)
str = "0.0";
else if (RealPart == 0&&ImagePart != 0)
str = ImagePart + "" + "i";
else if (RealPart != 0&&ImagePart == 0)
str = RealPart + "";
else if (RealPart !=0&&ImagePart > 0)
str = RealPart + "" + "+" + ImagePart + "i";
else if (RealPart !=0&&ImagePart < 0)
str = RealPart + "" + ImagePart + "i";
return str;
}
public Complex ComplexAdd(Complex a) {
return new Complex(RealPart + a.getRealPart(),ImagePart + a.getImagePart());
}
public Complex ComplexSub(Complex a) {
return new Complex(RealPart - a.getRealPart(),ImagePart - a.getImagePart());
}
public Complex ComplexMulti(Complex a) {
return new Complex(RealPart*a.getRealPart() - ImagePart*a.getImagePart(),RealPart*a.getImagePart() + ImagePart*a.getRealPart());
}
public Complex ComplexDiv(Complex a) {
if(a.getRealPart() == 0&&a.getImagePart() == 0) {
System.out.println("除数不能为0");
return new Complex();
}
else
return new Complex((RealPart*a.getRealPart() - ImagePart*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()),(RealPart*a.getImagePart() + ImagePart*a.getRealPart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()));
}
}
3.对搭档代码进行测试
测试代码
* Demo class
*
* @author 20175204 zyz
* @date 2019/4/29
*/
import junit.framework.TestCase;
import org.junit.Test;
import org.testng.annotations.ITestAnnotation;
public class wzhComplexTest extends TestCase {
wzhComplex a = new wzhComplex(2.0,5.0);
wzhComplex b = new wzhComplex(1.0,-4.0);
wzhComplex c = new wzhComplex(-3.0,2.0);
wzhComplex d = new wzhComplex(-4.0,-3.0);
wzhComplex e = new wzhComplex(0.0,0.0);
@Test
public void testgetRealPart() throws Exception {
assertEquals(2.0,a.getRealPart());
assertEquals(1.0,b.getRealPart());
assertEquals(-3.0,c.getRealPart());
assertEquals(-4.0,d.getRealPart());
assertEquals(0.0,e.getRealPart());
}
@Test
public void testgetImagePart() throws Exception {
assertEquals(5.0,a.getImagePart());
assertEquals(-4.0,b.getImagePart());
assertEquals(2.0,c.getImagePart());
assertEquals(-3.0,d.getImagePart());
assertEquals(0.0,e.getImagePart());
}
@Test
public void testequals() throws Exception {
assertEquals(true,a.toString().equals(a.toString()));
assertEquals(false,b.toString().equals(c.toString()));
assertEquals(false,d.toString().equals(e.toString()));
}
@Test
public void testtoString() throws Exception {
assertEquals("2.0+5.0i",a.toString());
assertEquals("1.0-4.0i",b.toString());
assertEquals("-3.0+2.0i",c.toString());
assertEquals("-4.0-3.0i",d.toString());
assertEquals("0.0",e.toString());
}
@Test
public void testwzhComplexAdd() throws Exception {
assertEquals("3.0+1.0i",a.wzhComplexAdd(b).toString());
assertEquals("-7.0-1.0i",c.wzhComplexAdd(d).toString());
assertEquals("-4.0-3.0i",d.wzhComplexAdd(e).toString());
}
@Test
public void testwzhComplexSub() throws Exception {
assertEquals("1.0+9.0i",a.wzhComplexSub(b).toString());
assertEquals("1.0+5.0i",c.wzhComplexSub(d).toString());
assertEquals("-3.0+2.0i",c.wzhComplexSub(e).toString());
}
public void testwzhComplexMulti() throws Exception {
assertEquals("22.0-3.0i",a.wzhComplexMulti(b).toString());
assertEquals("18.0+1.0i",c.wzhComplexMulti(d).toString());
assertEquals("0.0",b.wzhComplexMulti(e).toString());
}
public void testwzhComplexDiv() throws Exception {
assertEquals("-1.4666666666666666+0.2i",a.wzhComplexDiv(b).toString());
assertEquals("2.5714285714285716+0.14285714285714285i",c.wzhComplexDiv(d).toString());
assertEquals("0.0",a.wzhComplexDiv(e).toString());
}
}
(三)对搭档代码进行重构
1.对代码进行规约扫描
2.对代码不规范问题加以改正
1.单语句时if else 也需要加大括号
2.对于所覆写的方法,必须加@override进行注解
3.方法名、参数名、成员变量、局部变量都统一使用lowerCamelCase,必须遵从驼峰形式
4.类需要添加创建者信息
3.重构之后的代码
* Demo class
*
* @author 20175204 zyz
* @date 2019/4/29
*/
@SuppressWarnings("ALL")
public class wzhComplex {
double RealPart;
double ImagePart;
public wzhComplex() {}
public wzhComplex(double R,double I) {
RealPart = R;
ImagePart = I;
}
public double getRealPart() {
return RealPart;
}
public double getImagePart() {
return ImagePart;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
if(!(obj instanceof wzhComplex)) {
return false;
} else {
wzhComplex wzhComplex = (wzhComplex) obj;
if (wzhComplex.ImagePart == ((wzhComplex) obj).ImagePart) {
return true;
}
if (wzhComplex.RealPart == ((wzhComplex) obj).RealPart) {
return true;
}
}
return false;
}
@Override
public String toString() {
String str = "";
if (RealPart == 0&&ImagePart == 0) {
str = "0.0";
} else if (RealPart == 0&&ImagePart != 0) {
str = ImagePart + "" + "i";
} else if (RealPart != 0&&ImagePart == 0) {
str = RealPart + "";
} else if (RealPart !=0&&ImagePart > 0) {
str = RealPart + "" + "+" + ImagePart + "i";
} else if (RealPart !=0&&ImagePart < 0) {
str = RealPart + "" + ImagePart + "i";
}
return str;
}
public wzhComplex wzhComplexAdd(wzhComplex a) {
return new wzhComplex(RealPart + a.getRealPart(),ImagePart + a.getImagePart());
}
public wzhComplex wzhComplexSub(wzhComplex a) {
return new wzhComplex(RealPart - a.getRealPart(),ImagePart - a.getImagePart());
}
public wzhComplex wzhComplexMulti(wzhComplex a) {
return new wzhComplex(RealPart*a.getRealPart() - ImagePart*a.getImagePart(),RealPart*a.getImagePart() + ImagePart*a.getRealPart());
}
public wzhComplex wzhComplexDiv(wzhComplex a) {
if(a.getRealPart() == 0&&a.getImagePart() == 0) {
System.out.println("除数不能为0");
return new wzhComplex();
}
else {
return new wzhComplex((RealPart*a.getRealPart() - ImagePart*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()),(RealPart*a.getImagePart() + ImagePart*a.getRealPart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()));
}
}
}
(四)实验体会
通过本次实验,对idea相关功能有所更多的了解,也对所编写的代码有了更好的规范,更加符合相关标准,养成一个良好的编程习惯。
原文地址:https://www.cnblogs.com/zyzgl/p/10793640.html