Experiment 3 Agile Development and XP Practice
Course: java programming
Name: Zhao Bingyu
Student ID: 20165218
Instructor: Lou Jiapeng
Experiment date: 2018.4.30
Experiment content, steps and experience:
(1) Coding standard
//实验要求
//参考 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));
}
}
Copy the code into IDEA, use
Code->Reformate Code
or shortcut keysCtrl+Alt+L
to organizeAdd blank lines according to code logic
The red line prompts on lines 15 and 17 are due to the installation of the alibaba plug-in, and it is detected that the code does not conform to the "Alibaba Java Development Manual", and you can change it according to the prompts.
code menu | Function |
---|---|
Override Methods | Overriding methods of base classes |
Implement Methods | The method that completes the interface of the current class implements (or the abstract base class) |
Generate | Create getter and setter methods for any field in the class |
Code->Comment with Line Comment | rewrite this line as a comment |
Reformat Code | Indent code in standard format |
(2) Pair programming
- partner code
public class Complex {
// 定义属性并生成getter,setter
private double RealPart;
private double ImagePart;
// 定义构造函数
public Complex() {
}
public Complex(double R, double I) {
this.RealPart = R;
this.ImagePart = I;
}
public double getRealPart() {
return RealPart;
}
public void setRealPart(double realPart) {
RealPart = realPart;
}
public double getImagePart() {
return ImagePart;
}
public void setImagePart(double imagePart) {
ImagePart = imagePart;
}
//Override Object
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Complex)) {
return false;
}
Complex complex = (Complex) obj;
if (complex.RealPart != ((Complex) obj).RealPart) {
return false;
}
if (complex.ImagePart != ((Complex) obj).ImagePart) {
return false;
}
return true;
}
public String toString() {
String string = "";
if (ImagePart > 0)
string = RealPart + "+" + ImagePart + "i";
if (ImagePart == 0)
string = RealPart + "";
if (ImagePart < 0)
string = RealPart + " " + ImagePart + "i";
return string;
}
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a) {
return new Complex(RealPart + a.RealPart, ImagePart + a.ImagePart);
}
Complex ComplexSub(Complex a) {
return new Complex(RealPart - a.RealPart, ImagePart - a.ImagePart);
}
Complex ComplexMulti(Complex a) {
return new Complex(RealPart * a.RealPart - ImagePart * a.ImagePart, ImagePart * a.RealPart + RealPart * a.ImagePart);
}
Complex ComplexDiv(Complex a) {
Complex d = new Complex();
d.RealPart = (this.RealPart * a.RealPart + this.ImagePart * a.ImagePart) / (a.RealPart * a.RealPart + a.ImagePart * a.ImagePart);
d.ImagePart = (this.ImagePart * a.RealPart - this.RealPart * a.ImagePart) / (a.RealPart * a.RealPart + a.ImagePart * a.ImagePart);
return d;
}
}
- test code
/**
* Created by zby on 2018/4/16.
*/
import org.junit.*;
import static org.junit.Assert.*;
public class ComplexTest {
Complex a = new Complex(1, 2);
Complex b = new Complex(1, -4);
@Test
public void testAdd() {
assertEquals("2.0 -2.0i", a.ComplexAdd(b).toString());
System.out.println(a.ComplexAdd(b));
}
@Test
public void testSub() {
assertEquals("0.0+6.0i", a.ComplexSub(b).toString());
System.out.println(a.ComplexSub(b));
}
@Test
public void testMulti() {
assertEquals("9.0 -2.0i", a.ComplexMulti(b).toString());
System.out.println(a.ComplexMulti(b));
}
@Test
public void testDiv() {
assertEquals("-0.4117647058823529+0.35294117647058826i", a.ComplexDiv(b).toString());
System.out.println(a.ComplexDiv(b));
}
}
- operation result
(3) Refactoring
- Partner code: I chose the practice questions in partner experiment 1 to realize the number of permutations
import java.util.*;
public class Pnm {
static Scanner in=new Scanner(System.in);
public static void main(String args[]) {
System.out.println("请输入n:");
int n = in.nextInt();
System.out.println("请输入m:");
int m=in.nextInt();
count(n,m);
}
public static void count(int n,int m){
if(n<m||n<0||m<=0){
System.out.println("输入错误,请重新输入");
System.out.println("确保0<m<=n");
return ;
}
int result=1;
for (int i=0;i<m;i++){
result*=n;
n--;
}
System.out.println("Pnm="+result);
}
}
- after refactoring
import java.util.*;
public class PermutNum {
static Scanner input = new Scanner(System.in);
public static void main(String args[]) {
System.out.println("请输入n:");
int n = input.nextInt();
System.out.println("请输入m:");
int m = input.nextInt();
count(n, m);
}
public static void count(int n, int m) {
if (n < m || n < 0 || m <= 0) {
System.out.println("输入错误,请重新输入");
System.out.println("确保0<m<=n");
return;
}
int result = 1;
for (int i = 0; i < m; i++) {
result *= n;
n--;
}
System.out.println("PermutNum=" + result);
}
}
- Change the class name to
PermutNum
reflect the function of permutation numbers - Define the input object to change to
input
(3) Implementing the Caesar cipher
/**
* Created by zby on 2018/4/2.
*/
import java.util.*;
import java.lang.*;
public class CaeserCipher {//凯撒密码
public static void main(String[] args) {
System.out.println("输入一串字符串作为明文(回车结束):");
Scanner input = new Scanner(System.in);
String m = input.next();//读入一行字符串,以回车为标志
Arithmetic output = new Arithmetic();
String c = output.encrpty(m);
System.out.println("加密后的密文为:" + c);
System.out.println("解密后的明文为:" + output.decrypt(c));
}
}
The class of the algorithm method
/**
* Created by zby on 2018/4/2.
*/
import java.lang.*;
public class Arithmetic {//加密和解密算法
public String encrpty(String m) {
StringBuilder result = new StringBuilder();
char[] mi = m.toCharArray();
int n = mi.length;
for (int c : mi) {
if (c >= 'a' && c <= 'z') {
c += 3; // 移动key%26位
if (c < 'a')
c += 26; // 向左超界
if (c > 'z')
c -= 26; // 向右超界
}
// 如果字符串中的某个字符是大写字母
else if (c >= 'A' && c <= 'Z') {
c += 3; // 移动key%26位
if (c < 'A')
c += 26;// 同上
if (c > 'Z')
c -= 26;// 同上
}
result.append((char) c);
}
return result.toString();
}
public String decrypt(String m) {
StringBuilder result = new StringBuilder();
char[] mi = m.toCharArray();
int n = mi.length;
for (int c : mi) {
if (c >= 'a' && c <= 'z') {
c -= 3; // 向前移动3位
if (c < 'a')
c += 26; // 向左超界
if (c > 'z')
c -= 26; // 向右超界
}
// 如果字符串中的某个字符是大写字母
else if (c >= 'A' && c <= 'Z') {
c -= 3; // 向前移动3位
if (c < 'A')
c += 26;// 同上
if (c > 'Z')
c -= 26;// 同上
}
result.append((char) c);
}
return result.toString();
}
}