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;
    }

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

 变更前的代码

public class MyUtil{
    public static String percentage2fivegrade(int grade){
        //如果成绩小于0,转成“错误”
        if ((grade < 0))
            return "错误";
            //如果成绩小于60,转成“不及格”
        else if (grade < 60)
            return "不及格";
            //如果成绩在60与70之间,转成“及格”
        else if (grade < 70)
            return "及格";
            //如果成绩在70与80之间,转成“中等”
        else if (grade < 80)
            return "中等";
            //如果成绩在80与90之间,转成“良好”
        else if (grade < 90)
            return "良好";
            //如果成绩在90与100之间,转成“优秀”
        else if (grade <=100)
            return "优秀";
            //如果成绩大于100,转成“错误”
        else
            return "错误";
    }
}
IDEA进行规约扫描

通过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对象

这样做可以提高效率。

小结:对代码还是不太了解,只是在格式上进行了调整和规范。

 

posted @ 2019-05-05 08:44  20175330杨璟旭  阅读(240)  评论(0编辑  收藏  举报