2018-2019-2 20175330实验三《敏捷开发与XP实践》实验报告
实验三 敏捷开发与XP实践-1
参考了http://www.cnblogs.com/rocedu/p/4795776.html。安装了阿里云,重启IDEA就可以用了
3.在IDEA中重新格式化代码。
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)); } }
复制到IDEA之后。
也可以在IDEA中使用工具通过(Code->Reformate Code)把代码重新格式化。
并通过编码规约扫描对代码进行扫描
将代码改正为符合规定的格式
研究Code菜单
在网上翻阅了许多博客:https://blog.csdn.net/deniro_li/article/details/72902621
https://www.cnblogs.com/gsc20175307/p/10802842.html
还有一些快捷键
| 选项 | 快捷键 |功能 |
| --- | --- | --- |
|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 | 取消代码包围 |
| Reformat Code | Ctrl + Alt + L | 格式化代码 |
| Move Statement Down |Ctrl + Shift + ↓ | 方法下移 |
| Move Statement Up | Ctrl + Shift + ↑ |方法上移 |
| Move Line Down | Alt + Shift + ↓ | 代码行下移 |
| Move Line Up | Alt + Shift + ↑ | 代码行上移 |
实验三 敏捷开发与XP实践-2
要求:在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;提交搭档项目git log的截图,包含上面git commit的信息,并加上自己的学号水印信息。
搭档20175307Complex.java代码:
import java.text.DecimalFormat;
public class Complex {
double RealPart;
double ImagePart;
public Complex() {
}
public Complex(double R, double I) {
RealPart = R;
ImagePart = I;
}
public void setter(double R, double I) {
RealPart = R;
ImagePart = I;
}
public double getterRealPart() {
return RealPart;
}
public double getterImagePart() {
return ImagePart;
}
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex = new Complex(34,56);
Complex complex2 = new Complex();
public void testGetterRealPart() {
assertEquals(34.0,complex.getterRealPart());
complex.setter(0,-1);
assertEquals(0.0,complex.getterRealPart());
complex.setter(-3.6,5);
assertEquals(-3.6,complex.getterRealPart());
}
public void testGetterImagePart() {
assertEquals(56.0,complex.getterImagePart());
complex.setter(34,-57.233);
assertEquals(-57.233,complex.getterImagePart());
complex.setter(4,0);
assertEquals(0.0,complex.getterImagePart());
}
public void testEquals() {
assertTrue(complex.equals(complex));
Complex complex2 = new Complex();
assertFalse(complex.equals(complex2));
}
public void testToString() {
complex.setter(0,0);
assertEquals("0",complex.toString());
complex.setter(0.2,3);
assertEquals("0.2+3.0i",complex.toString());
complex.setter(4.5,0);
assertEquals("4.5",complex.toString());
complex.setter(0,6);
assertEquals("6.0i",complex.toString());
complex.setter(-5.6,-6.7);
assertEquals("-5.6-6.7i",complex.toString());
}
public void testComplexAdd() {
complex2 = complex.ComplexAdd(new Complex(-33,-57));
assertEquals("1.0-1.0i",complex2.toString());
assertEquals("33.0+57.0i",complex2.ComplexAdd(complex).toString());
}
public void testComplexSub() {
complex2 = complex.ComplexSub(new Complex(33,57));
assertEquals("1.0-1.0i",complex2.toString());
assertEquals("-33.0-57.0i",complex2.ComplexSub(complex).toString());
}
public void testComplexMulti() {
complex.setter(-3.5,5);
complex2.setter(0,0);
assertEquals("0",complex.ComplexMulti(complex2).toString());
complex2.setter(10,-3);
assertEquals("-20.0+60.5i",complex.ComplexMulti(complex2).toString());
}
@Test(expected = Exception.class)
public void testComplexDiv() throws Exception{
complex.setter(3,3);
complex2.setter(0,9);
assertEquals("0.3-0.3i",complex.ComplexDiv(complex2).toString());
complex.setter(0,0);
assertEquals("0",complex.ComplexDiv(complex2).toString());
}
}
实验三 敏捷开发与XP实践-3
重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更 。
变更前的代码
通过IDEA对代码进行更改
实验三 敏捷开发与XP实践-4
Java密码学
体验加解密---凯撒密码
凯撒密码是一种经典的古典密码体制,这次实验我按要求对代码进行了编码重构。
在IDEA中对原来的字符串加法给出了提示
String concatenation '+=' in loop less... (Ctrl+F1)
Inspection info: Reports String concatenation in loops. As every String concatenation copies the whole String, usually it is preferable to replace it with explicit calls to StringBuilder.append() or StringBuffer.append().
对此情况我将原来的String es ="",改为创建一个Stringbuilder对象
这样做可以提高效率。
小结:对代码还是不太了解,只是在格式上进行了调整和规范。