#實驗三 敏捷開發與XP實踐---實驗報告


一、實驗三 敏捷開發與XP實踐-1

1.實驗要求
-實驗三 敏捷開發與XP實踐 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的內容替換成IDEA

-參考 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));
}
}

2.實驗內容和步驟
(1)安裝、使用alibaba 插件規范代碼

(2)編碼規約掃描
-快捷鍵:ctri+shift+alt+J

(3)規范后的代碼

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

(4)使用工具(Code->Reformate Code)格式化代碼,並了解code菜單的功能
-在code中選擇Reformate Code格式化代碼(Ctrl+Alt+L);
code菜單中的其他功能:
-Override Methods(ctrl+O):重載基本類的方法
-Surround With(Ctrl+Alt+T):使用if-else、for、while等語句包裝代碼段
-Comment with Line Comment(Ctrl+/):將本行變成注釋
-Comment with Block Comment(Ctrl+Shift+/):將選中的代碼塊變成注釋

3.實驗截圖

二、實驗三 敏捷開發與XP實踐-2

1.實驗要求
-在碼雲上把自己的學習搭檔加入自己的項目中,確認搭檔的項目加入自己后,下載搭檔實驗二的Complex代碼,加入不少於三個JUnit單元測試用例;

-測試成功后git add .; git commit -m "自己學號 添加內容";git push;

-提交搭檔項目git log的截圖,包含上面git commit的信息,並加上自己的學號水印信息。

2.實驗步驟和內容
(1)下載搭檔實驗二的Complex代碼,加入不少於三個JUnit單元測試用例
搭檔代碼:

public class Complex {
    double RealPart=0;
    double ImagePart=0;
    public Complex(){}
    public Complex(double RealPart,double ImagePart){
        this.RealPart=RealPart;
        this.ImagePart=ImagePart;

    }
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }
    public String toString(){
        String s = "";
        double r=RealPart;
        double i=ImagePart;
        if(r==0&&i==0){
            s="0";
        }
        else if(r==0&&i!=0){
            s=i+"i";
        }
        else if(r!=0&&i==0){
            s=r+"";
        }
        else if(r!=0&&i<0){
            s=r+""+i+"i";
        }
        else
        {
            s=r+"+"+i+"i";
        }
        return s;
    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        else return false;
    }

    public Complex ComplexAdd(Complex a){
        return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart());
    }
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart());
    }
    public Complex ComplexMulti(Complex a){
        double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart();
        double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart();
        return new Complex(r,i);
    }
    public Complex ComplexDiv(Complex a){
        double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart);
        double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart);
        return new Complex(r,i);
    }
}

(2)對搭檔代碼進行測試
-要測試的類getReal、getImaginary、toString、equals、complexAdd、complexSub、complexMulti、complexDiv

-測試代碼如下:

import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
    Complex a =new Complex(3.0,4.0);
    Complex b =new Complex( 2.0,-4.0);
    Complex c =new Complex(0.0,0.0);
    Complex d =new Complex(-3.0,0.0);
    Complex e =new Complex(-6.0,-1.0);
    @Test
    public void testgetRealPart()throws Exception{
        assertEquals(3.0,a.getRealPart());
        assertEquals(2.0,b.getRealPart());
        assertEquals(0.0,c.getRealPart());
        assertEquals(-3.0,d.getRealPart());
        assertEquals(-6.0,e.getRealPart());
    }
    @Test
    public void testgetImagePart()throws Exception{
        assertEquals(4.0,a.getImagePart());
        assertEquals(-4.0,b.getImagePart());
        assertEquals(0.0,c.getImagePart());
        assertEquals(0.0,d.getImagePart());
        assertEquals(-1.0,e.getImagePart());

    }
    @Test
    public void testtoString()throws Exception{
        assertEquals("3.0+4.0i",a.toString());
        assertEquals("2.0-4.0i",b.toString());
        assertEquals("0",c.toString());
        assertEquals("-3.0",d.toString());
        assertEquals("-6.0-1.0i",e.toString());
    }
    @Test
    public void testComplexAdd()throws Exception{
        assertEquals("5.0",a.ComplexAdd(b).toString());
        assertEquals("2.0-4.0i",b.ComplexAdd(c).toString());
        assertEquals("-1.0-4.0i",b.ComplexAdd(d).toString());
    }
    @Test
    public void testComplexSub()throws Exception{
        assertEquals("1.0+8.0i",a.ComplexSub(b).toString());
        assertEquals("-2.0+4.0i",c.ComplexSub(b).toString());
        assertEquals("3.0",c.ComplexSub(d).toString());
    }
    @Test
    public void testComplexMulti()throws Exception{
        assertEquals("22.0-4.0i",a.ComplexMulti(b).toString());
        assertEquals("0",b.ComplexMulti(c).toString());
        assertEquals("18.0+3.0i",d.ComplexMulti(e).toString());
    }
    @Test
    public void testComplexDiv()throws Exception{
        assertEquals("-0.2-1.25i",a.ComplexDiv(b).toString());
        assertEquals("0",c.ComplexDiv(b).toString());
    }
    @Test
    public void testequals()throws Exception{
        assertEquals(true,a.equals(a));
        assertEquals(false,a.equals(b));

    }
}

-測試成功截圖:

(3)將測試代碼傳到搭檔的碼雲里方法
-先git clone 搭檔碼雲地址,將搭檔的項目添加到自己的項目中

-然后登錄搭檔的碼雲倉庫,點擊管理,在倉庫成員管理中找到開發者,然后點擊邀請用戶

-然后點擊復制鏈接

-然后登錄自己的碼雲,點擊同意就可以上傳

3.實驗截圖

三、實驗三 敏捷開發與XP實踐-3

1.實驗要求
-實驗三 敏捷開發與XP實踐 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的內容替換成IDEA;

-完成重構內容的練習,下載搭檔的代碼,至少進行三項重構,提交重構后代碼的截圖,加上自己的學號水印。提交搭檔的碼雲項目鏈接。

2.實驗步驟和內容
(1)重構有關知識:
-重構(Refactor),就是在不改變軟件外部行為的基礎上,改變軟件內部的結構,使其更加易於閱讀、易於維護和易於變更 。

-修改軟件的四種動機:
①增加新功能;
②原有功能有BUG;
③改善原有程序的結構;
④優化原有系統的性能 。

-需要重構的地方:
①代碼重復;
②方法過長;
③參數列過長;
④條件邏輯過度復雜;
⑤分支語句。

-一個完整的重構流程包括
①從版本控制系統代碼庫中Check out code;
②讀懂代碼(包括測試代碼);
③發現bad smell;
④Refactoring;
⑤運行所有的Unit Tests;
⑥往代碼庫中Check in code。

(2)下載搭檔的代碼,至少進行三項重構
搭檔代碼:

public class Complex {
    double RealPart=0;
    double ImagePart=0;
    public Complex(){}
    public Complex(double RealPart,double ImagePart){
        this.RealPart=RealPart;
        this.ImagePart=ImagePart;

    }
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }
    public String toString(){
        String s = "";
        double r=RealPart;
        double i=ImagePart;
        if(r==0&&i==0){
            s="0";
        }
        else if(r==0&&i!=0){
            s=i+"i";
        }
        else if(r!=0&&i==0){
            s=r+"";
        }
        else if(r!=0&&i<0){
            s=r+""+i+"i";
        }
        else
        {
            s=r+"+"+i+"i";
        }
        return s;
    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        else return false;
    }

    public Complex ComplexAdd(Complex a){
        return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart());
    }
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart());
    }
    public Complex ComplexMulti(Complex a){
        double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart();
        double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart();
        return new Complex(r,i);
    }
    public Complex ComplexDiv(Complex a){
        double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart);
        double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart);
        return new Complex(r,i);
    }
}

①重構1:重寫toString時的@override標志

②重構2:添加作者和日期

③重構3:對類中的變量和方法進行封裝

④重構4:規范方法名(首字母小寫)

⑤重構5:精簡過長的方法

重構后的代碼:

/**
 * @author  zzm
 * @date  2019/4/29
 */
public class Complex {
    private double RealPart=0;
    private double ImagePart=0;
    public Complex(){}
    public Complex(double RealPart,double ImagePart){
        this.RealPart=RealPart;
        this.ImagePart=ImagePart;

    }
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }

    @Override
    public String toString(){
        String s = "";
        double r=RealPart;
        double i=ImagePart;
        if(r==0&&i==0){
            s="0";
        }
        else if(r==0&&i!=0){
            s=i+"i";
        }
        else if(r!=0&&i==0){
            s=r+"";
        }
        else if(r!=0&&i<0){
            s=r+""+i+"i";
        }
        else
        {
            s=r+"+"+i+"i";
        }
        return s;
    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        else return false;
    }

    public Complex ComplexAdd(Complex a){
        return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart());
    }
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart());
    }
    public Complex ComplexMulti(Complex a){
        double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart();
        double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart();
        return new Complex(r,i);
    }
    public Complex ComplexDiv(Complex a){
        double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart);
        double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart);
        return new Complex(r,i);
    }
}

3.實驗截圖

四、代碼托管

搭檔碼雲鏈接:
https://gitee.com/cjy-123/20175218cjy/tree/master/Experiment2/test4
我的碼雲鏈接:
https://gitee.com/zzm-zcc/zhang_zhi_min/tree/master/%E5%AE%9E%E9%AA%8C2

五、實驗感想

通過這次實驗三的過程,並結合實驗二的知識學習了重構知識,學會了規范代碼的方法,並且在此次實驗中完成了結對學習,體驗了團隊的力量,我們應該在實驗過程中跟隊友相互合作,相互促進,我們應該養成代碼規范的習慣,總體來說結對學習的效率較高。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com