沒有Linux基礎的同窗建議先學習《Linux基礎入門(新版)》《Vim編輯器》 課程java
完成實驗、撰寫實驗報告,實驗報告以博客方式發表在博客園,注意實驗報告重點是運行結果,遇到的問題(工具查找,安裝,使用,程序的編輯,調試,運行等)、解決辦法(空洞的方法如「查網絡」、「問同窗」、「看書」等一概得0分)以及分析(從中能夠獲得什麼啓示,有什麼收穫,教訓等)。報告能夠參考範飛龍老師的指導算法
嚴禁抄襲,有該行爲者實驗成績歸零,並附加其餘懲罰措施。編程
請你們先在實驗樓中的~/Code目錄中用本身的學號創建一個目錄,代碼和UML圖要放到這個目錄中,截圖中沒有學號的會要求重作,而後跟着下面的步驟練習。設計模式
主類名+Test.class
:點擊主類名——>Ctrl+Enter
——>Creat Testimport 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=2147483647; } public void DisplayValue(){ System.out.println (value); } } class Float extends Data { float value; Float() { value=(float)20165322.0; } 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 LongFactory extends Factory { public Data CreateDataObject(){ return new Long(); } } class FloatFactory extends Factory { public Data CreateDataObject(){ return new Float(); } } //Client classes class Document { Data data; Document(Factory factory){ data = factory.CreateDataObject(); } public void DisplayData(){ data.DisplayValue(); } } public class MyDoc { static Document d; MyDoc(Document d) { this.d = d; } public static void main(String[] args) { d = new Document(new FloatFactory()); d.DisplayData(); } }
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) { 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); } 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); return new Complex((RealPart*a.RealPart+ImagePart*a.ImagePart)/d,Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/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(3.3,3.4), complex.ComplexAdd(new Complex(2.3,2.4))); } //測試加法 @Test public void testSub(){ assertEquals(new Complex(-5.3,-2.4), complex.ComplexSub(new Complex(6.3,3.4))); } //測試減法 @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))); } //測試判斷相等 }
學習了測試代碼的使用以及TDD的模式,但還不夠熟練,基本上是參照老師的教程去作的。接下來要更多的練習,爭取在大創中能編寫較爲全面的優秀的測試代碼供其餘成員編寫算法。網絡