实验内容
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态
- 初步掌握UML建模
- 熟悉S.O.L.I.D原则
- 了解设计模式
实验步骤
单元测试
代码及则是结果:
public class MyUtil{
public static String percentage2fivegrade(int grade){
//如果成绩小于60,转成“不及格”
if (grade < )
return "不及格";
//如果成绩在60与70之间,转成“及格”
else if (grade < )
return "及格";
//如果成绩在70与80之间,转成“中等”
else if (grade < )
return "中等";
//如果成绩在80与90之间,转成“良好”
else if (grade < )
return "良好";
//如果成绩在90与100之间,转成“优秀”
else if (grade < )
return "优秀";
//其他,转成“错误”
else
return "错误";
}
}
public class MyUtilTest {
public static void main(String[] args) {
// 百分制成绩是50时应该返回五级制的“不及格”
if(MyUtil.percentage2fivegrade() != "不及格")
System.out.println("test failed!");
else
System.out.println("test passed!");
}
}
public class MyUtilTest {
public static void main(String[] args) {
//测试正常情况
if(MyUtil.percentage2fivegrade() != "不及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade() != "及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade() != "中等")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade() != "良好")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade() != "优秀")
System.out.println("test failed!");
else
System.out.println("test passed!");
}
}
public class MyUtilTest {
public static void main(String[] args) {
//测试边界情况
if(MyUtil.percentage2fivegrade(0) != "不及格")
System.out.println("test failed 1!");
else if(MyUtil.percentage2fivegrade(60) != "及格")
System.out.println("test failed 2!");
else if(MyUtil.percentage2fivegrade(70) != "中等")
System.out.println("test failed 3!");
else if(MyUtil.percentage2fivegrade(80) != "良好")
System.out.println("test failed 4!");
else if(MyUtil.percentage2fivegrade(90) != "优秀")
System.out.println("test failed 5!");
else if(MyUtil.percentage2fivegrade(100) != "优秀")
System.out.println("test failed 6!");
else
System.out.println("test passed!");
}
}
TDD
的一般步骤如下:
- 明确当前要完成的功能,记录成一个测试列表
- 快速完成编写针对此功能的测试用例
- 测试代码编译不通过(没产品代码呢)
- 编写产品代码
- 测试通过
- 对代码进行重构
- 循环完成所有功能
TDD的编码节奏
- 增加测试代码,JUnit出现红条
- 修改产品代码
- JUnit出现绿条,任务完成
面向对象三要素
- 抽象
- 封装、继承与多态
- 设计模式初步
S.O.L.I.D原则
面向对象三要素是“封装、继承、多态”,任何面向对象编程语言都会在语法上支持这三要素。如何借助抽象思维用好三要素特别是多态还是非常困难的,S.O.L.I.D类设计原则是一个很好的指导:
- SRP(Single Responsibility Principle,单一职责原则)
- OCP(Open-Closed Principle,开放-封闭原则)
- LSP(Liskov Substitusion Principle,Liskov替换原则)
- ISP(Interface Segregation Principle,接口分离原则)
- DIP(Dependency Inversion Principle,依赖倒置原则)
练习
运行结果:
public class TestComplex
{ public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入复数A:");
float realpart1=sc.nextInt();float imagepart1=sc.nextInt();
System.out.println("请输入复数B:");
float realpart2=sc.nextInt();float imagepart2=sc.nextInt();
Complex c1=new Complex(realpart1,imagepart1);
Complex c2=new Complex(realpart2,imagepart2);
System.out.println("请选择运算:\n"+
"(选择 1 执行加法运算)\n"+
"(选择 2 执行减法运算)\n"+
"(选择 3 执行乘法运算)\n"+
"(选择 4 执行除法运算)");
int s=sc.nextInt();
Complex C=new Complex();
for(;;)
{
C.PressButton(s,c1,c2);
System.out.println("Please input complex A again:");
realpart1=sc.nextInt();imagepart1=sc.nextInt();
System.out.println("Please input complex B again:");
realpart2=sc.nextInt();imagepart2=sc.nextInt();
System.out.println("Please choose the operate pattern:\n"+
"(choose 1 will run add operation)\n"+
"(choose 2 will run sub operation)\n"+
"(choose 3 will run mul operation)\n"+
"(choose 4 will run div operation)");
s=sc.nextInt();
if(s==-1)
{
System.out.println("Game over!");
break;
}
} }
}