1.按照老師博客的要求新建一個MyUtil項目html
在src內新建java classjava
輸入以下代碼:設計模式
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 "錯誤"; } }
2.新建一個test文件夾,標註爲test sourceapp
在MyUtil類名處create testide
輸入以下代碼:函數
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 testException() { 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)); } } test運行結果以下:
1.學習有關StringBuffer的相關知識單元測試
StringBuffer append() 向字符串緩衝區追加元素 StringBuffer charAt() 返回此序列中指定索引處的 char 值 StringBuffer capacity() 獲得緩衝區分配的總容量 StringBuffer indexOf() 返回當前StringBuffer對象中,第一個知足str子串的位置 StringBuffer toString( ) 返回此序列中數據的字符串表示形式 StringBuffer length() 獲得當前StringBuffer的長度 length 與 capacity 的區別:分配的容量與實際的長度
2.按照老師博客中的要求,新建一個 StringBufferDemo項目
在src內新建一個java class
輸入以下代碼:學習
public class StringBufferDemo{ public static void main(String [] args){ StringBuffer buffer = new StringBuffer(); buffer.append('S'); buffer.append("tringBuffer12345"); System.out.println(buffer.charAt(1)); System.out.println(buffer.capacity()); System.out.println(buffer.indexOf("tringBuffer12345")); System.out.println("buffer = " + buffer.toString()); System.out.println(buffer.length()); } }
3.建立一個test文件夾,標註爲test source
新建一個test java class
輸入以下代碼:測試
import org.junit.Test; import junit.framework.TestCase; public class StringBufferDemoTest extends TestCase { StringBuffer sq = new StringBuffer("gst and sq"); StringBuffer gst = new StringBuffer("sq and gst lueluelue hahaha"); public void testcharat() { assertEquals('g', sq.charAt(0)); assertEquals('s', gst.charAt(0)); } public void testcapacity() { assertEquals(26, sq.capacity()); assertEquals(43, gst.capacity()); } public void testlength() { assertEquals(10, sq.length()); assertEquals(27, gst.length()); } public void testindexof() { assertEquals(0, sq.indexOf("gst")); assertEquals(11, gst.indexOf("lue")); } } 運行結果: ![](https://images2018.cnblogs.com/blog/1296864/201804/1296864-20180416095342534-2087470503.png)
參考http://www.cnblogs.com/rocedu/p/6736847.htmlthis
對設計模式示例進行擴充,體會OCP原則和DIP原則的應用,初步理解設計模式
用本身的學號%6進行取餘運算,根據結果進行代碼擴充:
4: 讓系統支持Float類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
建立項目等一系列操做同上
輸入的src內代碼爲:
abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=100; } public void DisplayValue(){ System.out.println (value); } } class Floater extends Data { float value; Floater() { value=5320; } public void DisplayValue(){ System.out.println (value); } } abstract class Factory { abstract public Data CreateDataObject(); } class IntFactory extends Factory { public Data CreateDataObject(){ return new Integer(); } } class FloatFactory extends Factory { public Data CreateDataObject(){ return new Floater(); } } class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(new FloatFactory()); d.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)
新建項目,src內新建java class
輸入以下代碼:
import java.util.Objects; import static java.lang.Math.sqrt; import static java.lang.StrictMath.round; public class Fushu { public static void main (String args[]){ Complex sq = new Complex(1.0,4.0); Complex gst = new Complex(1.0,3.0); } static class Complex{ double x; double y; Complex(double x,double y){ this.x=x; this.y=y; } Complex ComplexAdd(Complex a){ return new Complex(x+a.x,y+a.y ); } Complex ComplexSub(Complex a){ return new Complex(x-a.x,y-a.y ); } Complex ComplexMulti(Complex a){ return new Complex(x*a.x-y*a.y,x*a.y+y*a.x); } Complex ComplexDiv(Complex a){ double g = sqrt(a.x*a.x)+sqrt(a.y*a.y); double s = (x*a.y-y*a.x); return new Complex ((x*a.x)+(y*a.y)/g,round(s/g)); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Complex complex = (Complex) o; return Double.compare(complex.x, x) == 0 && Double.compare(complex.y, y) == 0; } @Override public String toString() { if (y > 0) { return "("+x+"+"+y+"i"+")"; } else { return "("+x+"-"+y+"i"+")"; } } } }
在test文件目錄下新建class
輸入代碼以下:
import junit.framework.TestCase; import org.junit.Test; public class FushuTest extends TestCase { Fushu.Complex sq = new Fushu.Complex(1.0, 4.0); Fushu.Complex gst = new Fushu.Complex(1.0, 3.0); @Test public void testAdd() { assertEquals("(2.0+7.0i)", sq.ComplexAdd(gst).toString()); } @Test public void testSub() { assertEquals("(0.0+1.0i)", sq.ComplexSub(gst).toString()); } @Test public void testMul() { assertEquals("(-11.0+7.0i)", sq.ComplexMulti(gst).toString()); } @Test public void testDiv() { assertEquals("(4.0-0.0i)", sq.ComplexDiv(gst).toString()); } }
運行截圖:
1、在IDEA中按照博客步驟找不到Generator,而且以後出現安裝失敗
解決方法:在setting中直接搜索Generator2.0安裝
2、老師的博客中SringBufferDeo類裏面測試理論與實際不符
解決辦法:在append()中加入12345
3、單元測試時出現test not found錯誤
解決方法:測試類中類名不能大寫,改爲小寫便可
實驗PSP:
步驟 | 耗時 | 百分比 |
---|---|---|
需求分析 | 30m | 16% |
設計 | 60m | 33% |
實現 | 30m | 16% |
測試 | 20m | 11% |
分析總結 | 40m | 22% |