20145313第二次实验
单元测试
代码实现
测试用例
找到BUG,进行修改
TDD测试用例
建立MyUtil类并测试
模块化
UML建模
继承
练习
源代码
package exp2;
public class Complex {
protected int a;
protected int b;
public Complex(int a, int b) {
this.a = a;
this.b = b;
}
public String toString() {
return this.a + " + " + this.b + "i";
}
public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}
public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}
public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}
public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}
测试代码
package exp2;
public class Comtest {
public static void main(String[] args) throws Exception{
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}
测试截图
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 20min | 5 |
设计 | 20min | 20 |
代码实现 | 50min | 50 |
测试 | 20min | 15 |
分析总结 | 20min | 10 |