實驗二《面向對象程序設計》_實驗報告

#實驗二《面向對象程序設計》_實驗報告html

##1、實驗前期預習準備git

###Junit的安裝和配置設計模式

  • 選擇File->Setting打開設置對話框,在設置對話框中選擇Plugins, 單擊Install JetBrains plugin...按鈕打開插件安裝界面:
  • 在插件安裝對話框的搜索中輸入junit,單擊JunitGenerator V2.0,單擊右邊綠色的Install1按鈕安裝:
  • 結果以下

##2、實驗內容與主要步驟app

###提交點1:測試MyUtil類ide

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 "錯誤";
    }
}
- 測試代碼
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(-55));
        assertEquals("錯誤", MyUtil.percentage2fivegrade(105));
    }
    @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));
    }
}
  • 運行測試結果

###提交點2:以TDD的方式研究學習StringBuffer函數

public class StringBufferDemo {
    public static void main(String [] args){
              StringBuffer buffer = new StringBuffer(20);
              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('t', a.charAt(1));
    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, b.indexOf("gBu"));
        assertEquals(8, c.indexOf("ffer"));
    }
}
  • 實驗結果截圖

###提交點3:對設計模式示例進行擴充單元測試

  • 實驗要求
    • 參考http://www.cnblogs.com/rocedu/p/6736847.html
    • 對設計模式示例進行擴充,體會OCP原則和DIP原則的應用,初步理解設計模式
      • 用本身的學號%6進行取餘運算,根據結果進行代碼擴充:
      • 0: 讓系統支持Byte類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
      • 1: 讓系統支持Short類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
      • 2: 讓系統支持Boolean類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
      • 3: 讓系統支持Long類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
      • 4: 讓系統支持Float類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
      • 5: 讓系統支持Double類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
  • 實驗代碼
abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=20175226%6;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Byte extends  Data {
   byte value;
   Byte() {
        value=20175226%6;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Short extends  Data {
    short value;
    Short() {
        value=20175226%6;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Boolean extends  Data {
    boolean value;
    Boolean() {
        value=20175226%6==4;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Long extends  Data {
    long value;
    Long() {
        value=20175226;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Double extends  Data {
    double value;
    Double() {
        value=20175226%6;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Float extends  Data {
    float value;
    Float() {
        value=20175226%6;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
// Pattern Classes
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
    public Data CreateDataObject(){
        return new Integer();
    }
}
class ByteFactory extends Factory {
    public Data CreateDataObject(){
        return new Byte();
    }
}
class ShortFactory extends Factory {
    public Data CreateDataObject(){
        return new Short();
    }
}
class BoolenFactory extends Factory {
    public Data CreateDataObject(){
        return new Boolean();
    }
}
class LongFactory extends Factory {
    public Data CreateDataObject(){
        return new Long();
    }
}
class DoubletFactory extends Factory {
    public Data CreateDataObject(){
        return new Double();
    }
}
class FloatFactory extends Factory {
    public Data CreateDataObject(){
        return new Float();
    }
}
//Client classes
class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
//Test class
public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new IntFactory());
        d.DisplayData();
        d = new Document(new ByteFactory());
        d.DisplayData();
        d = new Document(new ShortFactory());
        d.DisplayData();
        d = new Document(new BoolenFactory());
        d.DisplayData();
        d = new Document(new LongFactory());
        d.DisplayData();
        d = new Document(new DoubletFactory());
        d.DisplayData();
        d = new Document(new FloatFactory());
        d.DisplayData();
    }
}
  • 實驗結果

###提交點4:以TDD的方式開發一個複數類Complex學習

  • 實驗要求
    • 方式開發一個複數類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)
- 產品代碼
public class Complex {
    // 定義屬性並生成getter,setter
    double RealPart;
    double ImagePart;
    // 定義構造函數
    public Complex(){
        RealPart = 0;
        ImagePart = 1;
    }
    public Complex(double R,double I){
        ImagePart = I;
        RealPart = R;
    }

    //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) {
        double b=0,c=0;
        b = RealPart+a.RealPart;
        c = ImagePart+a.ImagePart;
        System.out.println("("+RealPart+"+"+ImagePart+"i) + ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
        return  new Complex(b,c);
    }
    Complex ComplexSub(Complex a) {
        double b=0,c=0;
        b = RealPart-a.RealPart;
        c = ImagePart-a.ImagePart;
        System.out.println("("+RealPart+"+"+ImagePart+"i) - ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
        return  new Complex(b,c);
    }
    Complex ComplexMulti(Complex a) {
        double b=0,c=0;
        b = RealPart*a.RealPart;
        c = ImagePart*a.ImagePart;
        System.out.println("("+RealPart+"+"+ImagePart+"i) * ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
        return  new Complex(b,c);
    }
    Complex ComplexDiv(Complex a) {
        if(a.RealPart==0||a.ImagePart==0) {
            System.out.println("被除數不能爲0,錯誤");
            return new Complex();
        }

        double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
        double b=0,c=0;
        b = (RealPart*a.RealPart+ImagePart*a.ImagePart)/d;
        c = Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d);
        System.out.println("("+RealPart+"+"+ImagePart+"i) / ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
        return  new Complex(b,c);
    }
}
- 測試代碼
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
    Complex complex = new Complex(1, 1);
    @Test
    public void testAdd() {
        assertEquals(new Complex(3.0, 3.0), complex.ComplexAdd(new Complex(2.0, 2.0)));
    }
    @Test
    public void testSub() {
        assertEquals(new Complex(-5.0, -2.0), complex.ComplexSub(new Complex(6.0, 3.0)));
    }
    @Test
    public void testMulti() {
        assertEquals(new Complex(3.0, 2.0), complex.ComplexMulti(new Complex(3.0, 2.0)));
    }
    @Test
    public void testDiv() {
        assertEquals(new Complex(1.0, 1.0), complex.ComplexDiv(new Complex(1.0, 1.0)));
        assertEquals(new Complex(0.0, 0.0), complex.ComplexDiv(new Complex(1.0, 0.0)));
        //邊緣測試
    }
}
  • 結果截圖

###提交點5:類圖測試

##實驗中碰到的問題this

  • 問題1:在第二個提交點寫測試類時,測試charAt方法時我寫的語句是
public void testcharAt() throws Exception {
    assertEquals("t", a.charAt(1));
    assertEquals("g", a.charAt(5));
    assertEquals("r", a.charAt(11));
    }

結果發現怎麼樣測試都是紅色條,不經過。

  • 問題1解決過程:通過反覆測試我將測試的代碼換成了下面這樣才能經過
public void testcharAt() throws Exception {
    assertEquals('t', a.charAt(1));
    assertEquals('g', a.charAt(5));
    assertEquals('r', a.charAt(11));
    }

這個問題體現了一些咱們的細節,像兩個字符串比較同樣,雖然" s "' s '看起來內容如出一轍但事實上二者比較起來二者之間有很大差別。二者佔用的內存空間就不同至少會有"\0".因此顯而易見字符能夠被視爲長度爲1的字符串這個想法是錯誤的。

(代碼託管)[https://gitee.com/wpyzka/20175226/tree/master/src/shiyan2]

步驟 耗時 百分比
需求分析 10min 5.7%
設計 25min 14.7%
代碼實現 80min 47.1%
測試 10min 5.7%
分析總結 45min 26.5%
相關文章
相關標籤/搜索