2018-2019-2-20175225 實驗二《Java開發環境的熟悉》實驗報告

姓名:張元瑞 學號:20175225 班級:1752 實驗課程:JAVA程序設計

實驗名稱:Java面向對象程序設計 實驗時間:2019.4.16 指導老師:婁嘉鵬

實驗內容

測試點一

- 「測試驅動開發」(TDD)。TDD的通常步驟以下:
              1.明確當前要完成的功能,記錄成一個測試列表
              2.快速完成編寫針對此功能的測試用例
              3.測試代碼編譯不經過(沒產品代碼呢)
              4.編寫產品代碼
              5.測試經過
              6.對代碼進行重構,並保證測試經過(重構下次實驗練習)
              7.循環完成全部功能的開發

測試代碼html

import junit.framework.TestCase;

import org.junit.Test;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase {
    @Test
    public void testNormal() {
        assertEquals("不及格", MyUtil.percentage2fivegrade(55));
        assertEquals("及格", MyUtil.percentage2fivegrade(65));
        assertEquals("中等", MyUtil.percentage2fivegrade(75));
        assertEquals("良好", MyUtil.percentage2fivegrade(85));
        assertEquals("優秀", MyUtil.percentage2fivegrade(95));
    }
    @Test
    public void testExceptions() {
        assertEquals("錯誤", MyUtil.percentage2fivegrade(105));
        assertEquals("錯誤", MyUtil.percentage2fivegrade(-55));
    }
    @Test
    public void testBoundary() {
        assertEquals("不及格",MyUtil.percentage2fivegrade(0));
        assertEquals("不及格",MyUtil.percentage2fivegrade(60));
        assertEquals("不及格",MyUtil.percentage2fivegrade(70));
        assertEquals("不及格",MyUtil.percentage2fivegrade(80));
        assertEquals("不及格",MyUtil.percentage2fivegrade(90));
        assertEquals("不及格",MyUtil.percentage2fivegrade(100));
    }
}

測試成功截圖
java

測試點二

- public int capacity()返回當前容量。容量指可用於最新插入字符的存儲量,超過這一容量便須要再次分配。
              - 示例代碼以下,使用TDD學習測試四個方法。

StringBufferDemo編程

public class StringBufferDemo{
   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());
       System.out.println(buffer.length());
         }
}

測試代碼設計模式

import junit.framework.TestCase;
        import org.junit.Test;

public class StringBufferDemoTest extends TestCase {
    StringBuffer a = new StringBuffer("StringBuffer");
    StringBuffer b = new StringBuffer("StringBufferStringBuffer");
    StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");
    @Test
    public void testcharAt() throws Exception {
        assertEquals('S',a.charAt(0));
        assertEquals('g',a.charAt(5));
        assertEquals('r',a.charAt(11));
    }
    @Test
    public void testcapacity() throws Exception {
        assertEquals(28,a.capacity());
        assertEquals(40,b.capacity());
        assertEquals(52,c.capacity());
    }
    @Test
    public void testlength() throws Exception {
        assertEquals(12,a.length());
        assertEquals(24,b.length());
        assertEquals(36,c.length());
    }
    @Test
    public void testindexOf() throws Exception {
        assertEquals(0,a.indexOf("Str"));
        assertEquals(5,a.indexOf("gBu"));
    }
}

測試成功截圖
app

測試點三

設計模式初步
             - S.O.L.I.D原則
                    1.SRP(Single ResponsibilityPrinciple,單一職責原則)
                    2.OCP(Open-Closed Principle,開放-封閉原則)
                    3.LSP(Liskov Substitusion Principle,Liskov替換原則)
                    4.ISP(Interface Segregation Principle,接口分離原則)
                    5.DIP(Dependency Inversion Principle,依賴倒置原則
              - 設計原則:
                    1.OCP原則:開放-封閉yuanze。其具體內容爲:軟件實體擴充開放,修改關閉。能夠經過抽象和繼承;面對接口編程。
                    2.DIP原則:依賴倒置原則。其具體內容爲:高層模版不該該依賴與底層模版,兩者都應該依賴於抽象;抽象不該該依賴於細節,細節一個依賴與抽象。
              - 設計模式有四個基本要素:
                    1.Pattern name:描述模式,便於交流,存檔
                    2.Problem:描述何處應用該模式
                    3.Solution:描述一個設計的組成元素,不針對特例
                    4.Consequence:應用該模式的結果和權衡(trade-offs)

MyDocide

abstract class Data {
    abstract public void DisplayValue();
}
class Integer1 extends  Data {
    int value;
    Integer1() {
        value=100;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Short1 extends Data{
    long value;
    Short1(){
        value=20175225;
    }
    public void DisplayValue(){
        System.out.println(value);
    }
}
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory1 extends Factory {
    public Data CreateDataObject(){
        return new Integer1();
    }
}
class ShortFactory1 extends Factory{
    public Data CreateDataObject(){
        return new Short1();
    }
}
class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
//Test class
public class MyDoc {
    static Document d,e;
    public static void main(String[] args) {
        d=new Document(new IntFactory1());
        d.DisplayData();
        e=new Document(new ShortFactory1());
        e.DisplayData();
    }
}

測試成功截圖
函數

測試點四

- 提交:單元測試代碼和運行成功截圖及碼雲上代碼連接,截圖要加上學號水印
        - 任務:以TDD的方式開發一個複數類Complex,要求以下:
// 定義屬性並生成getter,setter
double RealPart;
double ImagePart;
// 定義構造函數
public Complex()
public Complex(double R,double I)

//Override Object
public boolean equals(Object obj)
public String toString()

// 定義公有方法:加減乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
- TDD開發思路回顧:
           1.明確當前要完成的功能,記錄成一個測試列表
           2.快速完成編寫針對此功能的測試用例
           3.測試代碼編譯不經過(沒產品代碼呢)
           4.編寫產品代碼
           5.測試經過
           6.對代碼進行重構,並保證測試經過(重構下次實驗練習)
           7.循環完成全部功能的開發
  - 複數的四則運算公式
           1.(a+bi)+(c+di)=(a+c)+(b+d)i
           2.(a+bi)-(c+di)=(a-c)+(b-d)i
           3.(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
           4.(a+bi)/(c+di)=(a+bi)(c-di)/(c^2+d^2)

測試代碼單元測試

import junit.framework.TestCase;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;

public class ComplexTest extends TestCase {
    Complex c1 = new Complex(0, 3);
    Complex c2 = new Complex(-1, -1);
    Complex c3 = new Complex(2,1);
    @Test
    public void testgetRealPart() throws Exception {
        assertEquals(-1.0, Complex.getRealPart(-1.0));
        assertEquals(5.0, Complex.getRealPart(5.0));
        assertEquals(0.0, Complex.getRealPart(0.0));
    }
    @Test
    public void testgetImagePart() throws Exception {
        assertEquals(-1.0, Complex.getImagePart(-1.0));
        assertEquals(5.0, Complex.getImagePart(5.0));
        assertEquals(0.0, Complex.getImagePart(0.0));
    }
    @Test
    public void testComplexAdd() throws Exception {
        assertEquals("-1.0+2.0i", c1.ComplexAdd(c2).toString());
        assertEquals("2.0+4.0i", c1.ComplexAdd(c3).toString());
        assertEquals("1.0", c2.ComplexAdd(c3).toString());
    }
    @Test
    public void testComplexSub() throws Exception {
        assertEquals("1.0+4.0i", c1.ComplexSub(c2).toString());
        assertEquals("-2.0+2.0i", c1.ComplexSub(c3).toString());
        assertEquals("-3.0 -2.0i", c2.ComplexSub(c3).toString());
    }
    @Test
    public void testComplexMulti() throws Exception {
        assertEquals("3.0 -3.0i", c1.ComplexMulti(c2).toString());
        assertEquals("-3.0+6.0i", c1.ComplexMulti(c3).toString());
        assertEquals("-1.0 -3.0i", c2.ComplexMulti(c3).toString());
    }
    @Test
    public void testComplexComplexDiv() throws Exception {
        assertEquals("-1.5 -1.5i", c1.ComplexDiv(c2).toString());
        assertEquals("1.2+0.6i", c1.ComplexDiv(c3).toString());
        assertEquals("-0.6 -0.6i", c2.ComplexDiv(c3).toString());
    }
}

產品代碼學習

public class Complex{
    private double r;
    private double i;
    public Complex(double r, double i) {
        this.r = r;
        this.i = i;
    }

    public static double getRealPart(double r) {
        return r;
    }

    public static double getImagePart(double i) {
        return i;
    }

    public Complex ComplexAdd(Complex c) {
        return new Complex(r + c.r, i + c.i);
    }
    public Complex ComplexSub(Complex c) {
        return new Complex(r - c.r, i - c.i);
    }
    public Complex ComplexMulti(Complex c) {
        return new Complex(r * c.r - i * c.i, r * c.i + i * c.r);
    }
    public Complex ComplexDiv(Complex c) {
        return new Complex((r * c.i + i * c.r)/(c.i * c.i + c.r * c.r), (i * c.i + r * c.r)/(c.i * c.i + c.r * c.r));
    }

    public String toString() {
        String s = " ";
        if (i > 0)
            s =  r + "+" + i + "i";
        if (i == 0)
            s =  r + "";
        if (i < 0)
            s = r + " " + i + "i";
        return s;
    }
}

測試成功截圖
測試

測試點五

- 檢查點要求:使用WhiteStarUML對實驗二中的代碼進行建模,發類圖的截圖,加上學號水印。參考http://www.cnblogs.com/rocedu/p/6736847.html
            - 類圖中只少兩個類

實驗中遇到的問題

1.在安裝Plugins界面老是彈不出Junit從而沒法安裝。
解決方法:搜索的時候J和U都應該大寫,一會兒就搜出來了,其餘狀況不行,可是其餘人不論大小寫都對,應該是電腦問題吧。
2.1.增長MyUtil的測試類以後,TestCase是紅色的,可是沒有找到junit.jar包的地方
解決方法:
找到電腦中IDEA安裝路徑
打開File->Project Structure
點擊Dependencies,單擊右上角的+,而後選擇第一個JARs or directories
複製剛纔的路徑名,找到junit-4.12 jar、 junit.jar
選擇junit-4.12 jar、 junit.jar兩項,並點擊下方Ok

實驗體會

本次實驗,熟悉了代碼的編寫和添加類,對UML類圖有了更進一步的瞭解。在實驗中還學習了TDD模式,這種先編寫測試代碼,後編寫實驗代碼在必定程度上下降了編寫的錯誤。還了解到了S.O.L.I.D這五大原則,之後再寫程序時還應該熟記這些原則,從而避免走彎路。

相關文章
相關標籤/搜索