(一)单元测试
(1)三种代码
①伪代码
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
②产品代码
public class MyUtil{ public static String percentage2fivegrade(int grade){ //如果成绩小于60,转成“不及格” 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 "优秀"; //其他,转成“错误” else return "错误"; } }
③测试代码
import junit.framework.TestCase; import org.junit.Test; 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(115)); } @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)安装JUnit:File-Settings-Plugins-Marketplace
搜索JUnit,点击Install,之后重启IDEA安装成功。
(3)进行代码测试,测试正常、异常、边界结果如下:
(二)以TDD的方式研究学习StringBuffer
1、老师给出的StringBuffer的例子:
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()); } }
2、改写代码使其可以进行JUnit测试
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(){ return buffer.capacity(); } public int length(){ return buffer.length(); } public int indexOf(String buf) { return buffer.indexOf(buf); } }
3、测试代码
import junit.framework.TestCase; import org.junit.Test; public class StringBufferDemoTest extends TestCase { StringBuffer a1 = new StringBuffer("StringBuffer"); StringBuffer a2 = new StringBuffer("StringBufferStringBuffer"); StringBuffer a3 = new StringBuffer("StringBuffer used by 20175215"); @Test public void testCharAt() throws Exception{//验证返回是否是整个字符串中的第x个字符 assertEquals('S',a1.charAt(0)); assertEquals('t',a2.charAt(13)); assertEquals('b',a3.charAt(18)); } @Test public void testcapacity() throws Exception{//验证容量 assertEquals(28,a1.capacity()); assertEquals(40,a2.capacity()); assertEquals(45,a3.capacity()); } @Test public void testlength() throws Exception{//验证字符串的长度 assertEquals(12,a1.length()); assertEquals(24,a2.length()); assertEquals(29,a3.length()); } @Test public void testindexOf(){//验证所在位置 assertEquals(6,a1.indexOf("Buff")); assertEquals(1,a2.indexOf("tring")); assertEquals(25,a3.indexOf("5215")); } }
4.测试结果:
(三)对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
1、产品代码
abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=100; } @Override public void DisplayValue(){ System.out.println (value); } } class Float extends Data { float value; Float() { value = 20175228; } @Override 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 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(); } }
public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(new FloatFactory()); d.DisplayData(); } }
2、运行结果截图:
(四)以TDD的方式开发一个复数类Complex
1、产品代码
public class ComplexNumber { double RealPart; double ImagePart; public ComplexNumber(){ this.ImagePart=0; this.RealPart=0; } public ComplexNumber(double RealPart, double ImagePart) { this.RealPart = RealPart; this.ImagePart = ImagePart; } public double getRealPart(){ return this.RealPart; } public double getImagePart(){ return this.ImagePart; } public double setRealPart(double R){ this.RealPart = R; return R; } public double setImagePart(double I){ this.ImagePart = I; return I; } ComplexNumber ComplexAdd(ComplexNumber a){ double R = a.getRealPart(); double I = a.getImagePart(); double cr = R + this.RealPart; double ci = I + this.ImagePart; ComplexNumber c = new ComplexNumber(cr,ci); return c; } ComplexNumber ComplexSub(ComplexNumber a){ double R = a.getRealPart(); double I = a.getImagePart(); double cr = R - this.RealPart; double ci = I - this.ImagePart; ComplexNumber c = new ComplexNumber(cr,ci); return c; } ComplexNumber ComplexMultib(ComplexNumber a){ double R = a.getRealPart(); double I = a.getImagePart(); double cr = R * this.RealPart; double ci = I * this.ImagePart; ComplexNumber c = new ComplexNumber(cr,ci); return c; } ComplexNumber ComplexDiv(ComplexNumber a){ double R = a.getRealPart(); double I = a.getImagePart(); double cr = R / this.RealPart; double ci = I / this.ImagePart; ComplexNumber c = new ComplexNumber(cr,ci); return c; } public String toString() { String complex=new String(); if (ImagePart > 0) complex = "(" + RealPart + "+" + ImagePart + "i" + ")"; if (ImagePart == 0) complex = "(" + RealPart + ")" ; if (ImagePart < 0) complex = "(" + RealPart + "-" + ImagePart + "i" + ")"; return complex; } }
2、测试代码
import junit.framework.TestCase; import org.junit.Test; public class ComplexNumberTest extends TestCase { ComplexNumber a=new ComplexNumber(14,4); ComplexNumber b=new ComplexNumber(5,-2); ComplexNumber c=new ComplexNumber(2,1); ComplexNumber d=new ComplexNumber(4,0); ComplexNumber m=new ComplexNumber(); @Test public void testGetRealPart() throws Exception { assertEquals(14.0,a.getRealPart()); assertEquals(5.0,b.getRealPart()); } @Test public void testGetImagePart() throws Exception { assertEquals(4.0,a.getImagePart()); assertEquals(-2.0,b.getImagePart()); } @Test public void testSetRealPart() throws Exception { m.setRealPart(1); assertEquals(1.0,m.getRealPart()); } @Test public void testSetImagePart() throws Exception { m.setImagePart(4); assertEquals(4.0,m.getImagePart()); } @Test public void testComplexNumberAdd() throws Exception { m=a.ComplexAdd(b); assertEquals("(19.0+2.0i)",m.toString()); m=a.ComplexAdd(c); assertEquals("(16.0+5.0i)",m.toString()); } @Test public void testComplexNumberSub() throws Exception { m=b.ComplexSub(a); assertEquals("(9.0+6.0i)",m.toString()); m=c.ComplexSub(b); assertEquals("(3.0--3.0i)",m.toString()); } @Test public void testComplexNumberMultib() throws Exception { m=a.ComplexMultib(b); assertEquals("(70.0--8.0i)",m.toString()); m=a.ComplexMultib(c); assertEquals("(28.0+4.0i)",m.toString()); } @Test public void testComplexNumberDiv() throws Exception { m=c.ComplexDiv(a); assertEquals("(7.0+4.0i)",m.toString()); m=c.ComplexDiv(b); assertEquals("(2.5--2.0i)",m.toString()); } }
3、测试截图
(五)使用StarUML对实验二中的代码进行建模
二、实验中遇到的问题
问题1:Test文件显示出错。
问题1解决方案:如下图所示,File-Project Structure-Dependices,点击“+”选择如下的jar文件即可。
三、PSP表格
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 45分钟 | 17% |
设计 | 40分钟 | 16% |
代码实现 | 70分钟 | 30% |
测试 | 60分钟 | 25% |
分析总结 | 30分钟 | 12% |