2017-2018-2 20165218 实验三《敏捷开发与XP实践》实验报告

实验三 敏捷开发与XP实践

课程:java程序设计

姓名:赵冰雨

学号:20165218

指导教师:娄嘉鹏

实验日期:2018.4.30

实验内容、步骤与体会:

(一)编码标准

//实验要求
//参考 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));
}
}
  1. 将代码复制进IDEA,使用Code->Reformate Code或快捷键Ctrl+Alt+L进行整理

  2. 根据代码逻辑加入空行

  3. 15和17行的红线提示是由于安装了alibaba 插件,检测出代码中有不符合《阿里巴巴Java开发手册》的地方,根据提示更改完毕即可

code菜单 功能
Override Methods 重载基本类的方法
Implement Methods 完成当前类implements的(或者抽象基本类的)接口的方法
Generate 创建类里面任何字段的 getter 与 setter 方法
Code->Comment with Line Comment 此行改写为注释
Reformat 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;

    }
}
  • 测试代码
/**
 * 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));
    }
}
  • 运行结果

(三)重构

  • 搭档代码:我选择的是搭档实验一中的练习题,实现排列数
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);
    }
}
  • 重构以后
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);
    }
}
  1. 将类名改为PermutNum , 体现排列数的功能
  2. 定义输入对象改为input

(三)实现凯撒密码

 /**
 * 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));
    }
}

算法方法所在类

/**
 * 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();
    }

}


参考资料

1.重构与Intellij Idea初探

posted @ 2018-05-06 22:48  zicerain  阅读(180)  评论(0编辑  收藏  举报