20165230 《Java程式設計》實驗二(Java開發環境的熟悉)實驗報告

來源:互聯網
上載者:User

標籤:多態   教師   第一個   物件導向編程   ESS   main   unittest   sys   設計   

20165230 《Java程式設計》實驗二(Java開發環境的熟悉)實驗報告一、實驗報告封面

課程:Java程式設計 班級:1652班 姓名:田坤燁 學號:20165230 成績:

指導教師:婁嘉鵬 實驗日期:2018年4月16日
實驗時間:15:45 - 17:20

實驗序號:實驗二 實驗名稱:Java物件導向程式設計

實驗內容:

  1. 初步掌握單元測試和TDD
  2. 理解並掌握物件導向三要素:封裝、繼承、多態
  3. 初步掌握UML建模
  4. 熟悉S.O.L.I.D原則
  5. 瞭解設計模式

實驗要求:

  1. 沒有Linux基礎的同學建議先學習《Linux基礎入門(新版)》《Vim編輯器》 課程;
  2. 完成實驗、撰寫實驗報告,注意實驗報告重點是運行結果,遇到的問題(工具尋找,安裝,使用,程式的編輯,調試,運行等)、解決辦法(空洞的方法如“查網路”、“問同學”、“看書”等一律得0分)以及分析(從中可以得到什麼啟示,有什麼收穫,教訓等);
  3. 實驗報告中統計自己的PSP(Personal Software Process)時間;
  4. 嚴禁抄襲。
二、實驗內容及步驟(一)單元測試(1) 三種代碼
  • 虛擬碼
如果成績小於0,轉成“錯誤”如果成績小於60,轉成“不及格”如果成績在60與70之間,轉成“及格”如果成績在70與80之間,轉成“中等”如果成績在80與90之間,轉成“良好”如果成績在90與100之間,轉成“優秀”如果成績大於100,轉成“錯誤”
  • 產品代碼
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 junit.framework.TestCase;import org.junit.Test;import static org.junit.Assert.*;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 testException(){        assertEquals("錯誤",MyUtil.percentage2fivegrade(-55));        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));    }}
  • 測試代碼
(二)以TDD的方式研究學習StringBuffer
  • 虛擬碼
測試charAt方法:字串中指定索引處的字元與原序列是否匹配測試capacity方法:StringBuffer的容器容量測試length方法:字串長度測試indexOf方法:子字串的第一個字母在母字串的位置
  • 產品代碼
public class StringBufferDemo{    StringBuffer buffer = new StringBuffer();    public StringBufferDemo(StringBuffer buffer){        this.buffer = buffer;    }    public Character charAt(int i){//字串中指定索引處的字元與原序列是否匹配        return buffer.charAt(i);    }    public int capacity(){//StringBuffer的容器容量        return buffer.capacity();    }    public int length(){//字串長度        return buffer.length();    }    public int indexOf(String buf) {//子字串的第一個字母在母字串的位置        return buffer.indexOf(buf);    }}
  • 測試代碼
import junit.framework.TestCase;import org.junit.Test;import static org.junit.Assert.*;public class StringBufferDemoTest extends TestCase {    StringBuffer string1 = new StringBuffer("tiankunye");//測試9個字元    StringBuffer string2 = new StringBuffer("tiankunye is learning");//測試21個字元    StringBuffer string3 = new StringBuffer("tiankunye is learning java");//測試26個字元    @Test    public void testCharAt(){//測試字串中指定索引處的字元與原序列是否匹配        assertEquals(‘t‘,string1.charAt(0));        assertEquals(‘k‘,string2.charAt(4));        assertEquals(‘y‘,string3.charAt(7));    }    @Test    public void testCapacity(){//測試StringBuffer的容器容量        assertEquals(25,string1.capacity());        assertEquals(37,string2.capacity());        assertEquals(42,string3.capacity());    }    @Test    public void testindexOf() {//測試子字串的第一個字母在母字串的位置        assertEquals(4, string1.indexOf("kun"));        assertEquals(10, string2.indexOf("is"));        assertEquals(22, string3.indexOf("java"));    }    @Test    public void testlength() {//測試字串長度        assertEquals(9, string1.length());        assertEquals(21, string2.length());        assertEquals(26, string3.length());    }}
  • 測試代碼
(三)體會OCP原則和DIP原則的應用
  • 支援Byte類代碼
import java.util.Objects;abstract class Data {    abstract public void DisplayValue();}class Integer extends Data {    int value;    Integer() {        value=100;    }    public void DisplayValue(){        System.out.println (value);    }}class Long extends Data {    long value;    Long() {        value=(long)20165230;    }    public void DisplayValue(){        System.out.println (value);    }}class Byte extends Data {//Byte繼承Data類    byte value;    Byte() {        value=(byte)20165230;    }    public void DisplayValue(){        System.out.println (value);    }}// Pattern Classesabstract class Factory {    abstract public Data CreateDataObject();}class IntFactory extends Factory {    public Data CreateDataObject(){        return new Integer();    }}class LongFactory extends Factory {    public Data CreateDataObject(){        return new Long();    }}class ByteFactory extends Factory {//ByteFactory繼承工廠類    public Data CreateDataObject(){        return new Byte();    }}//Client classesclass Document {    Data data;    Document(Factory factory){        data = factory.CreateDataObject();    }    public void DisplayData(){        data.DisplayValue();    }}public class MyDoc {    static Document d;    static Document c;    public static void main(String[] args) {        d = new Document(new ByteFactory());        d.DisplayData();        c = new Document(new LongFactory());        c.DisplayData();    }}
  • 代碼運行
(四)以TDD的方式開發一個複數類Complex
  • 虛擬碼
(1)屬性:複數包含實部和虛部兩個部分,double RealPart;複數的實部double ImagePart;複數的虛部getRealPart():返回複數的實部getImagePart();返回複數的虛部setRealPart():設定複數的實部setImagePart();設定複數的虛部輸出形式:a+bi(2)方法:①定義建構函式public Complex()public Complex(double R,double I)②定義公有方法:加減乘除Complex ComplexAdd(Complex a):實現複數加法Complex ComplexSub(Complex a):實現複數減法Complex ComplexMulti(Complex a):實現複數乘法Complex ComplexDiv(Complex a):實現複數除法③Override Objectpublic String toString():將計算結果轉化為字串形式並輸出
  • 產品代碼
public class Complex {    // 定義屬性並產生getter,setter    private double RealPart;    private double ImagePart;    // 定義建構函式    public Complex(){    }    public Complex(double R, double I){        this.RealPart = R;        this.ImagePart = I;    }    public double getRealPart() {        return RealPart;    }    public void setRealPart(double realPart) {        RealPart = realPart;    }    public double getImagePart() {        return ImagePart;    }    public void setImagePart(double imagePart) {        ImagePart = imagePart;    }    //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) {        return  new Complex(RealPart+a.RealPart,ImagePart+a.ImagePart);    }    Complex ComplexSub(Complex a) {        return new Complex(RealPart-a.RealPart,ImagePart-a.ImagePart);    }    Complex ComplexMulti(Complex a) {        return new Complex(RealPart*a.RealPart-ImagePart*a.ImagePart,ImagePart*a.RealPart+RealPart*a.ImagePart);    }    Complex  ComplexDiv(Complex a) {        Complex d = new Complex();        d.RealPart = (this.RealPart * a.RealPart + this.ImagePart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);        d.ImagePart = (this.ImagePart * a.RealPart - this.RealPart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);        return d;    }}
  • 測試代碼
import static org.junit.Assert.*;import org.junit.Test;import junit.framework.TestCase;public class ComplexTest extends TestCase {    Complex complex = new Complex(1,1);    @Test    public void testAdd(){        assertEquals(new Complex(2.0,1.6), complex.ComplexAdd(new Complex(5.2,3.0)));    }    //測試加法    @Test    public void testSub(){        assertEquals(new Complex(-2.0,-1.6), complex.ComplexSub(new Complex(5.2,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)));        //assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(3,4)));        //邊緣測試    }    @Test    public void testequals(){        assertEquals(true, complex.equals(new Complex(1.0,1.0)));    }    //測試判斷相等}
  • 測試代碼
(五)使用StarUML對實驗二中的代碼進行建模

三、實驗遇到的問題
  • 問題1:查詢不到junit,如何匯入Junit

  • 問題1解決方案:通過學習學長學姐的部落格,找到瞭解決方法,步驟如下:
  1. 開啟File中的Project Structure
  2. 點擊Dependencies,單擊右上方的+,然後選擇第一個JARs...
  3. 找到下載IDEA的路徑,在lib檔案夾中找到junit-4.12.jar和junit,jar檔案,點擊ok
  4. 選中這兩個包然後點擊ok
  • 問題2:Stringbuffer類的capcity()方法是什麼意思?
  • 問題2解決方案:通過查詢API學習到是返回當前容量

接著學習了狄維佳學姐的部落格,瞭解到

如果小於16則預設容器的大小為16。如果大於16則會調用expandCapacity 函數進行容量的擴充。所以第一次append時,小於16則不需擴充,如果大於16則會直接擴充到34(162+2),比較得到大於append後的長度的話則用34,如果不 是則用append後的長度。此時capacity的大小等於append後的長度,如果在append的話,若不超過70(342+2)的話,此時則capacity為70,如果超過70則繼續用第二次append後的總長度。

  • 問題3:如何?複數的除法?
  • 解決方案:首先通過上網查詢學習到除法的運演算法則

    (a+bi)/(c+di)=(ac+bd)/(c^2 + d^2) +((bc-ad)/(c^2 + d^2))i

接著通過代碼中對abcd的定義套入公式便實現了複數的除法。

四、實驗心得
  • 單元測試的好處
  1. 單元測試可最大限度的減少bug
  2. 可快速定位bug,減少調試時間
  3. 可突破構思瓶頸
  4. 提高代碼品質
  • 在這次實驗中學習到了如何進行Junit測試,並初步掌握了物件導向編程的過程,同時瞭解了TDD方式,OCP、DIP原則和SOLID原則,知道了模式的重要作用和設計模式在Java中的重要地位。通過對老師給出的例子進行學習擴充,運用了舉一反三的學習方法,對編程有了進一步的認識和熟悉
五、PSP時間
步驟 耗時 百分比
需求分析 40min 9%
設計 65min 16%
代碼實現 120min 29%
測試 60min 15%
分析總結 120min 29%
六、參考資料
  • 實驗二 Java物件導向程式設計
  • 實驗二《Java物件導向程式設計》的一點說明
  • Intellj IDEA 簡易教程
  • 積極主動敲代碼,使用JUnit學習Java

20165230 《Java程式設計》實驗二(Java開發環境的熟悉)實驗報告

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.