《第二次实验报告》

一、单元测试与TDD:

编程前需要理清思路,知道需要做什么,如何做,可以先用伪代码来描述该过程。

用编程语言将伪代码翻译处理后,就是产品代码了,伪代码是对产品代码最好的注释。

完成产品代码后,还需要测试代码来证明自己的代码没有问题,java编程对类实现的测试叫单元测试。

单元测试练习

伪代码如下
···
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
···

经过数次调试bug,得到最终代码

···
package experiment2;
public class MyUtil {
public static String percentage2fivegrade(int grade){
//如果成绩小于0,转成“错误”
if (grade<0) return "错误";
//如果成绩小于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 "错误";
}
}
···

然后接下来是编写测试代码,检验自己的产品代码有无错误。选中MyUtil,在左边出现的小灯泡下拉菜单中选中Create Test:

接着在创建出的MyUtilTest中编写如下测试代码:

···
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(105));
    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:

···
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());
}
}
···
通过学习,选择了charAt(),length(),capacity()三个方法,开始编写产品代码:
···
public class StringBufferDemo {
StringBuffer buffer=new StringBuffer();
public Character charAt(int i){
return buffer.charAt(i);
}
public int length(){
return buffer.length();
}
public int capacity(){
return buffer.capacity();
}
}
···
编写测试代码,检验产品代码的正确性:
···
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 testBoundry() {
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的方式开发一个复数类Comeplex:

要求:定义属性并生成getter,setter;定义构造函数,定义公有方法(加减乘除)。

按要求编写产品代码如下:

···
class complex {
double a, b,p;

complex() {
    this.a = 0;
    this.b = 0;
}

complex(double a) {
    this.a = a;
    this.b = 0;
}

complex(double a, double b) {
    this.a = a;
    this.b = b;
}

complex add(complex p1, complex p2) {
    complex p = new complex(p1.a + p2.a, p1.b + p2.b);

    return p;
}

complex minus(complex p1, complex p2) {
    complex p = new complex(p1.a - p2.a, p1.b - p2.b);
    return p;
}

complex multiply(complex p1, complex p2) {
    complex p = new complex(p1.a*p2.a-p1.b*p2.b,p1.a*p2.b+p1.b*p2.a);

    return p;
}


complex divide(complex p1, complex p2) {
    complex p = new complex((p1.a*p2.a+p1.b*p2.b)/(p2.a*p2.a+p2.b*p2.b)+(p1.b*p2.a-p1.a*p2.b)/(p2.a*p2.a+p2.b*p2.b),0);

    return p;
}


void print() {
    System.out.println("The complex number is :");
    if (this.b != 0)
        System.out.println(this.a + "+" + this.b + "i");
    else
        System.out.println(this.a);
}

}
···

编写测试代码检验产品代码的正确性,测试代码如下:

···
import junit.framework.TestCase;

class complextest{
public static void main(String[] args) {
complex c = new complex();
complex c1 = new complex(7,7 );
complex c2 = new complex(5,9 );
c1.print();
c2.print();
System.out.println("Plus the complex number is 😊;
System.out.println((c.add(c1, c2).a + "+" + c.add(c1, c2).b + "i")
.toString());
System.out.println("Minus the complex number is 😊;
System.out .println((c.minus(c1, c2).a + "+" + c.minus(c1, c2).b + "i")
.toString());
System.out.println("Multiply the complex number is 😊;
System.out .println((c.multiply(c1, c2).a + "+" + c.multiply(c1, c2).b +"i")
.toString());
System.out.println("Divede the complex number is 😊;
System.out .println(c.divide(c1,c2).a );

}

}
···

截图:

四、体会OCP原则和DIP原则的应用:

以我的学号模6所得,代码如下

···
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Short extends Data {
short value;
Short() {
value = 5319;
}
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 ShortFactory extends Factory {
public Data CreateDataObject(){
return new Short();
}
}
//Client classes
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d;
public static void main(String[] args) {
d = new Document(new ShortFactory());
d.DisplayData();
}
}
···

截图:

五、StarUML进行建模,画类图:

posted @ 2018-04-22 21:44  高君天  阅读(422)  评论(0编辑  收藏  举报