一、实验内容
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态
- 初步掌握UML建模
- 熟悉S.O.L.I.D原则
- 了解设计模式 - 二、实验步骤- (一)单元测试1.三种代码:伪代码、测试代码、产品代码
 需求:在一个MyUtil类中解决一个百分制成绩转成“优、良、中、及格、不及格”五级制成绩的功能。
 伪代码:
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”之后用java语言编程MyUtil.java
public class MyUtil{public static String percentage2fivegrade(int grade){//如果成绩小于60,转成“不及格”if (grade<0)return "错误";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 "优秀";//其他,转成“错误”elsereturn "错误";}
}再写一个测试代码MyUtilTest.java来检验产品代码
 测试三种情况
 1.正常情况
 2.错误情况(负数,超过100的数)
 3.边界情况(0,60,70,80,90,100)
 测试代码:
import junit.framework.TestCase;
import org.junit.Test;public class MyUtilTest extends TestCase {@Testpublic void testNormal() {assertEquals("不及格", MyUtil.percentage2fivegrade(55));assertEquals("及格", MyUtil.percentage2fivegrade(65));assertEquals("中等", MyUtil.percentage2fivegrade(75));assertEquals("良好", MyUtil.percentage2fivegrade(85));assertEquals("优秀", MyUtil.percentage2fivegrade(95));}@Testpublic void testExceptions(){assertEquals("错误", MyUtil.percentage2fivegrade(105));assertEquals("错误", MyUtil.percentage2fivegrade(-55));}@Testpublic 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(测试代码->产品代码)
 测试StringBufferDemo类的方法有charAt()、capacity()、indexOf()、length():char charAt(int index)返回此序列中指定索引处的 char 值。int capacity()返回当前容量。int indexOf(String str)返回第一次出现的指定子字符串在该字符串中的索引。int length()返回长度(字符数)。
 代码:
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("tring12345"));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");@Testpublic void testcharAt() throws Exception{assertEquals('S',a.charAt(0));assertEquals('n',a.charAt(4));assertEquals('u',a.charAt(7));}public void testcapacity() throws Exception{assertEquals(28,a.capacity());assertEquals(40,b.capacity());assertEquals(52,c.capacity());}public void testlength() throws Exception{assertEquals(12,a.length());assertEquals(24,b.length());assertEquals(36,c.length());}public void testindexOf() throws Exception{assertEquals(0,a.indexOf("Str"));assertEquals(5,a.indexOf("gBu"));assertEquals(6,b.indexOf("Buf"));assertEquals(7,b.indexOf("uff"));assertEquals(9,c.indexOf("fer"));assertEquals(10,c.indexOf("erS"));}
}测试通过截图:
(二)对设计模式示例进行扩充,让其支持Long类
要求支持Long类,这样需Document类修改构造方法,这违背了OCP原则,。封装、继承、多态解决不了问题,需要添加:class Long extends Dataclass LongFactory extends Factory
 代码:
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=2017530999;}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 LonFactory extends Factory {public Data CreateDataObject(){return new Long();}
}
class Document {Data pd;Document(Factory pf){pd = pf.CreateDataObject();}public void DisplayData(){pd.DisplayValue();}
}
public class MyDoc {static Document e;public static void main(String[] args) {e=new Document(new LonFactory());e.DisplayData();}
}运行通过截图
(三)以TDD的方式开发一个复数类Complex
方法:
getA(int a);返回实部
getB(int b);返回虚部
ComplexAdd(Complex c);实现复数相加
ComplexMinus(Complex c);实现复数相减
ComplexMulti(Complex c);实现复数相乘
ComplexDiv(Complex c);实现复数相除代码:
public class Complex {    //a + biprivate double a;private double b;public Complex(){                      //构造方法,置0this.a = 0;this.b = 0;}public Complex(double a, double b) {  //构造方法,初始化一个复数this.a = a;this.b = b;}public  double getA(){                  //获取实部return this.a;}public double getB(){                  //获取虚部return this.b;}public double setA(double a){         //设置实部this.a = a;return a;}public double setB(double b){         //设置虚部this.b = b;return b;}Complex ComplexAdd(Complex c){//复数相加double a = c.getA();double b = c.getB();double newA = a + this.a;double newB = b + this.b;Complex Result = new Complex(newA,newB);return Result;}Complex ComplexMinus(Complex c){//复数相减double a = c.getA();double b = c.getB();double newA = a - this.a;double newB = b - this.b;Complex Result = new Complex(newA,newB);return Result;}Complex ComplexMulti(Complex c){//复数相乘double a = c.getA();double b = c.getB();double newA = a * this.a;double newB = b * this.b;Complex Result = new Complex(newA,newB);return Result;}Complex ComplexDiv(Complex c){//复数相乘double a = c.getA();double b = c.getB();double newA = a / this.a;double newB = b / this.b;Complex Result = new Complex(newA,newB);return Result;}public String toString() {String s = " ";if (b > 0)s =  a + "+" + b + "i";if (b == 0)s =  a + "";if (b < 0)s = a + " " + b + "i";return s;}
}测试代码:
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.*;public class ComplexTest extends TestCase {Complex c1 = new Complex(0, 3);Complex c2 = new Complex(-1, -1);Complex c3 = new Complex(2,1);@Testpublic void testgetRealPart() throws Exception {assertEquals(-1.0, new Complex().setA(-1.0));assertEquals(5.0, new Complex().setA(5.0));assertEquals(0.0, new Complex().setA(0.0));}@Testpublic void testgetImagePart() throws Exception {assertEquals(-1.0, new Complex().setB(-1.0));assertEquals(5.0, new Complex().setB(5.0));assertEquals(0.0, new Complex().setB(0.0));}@Testpublic void testComplexAdd() throws Exception {assertEquals("-1.0+2.0i", c1.ComplexAdd(c2).toString());assertEquals("2.0+4.0i", c1.ComplexAdd(c3).toString());assertEquals("1.0", c2.ComplexAdd(c3).toString());}@Testpublic void testComplexSub() throws Exception {assertEquals("-1.0 -4.0i", c1.ComplexMinus(c2).toString());assertEquals("2.0 -2.0i", c1.ComplexMinus(c3).toString());assertEquals("3.0+2.0i", c2.ComplexMinus(c3).toString());}@Testpublic void testComplexMulti() throws Exception {assertEquals("-0.0 -3.0i", c1.ComplexMulti(c2).toString());assertEquals("0.0+3.0i", c1.ComplexMulti(c3).toString());assertEquals("-2.0 -1.0i", c2.ComplexMulti(c3).toString());}@Testpublic void testComplexComplexDiv() throws Exception {assertEquals("-0.0 -3.0i", c2.ComplexDiv(c1).toString());assertEquals("-0.0 -3.0i", c2.ComplexDiv(c1).toString());assertEquals("-2.0 -1.0i", c2.ComplexDiv(c3).toString());}
}测试截图
(四)面向对象三要素
使用UML对实验二中代码进行建模 
 代码:
public abstract class Animal {private String color;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public abstract String shout(); 
}
public class Dog extends Animal{public String shout(){return "汪汪";}public String toString(){return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";}
}
public class Cat extends Animal{public String shout(){return "喵喵";}public String toString(){return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";}
}UML图
实验中遇到的问题
1.Junit的包安装上之后无法使用。
 解决办法:最开始只装了一个junit.jar的包,没有装另外一个junit-4.12.jar的包导致无法使用
2.UML软件不会使用
 解决办法:参考教程:https://blog.csdn.net/luansha0/article/details/82260678
PSP
| 步骤 | 耗时 | 百分比 | 
|---|---|---|
| 需求分析 | 30min | 10% | 
| 设计 | 30min | 10% | 
| 代码实现 | 110min | 36.7% | 
| 测试 | 70min | 23.3% | 
| 分析总结 | 60min | 20% | 
感悟
不知道怎么回事,老师的博客图片不能显示,对我做实验造成了极大的困扰,后来参照着20175306王佳烁、20175313张黎仙同学的博客上面的步骤才得以完成,再此谢谢二位前辈!
 参考博客
 20175306王佳烁:https://www.cnblogs.com/wjs123456/p/10700936.html
 20175313张黎仙:https://www.cnblogs.com/xiannvyeye/p/10720425.html