北京电子科技学院(besti)实验报告
实验内容
初步掌握单元测试和TDD理解并掌握面向对象三要素:封装、继承、多态初步掌握UML建模熟悉S.O.L.I.D原则了解设计模式实验步骤
一、三种代码:
产品代码
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 "错误"; } }编写MyUtilTest的测试模块
public class MyUtilTest { public static void main(String[] args) { // 百分制成绩是50时应该返回五级制的“不及格” if(MyUtil.percentage2fivegrade(50) != "不及格") System.out.println("test failed!"); else System.out.println("test passed!"); } }结果:
只有一组输入的测试是不充分的,我们把一般情况都测试一下,代码如下:
public class MyUtilTest { public static void main(String[] args) { //测试正常情况 if(MyUtil.percentage2fivegrade(55) != "不及格") System.out.println("test failed!"); else if(MyUtil.percentage2fivegrade(65) != "及格") System.out.println("test failed!"); else if(MyUtil.percentage2fivegrade(75) != "中等") System.out.println("test failed!"); else if(MyUtil.percentage2fivegrade(85) != "良好") System.out.println("test failed!"); else if(MyUtil.percentage2fivegrade(95) != "优秀") System.out.println("test failed!"); 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 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() + "!"; } }
四、练习
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; } } } }结果:
import java.util.Scanner; public class ComplexMain { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入两个复数的实部和虚部:(a+bi、c+di)"); System.out.printf("a ="); int a=scanner.nextInt(); System.out.printf("b ="); int b=scanner.nextInt(); System.out.printf("c ="); int c=scanner.nextInt(); System.out.printf("d ="); int d=scanner.nextInt(); Complex fushu1=new Complex(a,b); Complex fushu2=new Complex(c,d); while(true) { System.out.println("请输入需要进行的运算:1、ADD 2、SUBTRACT 3、MULTIPLY "); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.println(Complex.addition(fushu1, fushu2)); break; case 2: System.out.println(Complex.subtract(fushu1, fushu2)); break; case 3: System.out.println(Complex.multiplication(fushu1, fushu2)); break; default:System.out.println("ERROR!!!"); } } } }结果:
转载于:https://www.cnblogs.com/20144303sys/p/5402785.html
