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

实验三 敏捷开发与XP实践-1

1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这里展开了。
2.安装了alibaba 插件,点击左上角的File,然后点击第五个选项Settings,弹出了一个选项框,在左侧点击Plugins,在上面的Marketplace搜索alibaba,然后就可以下载了,下载之后会要求重启IDEA。

3.在IDEA中重新格式化代码。

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)把代码重新格式化,发现了一些不同。

4.研究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 取消代码包围
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 + ↑ 代码行上移

例子:Move Line
将鼠标移动至需要换行的代码行,单击左键,会发现该行会被选定。

使用快捷键或者点击Code菜单,完成换行。

实验三 敏捷开发与XP实践-2

要求:在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;提交搭档项目git log的截图,包含上面git commit的信息,并加上自己的学号水印信息。

搭档20175333Complex.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;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String s = "";
        if (getterRealPart() == 0 && getterImagePart() == 0) {
            s = "0";
        } else if (getterRealPart() != 0 && getterImagePart() == 0) {
            s = s + getterRealPart();
        } else if (getterRealPart() == 0 && getterImagePart() != 0) {
            s = s + getterImagePart() + "i";
        } else {
            if (getterImagePart() < 0) {
                s = s + getterRealPart() + getterImagePart() + "i";
            } else {
                s = s + getterRealPart() + "+" + getterImagePart() + "i";
            }

        }
        return s;
    }

    public Complex ComplexAdd(Complex a) {
        return new Complex((getterRealPart() + a.getterRealPart()), (getterImagePart() + a.getterImagePart()));
    }

    public Complex ComplexSub(Complex a) {
        return new Complex((getterRealPart() - a.getterRealPart()), (getterImagePart() - a.getterImagePart()));
    }

    public Complex ComplexMulti(Complex a) {
        double imaginePart = getterRealPart() * a.getterImagePart() + getterImagePart() * a.getterRealPart();
        double realPart = getterRealPart() * a.getterRealPart() - getterImagePart() * a.getterImagePart();
        return new Complex(realPart, imaginePart);
    }

    public Complex ComplexDiv(Complex a) throws Exception {
        double imaginePart = -getterRealPart() * a.getterImagePart() + getterImagePart() * a.getterRealPart();
        double realPart = getterRealPart() * a.getterRealPart() + getterImagePart() * a.getterImagePart();
        double div = a.getterRealPart() * a.getterRealPart() + a.getterImagePart() * a.getterImagePart();
        if (div == 0) {
            throw new Exception();
        }
        realPart = realPart / div;
        imaginePart = imaginePart / div;
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        return new Complex(Double.valueOf(decimalFormat.format(realPart)), Double.valueOf(decimalFormat.format(imaginePart)));
    }
}

测试代码ComplexTest

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),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更 。
重构后的代码:

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 set(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;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String s = "";
        if (getRealPart() == 0 && getImagePart() == 0) {
            s = "0";
        } else if (getRealPart() != 0 && getImagePart() == 0) {
            s = s + getRealPart();
        } else if (getRealPart() == 0 && getImagePart() != 0) {
            s = s + getImagePart() + "i";
        } else {
            if (getImagePart() < 0) {
                s = s + getRealPart() + getImagePart() + "i";
            } else {
                s = s + getRealPart() + "+" + getImagePart() + "i";
            }

        }
        return s;
    }

    public Complex complexAdd(Complex a) {
        return new Complex((getRealPart() + a.getRealPart()), (getImagePart() + a.getImagePart()));
    }

    public Complex complexSub(Complex a) {
        return new Complex((getRealPart() - a.getRealPart()), (getImagePart() - a.getImagePart()));
    }

    public Complex complexMulti(Complex a) {
        double imaginePart = getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() - getImagePart() * a.getImagePart();
        return new Complex(realPart, imaginePart);
    }

    public Complex complexDiv(Complex a) throws Exception {
        double imaginePart = -getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() + getImagePart() * a.getImagePart();
        double div = a.getRealPart() * a.getRealPart() + a.getImagePart() * a.getImagePart();
        if (div == 0) {
            throw new Exception();
        }
        realPart = realPart / div;
        imaginePart = imaginePart / div;
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        return new Complex(Double.valueOf(decimalFormat.format(realPart)), Double.valueOf(decimalFormat.format(imaginePart)));
    }
}

实验三 敏捷开发与XP实践-4

Java密码学

1.凯撒密码

凯撒密码是在密码学老师讲过的古典密码体制,在这里我将老师的代码重构,规范了编码标准。
过程主要结合IDEA中的Reformat Code之后给出的提示,进行修改代码。

/**
 * Caesa class
 *
 * @author 20175307gsc
 * @date 2019/05/03
 */
public class Caesa {
    public static void main(String[] args) throws Exception {
        String s = args[0];
        int key = Integer.parseInt(args[1]);
        StringBuilder es = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'a' && c <= 'z')
                // 是小写字母
            {
                c += key % 26;
                //移动key%26位
                if (c < 'a'){
                    c += 26;
                    //向左超界
                }
                if (c > 'z'){
                    c -= 26;
                    //向右超界
                }
            } else if (c >= 'A' && c <= 'Z')
                // 是大写字母
            {
                c += key % 26;
                if (c < 'A'){
                    c += 26;
                }
                if (c > 'Z'){
                    c -= 26;
                }
            }
            es.append(c);
        }
        System.out.println(es.toString());
    }
}

最主要的重构在于将原来的String es ="",改为创建一个Stringbuilder对象:StringBuilder es = new StringBuilder();这样做的原因是因为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().
提示我们应该使用 StringBuilder.append() or StringBuffer.append()方法来完成字符串的更替。
在https://www.cnblogs.com/jack-Leo/p/6684447.html这篇博客中讲到“我们常常碰到字符串连接的情况,方便和直接的方式是通过"+"符号来实现,但是这种方式达到目的的效率比较低,且每执行一次都会创建一个String对象,即耗时,又浪费空间。使用StringBuilder类就可以避免这种问题的发生”。

2.Java对称加密-DES算法

1.将密钥通过字节保存在文件中:
重构的点在于将最后打印kb数组的

for(int i=0;i<kb.length;i++){
       System.out.print(kb[i]+",");
  }

替换成了foreach语句格式:
  for(元素类型type 元素变量value : 遍历对象obj) {
    引用x的java语句;  
}

for (byte i : kb) {    
    System.out.print(i+ ",");
    }

在这里还要说明的一点是,kb是一个byte类型的数组,不能将循环变量i设为常用的int类型去遍历kb,应该设为byte类型去输出kb数组。

2.将SEnc.java中的默认明文改为了手动输入明文,提高了代码的实用性。

Scanner reader = new Scanner(System.in);
String s = reader.nextLine();

通过Scanner读入s。

小结:因为不是和很懂代码,所以关键的代码并没有进行修改,只是在输入输出上略做了调整和规范。

posted on 2019-05-02 23:26  20175307GSC  阅读(222)  评论(0编辑  收藏  举报

导航